:
#
#       This script is used to start/stop scologin
#       
#       usage:  scologin [disable|enable|init|reread|start|stop|query]
#
#           disable  
#               stops the current scologin session and disables 
#               automatic startup of scologin at boot up.
#           enable 
#               starts a scologin session and enables automatic 
#               startup of scologin at boot up.
#           init 
#               used a bootup to disable gettys on ttys that
#               will run scologin.
#           reread 
#               rereads Xservers file (this option is currently disabled)
#           start 
#               starts a scologin session
#		enables scologin if necessary
#           stop 
#               stops the scologin session
#           query 
#               query current state of scologin
#

# Define return values
: ${OK=0} ${FAIL=1} ${STOP=10} ${HALT=11}

TMP=/tmp/scl$$
STATE=
DISABLED_TTYS=
KILL_PID=
TTYS=/usr/lib/X11/scologin/ttyFile
INITD=/etc/conf/init.d/scologin
XSERVERS=/usr/lib/X11/scologin/Xservers
GRAFDEV=/usr/lib/grafinfo/grafdev
SCL="scl:b:once:/etc/rc2.d/S91scologin start no_switch\nsclb:2:bootwait:/etc/scologin init"

export STATE

# Define traps for critical and non critical code.
set_trap()  {   
    trap 'echo "Interrupted! Exiting ... \c"; cleanup $FAIL' 1 2 3 15
}
unset_trap()  {
    trap '' 1 2 3 15
}
 
# Remove temp files and exit with the status passed as argument
cleanup() {
    trap '' 1 2 3 15
    rm -f $TMP
    case "$STATE" in
        tty_enable)
            set_ttys enable
            ;;
        tty_disable)
            set_ttys disable
            ;;
        enable)
            echo $SCL > $INITD
            mkinittab
            ;;
        disable)
            rm -f $INITD
            mkinittab
            ;;
        reread1)
            x=`servers`
            if [ "$DISABLED_TTYS" = "" ]
            then
                enable $x
            else
                for i in $x
                do
                    echo $DISABLED_TTYS | grep $i > /dev/null 2>&1
                    [ $? -eq '0' ] || enable $i
                done
            fi
            rm -f $TTYS
            echo "$DISABLED_TTYS" > $TTYS
            ;;
        reread2)
            if [ "$DISABLED_TTYS" != "" ]
            then
                for i in $DISABLED_TTYS
                do
                    echo $x | grep $i > /dev/null 2>&1
                    [ $? -eq '0' ] || enable $i
                done
            fi
            ;;
        kill)
            [ "$KILL_PID" != "" ] && kill -15 $KILL_PID
            ;;
    esac
    echo
    print_state
    exit $1
}

# Test for mouse installed on system.
ismouse () {
    [ -f /usr/lib/event/ttys ] || return 1
    rm -f $TMP
    grep -v "_pmouse" < /usr/lib/event/ttys > $TMP
    awk '/^[ \t]*#/ { next; }
        NF > 1      { found++; exit;}
        END         { exit !found; }' $TMP
    return $?
}


# 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
}


# Usage message
usage () {
    echo "usage: scologin [disable|enable|init|start|stop|query]"
}



# Print ttys that 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 list of ttys.
enable () {
    while  test $# -gt '0'
    do
        /bin/su root -c "/usr/bin/enable /dev/tty$1" > /dev/null 2>&1
        shift
    done
}


# Disable list of ttys.
disable () {
    while  test $# -gt '0'
    do
        /bin/su root -c "/usr/bin/disable /dev/tty$1" > /dev/null 2>&1
        shift
    done
}

# Enable/disable ttys used by 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 [ "$x" != "" ]
    then
        if [ "$state" = "enable" ]
        then
            enable $x
        else
            disable $x
        fi
    fi 
}


# Switch to a screen that will be running scologin.
switch_screen () {
    x=`servers`
    if [ "$x" != "" ]
    then
        set $x
        if [ $# -gt '0' ]
        then
            screen=`expr $1 - 1`
            echo "\033[${screen}z\c"
        fi
    fi
}



# Print out list of ttys running scologin.
print_ttys () {
    if `local_servers`
    then
        x=`servers`
        if [ "$x" != "" ]
        then
            set $x
            echo "on \c"
            while [ $# -gt '0' ]
            do
                echo "/dev/tty$1 \c"
                shift
            done
        fi 
    fi
}


# Print out scologin state.
print_state ()
{
    if [ -r /usr/lib/X11/scologin/pidFile ]
    then
        echo "Scologin is currently running."
    else
        echo "Scologin is not currently running."
    fi

    if [ -f $INITD ] && `grep -y scologin /etc/inittab > /dev/null 2>&1`
    then
        echo "Scologin is enabled."
    else
        echo "Scologin is disabled."
    fi
}


# Update inittab file
mkinittab () {
    /etc/conf/bin/idmkinit
    cp /etc/conf/cf.d/inittab /etc/inittab
}



# Configure scologin
configure () {
    case $1 in
        start)
            set `who -r`
            if [ "$9" != "S" ]
            then
                exit $OK
            elif [ -r  /usr/lib/X11/scologin/pidFile ] ;
            then
                echo "Scologin is already running."
            else
                if  `ismouse` 
                then
                    if [ -f $INITD ]
                    then
                        echo "Starting scologin \c"
                        print_ttys
                        echo "... \c"
                        STATE="tty_enable"
                        `local_servers` && set_ttys disable
                        STATE=
			init b
                        echo "done."
                    else
  			configure enable
                    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
                KILL_PID=$1
                /bin/rm -rf /usr/lib/X11/scologin/pidFile
                STATE=kill
                kill -15 $1
                STATE=tty_enable
                set_ttys enable
                STATE=
                echo "done."
            else
                echo "Scologin is not currently running."
            fi
            ;;

#       reread)
#           if [ -r /usr/lib/X11/scologin/pidFile ] ;
#           then
#               echo "Scologin re-reading Xservers file ... \c"
#               [ -f $TTYS ] && read DISABLED_TTYS < $TTYS
#               x=`servers`
#               echo $x > $TTYS
#               STATE=reread1
#               disable $x
#               read line < /usr/lib/X11/scologin/pidFile
#               set $line
#               kill -1 $1
#               STATE=reread2
#               if [ "$DISABLED_TTYS" != "" ]
#               then
#                   for i in $DISABLED_TTYS
#                   do
#                       echo $x | grep $i > /dev/null 2>&1
#                       [ $? -eq '0' ] || enable $i
#                   done
#               fi
#               STATE=
#               echo "done."
#           else
#               echo "Scologin is not currently running."
#           fi
#           ;;

        query)
            print_state
            ;;

        init)
            if  `ismouse` && `local_servers` && [ -f $INITD ] 
            then
                set_ttys disable
            else
                set_ttys enable
            fi
            ;;

        enable)
            if  `ismouse` 
            then
                echo "Enabling scologin ... \c"
                STATE=disable
                rm -f $INITD
                echo $SCL > $INITD
                mkinittab
                STATE=
                echo "done."
		configure start
            else
                echo "Scologin: mouse needs to be configured."
            fi
            ;;

        disable)
            echo "Disabling scologin ... \c"
            rm -f $INITD
            STATE=enable
            mkinittab
            STATE=
            echo "done."
            configure stop
            ;;

        *)
            usage
            ;;
    esac
}


#
# Main
#
set_trap
configure $*
rm -f $TMP
