:
#
#       This script is used to start/stop scologin
#	If the argument no_switch is used at startup, then
#	the console screen will not automatically be switched
#	to the scologin screen.
#       
#       usage:  scologin start [no_switch]
#		scologin stop
#

XSERVERS=/usr/lib/X11/scologin/Xservers
GRAFDEV=/usr/lib/grafinfo/grafdev
INITD=/etc/conf/init.d/scologin
TTYS=/usr/lib/X11/scologin/ttyFile
TMP=/tmp/tmp$$
NO_SWITCH=no_switch


#
# Test for presence of mouse
#
ismouse () 
{

    [ -f /usr/lib/event/ttys ] || return 1

    # Get rid of pseudo mice, so we only check for real ones!
    rm -f $TMP
    grep -v "_pmouse" < /usr/lib/event/ttys > $TMP
    awk '/^[ \t]*#/ { next; }
        NF > 1      { found++; exit;}
        END         { exit !found; }' $TMP
    return $?

}



#
# Find out which ttys will run scologin.
#
servers () 
{
    h=`awk '/^:[0-9]/ {sub(".*-crt */dev/tty", "");
        if ($1>0 && $1<65) {tty=$1; print tty }}
        END {if (tty==0) {print tty; exit 1} else exit 0}' $XSERVERS`
    echo $h
}




#
# Enable or disable ttys that run scologin.
#
set_ttys () {

    state=$1
    x=`servers`
    if [ "$state" = "enable" ] && [ -f $TTYS ]
    then
        ttys=`cat $TTYS`
        rm -f $TTYS
        x="$ttys $x"
    elif [ "$state" = "disable" ]
    then
        [ -d /usr/lib/X11/scologin ] && echo $x > $TTYS
    fi

    if test "$x" != ""
    then
        set $x
        while  test $# -gt '0'
        do
            if [ "$state" = "enable" ]
            then
                /bin/su root -c "/usr/bin/enable /dev/tty$1" > /dev/null 2>&1
            else
                /bin/su root -c "/usr/bin/disable /dev/tty$1" > /dev/null 2>&1
            fi
            shift
        done
    fi 
}


#
# Switch to a screen that will be running scologin.
#
switch_screen () 
{
    SWITCH=$1
    x=`servers`
    if test "$x" != ""
    then
        set $x
        if test $# -gt '0'
        then
            screen=`expr $1 - 1`
	    if [ "$SWITCH" != "$NO_SWITCH" ]
	    then
                echo "\033[${screen}z\c" > /dev/console
	    fi
            echo "\033[2JScologin is starting (/dev/tty$1).  Please wait ...\c" > /dev/tty$1

        fi
    fi 
}



#
# Test if if X server is configured to run on console.
#
local_servers ()
{
    [ -f ${GRAFDEV} ] || return 1
    if `grep -y none ${GRAFDEV} > /dev/null 2>&1`
    then
        return 1
    else
        return 0
    fi
}



#
# Main
#
case $1 in
    start)
        if [ -f $INITD ]
        then
            if  `ismouse`
            then
                if `local_servers`
                then
                    /usr/bin/X11/scologin > /dev/null 2>&1 &
                    switch_screen $2
                else
                    /usr/bin/X11/scologin -nolocal > /dev/null 2>&1 &
                fi
            else
                echo "Scologin: mouse needs to be configured."
            fi
        fi
        ;;

    stop)
        if [ -r /usr/lib/X11/scologin/pidFile ] ;
        then
            echo "Stopping scologin ... \c"
            read line < /usr/lib/X11/scologin/pidFile
            set $line
            /bin/rm -rf /usr/lib/X11/scologin/pidFile
            kill -15 $1
            set_ttys enable
            echo "done."
        else
            echo "Scologin is not currently running."
        fi
        ;;

esac

rm -f $TMP
