#!/bin/sh
#
#  This script comments out all virtual terminals which aren't on the
# first console - that must remain so that 'xm console ...' works
# correctly.
#


prefix=$1


#
#  Source our common functions
#
if [ -e /usr/lib/xen-tools/common.sh ]; then
    . /usr/lib/xen-tools/common.sh
else
    . ./hooks/common.sh
fi


#
# Log our start
#
logMessage Script $0 starting

#
#  Remove the links for upstart
#
rm -f ${prefix}/etc/init/tty[!1].conf
[ -f ${prefix}/etc/init/tty1.conf ] && {
    sed -i -e s/tty1/hvc0/ ${prefix}/etc/init/tty1.conf
    mv ${prefix}/etc/init/tty1.conf  ${prefix}/etc/init/hvc0.conf
}
[ -f ${prefix}/etc/inittab ] && sed -i -e 's/^\([2-6].*:respawn*\)/#\1/' -e 's/^T/#\t/' -e 's/tty1$/hvc0/' ${prefix}/etc/inittab

#
#  Are we using an alternative serial device?
#
if [ ! -z "${serial_device}" ]; then

    serial_device=`basename ${serial_device}`

    # Let the user know.
    logMessage "Replacing default serial device (tty1) with ${serial_device}"

    # replace existing device.
    mv ${prefix}/etc/init/hvc0.conf  ${prefix}/etc/init/${serial_device}.conf
    sed -i -e s/hvc0/${serial_device}/ ${prefix}/etc/init/${serial_device}.conf
    [ -f ${prefix}/etc/inittab ] && sed -i -e s/hvc0/${serial_device}/ ${prefix}/etc/inittab



    # make sure that it is allowed to login.
    echo $serial_device >> ${prefix}/etc/securetty
fi


#
#  Log our finish
#
logMessage Script $0 finished
