#!/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

    if [ "${CPU}" = "Geode" ]; then
        consoledev=ttyS0
        initctl --quiet stop ${consoledev}
        pkill omgmenu
        cat >/etc/init/${consoledev}.conf <<-EOF
        	#LOCKED
        	# ${consoledev} - getty
        	#
        	# This service maintains a getty on ${consoledev} from the point the system is
        	# started until it is shut down again.

        	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/getty -L 115200 ${consoledev} vt102
EOF
        initctl --quiet start ${consoledev}
    else
        consoledev=tty1
        initctl --quiet stop ${consoledev}
        pkill omgmenu
        cat >/etc/init/${consoledev}.conf <<-EOF
        	# ${consoledev} - getty
        	#
        	# This service maintains a getty on ${consoledev} from the point the system is
        	# started until it is shut down again.

        	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/getty -8 38400 ${consoledev}
EOF
        initctl --quiet start ${consoledev}
    fi
else
    sed -i -e "s#ACTIVE_CONSOLES=/dev/tty.2-6.#ACTIVE_CONSOLES=/dev/tty[1-6]#" /etc/sysconfig/init
    initctl --quiet stop omgmenu
    pkill omgmenu
    rm -f /etc/init/omgmenu.conf
    echo "please reboot to disable OMGMENU"
fi