#! /bin/sh
#
# xcatconsole  Start/Stop serial console.
#
# Copyright (C) 2010 Andrew V. Stepanov <stanv@altlinux.org>
#
# chkconfig: 2345 40 60
# description: script made for ALT Linux. See original rh/fedora/sles genimage sources. \
#              nodeset <node> update PXE config <node> \
#              nodehm.serialport key=<node> value go to PXE config \
#              parse cmdline and bring up getty on specified console.
#
# limitation:  only one serial console can be configured
#
# attention:   This script allows root login via specified TTY
#
# config: /proc/cmdline

WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

LOCKFILE=/var/lock/subsys/xcatserial
RETVAL=0
GETTY="/sbin/agetty"
INITTAB="/etc/inittab"

# Serial port configuration
TTY=
SPEED=
HW=

##
# Init global variables
# 
initvars() {
        local key= value=
        local ret=-1
        local tty= speed= hw=

        for i in $(cat "/proc/cmdline"); do
                key="$(echo $i | cut -d= -f 1)"
                if [ "$key" = "console" -a "$i" != "console=tty0" ]; then

                        # Console parameters
                        value="$(echo $i | cut -d= -f 2)"
                        [ -n "$value" ] || continue

                        # Port
                        tty="$(echo $value | cut -d, -f 1)"
                        [ -e "/dev/$tty" ] || continue

                        # Speed
                        speed="$(echo $value | cut -d, -f 2 | cut -dn -f 1)"
                        [ -n "$speed" ] || continue

                        # Hardware (RTS/CTS) flow control
                        if echo $value | grep -qs n8r; then
                                hw="Yes"
			else
                                hw="No"
			fi

                        TTY=$tty
                        SPEED=$speed
                        HW=$hw
                        ret=0
                        break
                fi
        done

        return $ret
}

tty_used() {
        local used="No"

	# Test syntax.
	if [ $# != 1 ]; then
		msg_usage "tty_used {tty}"
		return 1
	fi

 	[ -e "/dev/$TTY" ] || return -1


	sed -e '/^[[:space:]]*#/d' -e '/^$/d' "$INITTAB" | grep -qs "$TTY" && used="Yes"

        is_yes $used
}

start()
{
        local msg=

        msg_starting "xCAT console"

        if ! [ -x "$GETTY" ]; then
                msg="can't find $GETTY"
                echo -n "$msg"
                failure "$msg. Necessary to install appropriate package."
                echo
                RETVAL=1
                return $RETVAL
        fi

        if ! initvars; then
                msg="node was not configured to use serial console"
                echo -n "$msg"
                passed "$msg. Check you xCAT configuration."
                RETVAL=$?
                echo
                return $RETVAL
        fi

        if tty_used $TTY; then
                msg="found configuration for $tty at $INITTAB"
                echo -n "$msg"
                passed "$msg"
                RETVAL=$?
                echo
                return $RETVAL
        fi

        # Populate inittab file
        echo "xco:2345:respawn:$GETTY ${HW:+-h} $TTY $SPEED xterm" >> "$INITTAB"

	# Allow root login
	if ! grep -qs "$TTY" "/etc/securetty"; then
		echo "$TTY" >> "/etc/securetty"
	fi

        # Tell init to re-examine the /etc/inittab file
        /sbin/init q

        msg="listen at $TTY, $SPEED, HW ctrl: $HW"
        echo -n "$msg"
        success "$msg"
        echo
        RETVAL=0

        touch "$LOCKFILE"

        return $RETVAL
}

stop()
{
        local msg=

        msg_stopping "xCAT console"
        
        rm -f "$LOCKFILE"

        if ! initvars; then
                msg="node was not configured to use serial console"
                echo -n "$msg"
                passed "$msg. Check you xCAT configuration."
                RETVAL=$?
                echo
                return $RETVAL
        fi
        
        if ! tty_used $TTY; then
                msg="$tty not configured at $INITTAB"
                echo -n "$msg"
                passed "$msg"
                RETVAL=$?
                echo
                return $RETVAL
        fi

        # Remove line from inittab file
        sed -i -e "/^[[:space:]]*#/! { /$TTY/d }" $INITTAB
        
        # Tell init to re-examine the /etc/inittab file
        /sbin/init q

        msg="stop listening at $TTY"
        echo -n "$msg"
        success "$msg"
        RETVAL=$?
        echo

        return $RETVAL
}

restart()
{
        stop
        start
}

reload()
{
        # Tell init to re-examine the /etc/inittab file
        /sbin/init q

        RETVAL=0
        return $RETVAL
}

constatus()
{
	RETVAL=0

        if ! initvars; then
                echo "node was not configured to use serial console"
		return $RETVAL
	fi
	
	if tty_used $TTY; then
		echo "$GETTY listening at $TTY"
	else
		echo "$GETTY not listening at $TTY"
	fi

	return $RETVAL
}

# See how we were called.
case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        reload)
                reload
                ;;
        restart)
                restart
                ;;
        condstop)
                if [ -e "$LOCKFILE" ]; then
                        stop
                fi
                ;;
        condrestart)
                if [ -e "$LOCKFILE" ]; then
                        restart
                fi
                ;;
        condreload)
                if [ -e "$LOCKFILE" ]; then
                        reload
                fi
                ;;
        status)
                constatus
                ;;
        *)
                msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
                RETVAL=1
esac


exit $RETVAL
