#!/bin/bash

. /tmp/cmdline

OSType=$(omgsysteminfo --val --filter=os.name)

if [ "$OSType" = "ubuntu" ]; then
    if [ "${CPU}" = "Geode" ]; then
        consoledev=ttyS0
    else
        consoledev=tty1
    fi

    if [ "$(tty)" == "/dev/${consoledev}" ];then
    	echo "Error: Running on console"
    	exit 1
    fi

    cat >/etc/init/${consoledev}.conf <<EOF
    # ${consoledev} - rungetty
    #
    # Run omgmenu on ${consoledev}

    start on stopped rc RUNLEVEL=[2345] and (
                not-container or
                container CONTAINER=lxc or
                container CONTAINER=lxc-libvirt)

    stop on runlevel [!2345]

    respawn
    exec /sbin/rungetty -u root -d 5 ${consoledev} -- /usr/bin/omgmenu
EOF

    initctl --quiet stop ${consoledev}
    pkill omgmenu
    initctl --quiet start ${consoledev}
else
        cat >/etc/init/omgmenu.conf <<-EOF
        #
        # Run omgmenu on tty1

        # start on runlevel [35]

        respawn
        exec /usr/bin/omgmenu </dev/tty1 >/dev/tty1 2>/dev/tty1
EOF
    sed -i -e "s#ACTIVE_CONSOLES=/dev/tty.1-6.#ACTIVE_CONSOLES=/dev/tty[2-6]#" /etc/sysconfig/init
    echo "please reboot to enable OMGMENU"
fi