#ident  "@(#)/usr/local/sysadmin/3BMGR 1.3 03/31/91" Author: Mr Reinhard Pfaffinger
# Office: 1924 CSS/SDA, Randolph AFB, Texas 78150
# Phone: AV 487-8649  Comm: (512) 652-8649
#
# This script allows a system administrator to shut down or reboot a 3B2/600G.
#
# Ver 1.1 - Added check for system console on firmware, single user,
#           make floppy key, and make disgnostic diskette operations.
#           Changed Single User state from 'init S' to 'init 1.'
# Ver 1.2 - Allowed external parameter, $1, to be passed in for execution
#           of menu choices from external modules.
#
# Ver 1.3 - Checked for $2 in option 4 so that /etc/mountall message does not
#           appear when a complete backup procedure is executed.
#

chkfortty() {
             if [ "$TERM" = "tty" ]
               then
                   echo  
             else
                 tput clear 
             fi
            }

warnuser() {
             NS=""
             T="`who | fgrep -v root | cut -d' ' -f1 | sed -e 's/ //g'`"
             for ID in $T
              do
                UID="`fgrep $ID /etc/passwd | cut -d: -f3 | cut -d: -f1`"
                if [ "$UID" != "0" ]
                  then
                      NS="doit"
                fi
              done
             if [ "$NS" = "doit" ]
               then
                   chkfortty
                   echo "You have elected to bring '`uname -n`' to $1 state.\n"
                   echo "Enter the GRACE PERIOD (in seconds) you wish the users to have before"
                   echo "taking the system down <RETURN> =>> \c"
                   read GRACE
                   if [ "$1" = "multi-user" ]
                     then
                         MSG="\n\n'`uname -n`' is being rebooted.\n\nYou will have $GRACE seconds to logoff!\n\nThank you.\n\nSystem Administrator"
                   else
                       MSG="\n\n'`uname -n`' is being taken down to '$1' state.\n\nYou will have $GRACE seconds to logoff!\n\nThank you.\n\nSystem Administrator"
                   fi
                   chkfortty
                   echo $MSG | wall
                   sleep $GRACE 
                   if [ "$1" != "floppykey" ]
                     then
                         echo "\nBeginning shutdown ... \n"
                         sleep 3
                   fi
             else
                 if [ "$1" != "floppykey" ]
                   then
                       chkfortty
                       echo "\nBeginning shutdown ... \n"
                       sleep 3
                 fi
             fi
           }
while :
 do
   if [ "$1" = "" ]
     then
         chkfortty
         echo "                     <<< AT&T 3B2/600G Machine Management Menu >>>\n"
         echo "                         1.  Reboot 3B2/600G (init 6)"
         echo "                         2.  Firmware (init 5)"
         echo "                         3.  Power Down (init 0)"
         echo "                         4.  Single User (init 1)"
         echo "                         5.  Make a Floppy Key (init 5)"
         echo "                         6.  Make a Diagnostics Diskette"
         echo "                         7.  Time of Last System Reboot\n"
         echo "                         q or Q to quit\n"
         echo "                         Enter response <RETURN> =>> \c"
         read RESP
   else
       RESP="$1"
   fi
   if [ "$RESP" = "2" -o "$RESP" = "4" -o "$RESP" = "5" -o "$RESP" = "6" ]
     then
         if [ "`tty`" != "/dev/console" ]
           then
               echo "\n\n\n\tYou must be on the system console to execute option '$RESP'!!!\c"
               sleep 3 
               continue  
         fi
   fi
   case "$RESP" in
      1)
        warnuser multi-user
        init 6
        ;;
      2)
        warnuser firmware 
        init 5 
        ;;
      3)
        warnuser powerdown 
        init 0 
        ;;
      4)
        warnuser single-user 
        if [ "$2" = "" ]
          then
              echo "\nNOTE: All daemons currently running will be killed; all file systems"
              echo "      must be re-mounted: /etc/mountall <RETURN>\n" 
              echo "Press <RETURN> ... \c"
              read RTN
        else
            :
        fi
        chkfortty
        init 1 
        exit
        ;;
      5)
        chkfortty
        echo "In order to perform this function, you will need to format a floppy disk"
        echo "first. Do you wish to proceed with the format/verify? y/n =>> \c"
        read FMT
        if [ "$FMT" = "y" -o "$FMT" = "Y" ]
          then
              /usr/local/ffmt y
        else
            echo "\nPlace a formatted floppy disk into the drive and press <RETURN> ... \c"
            read RTN
        fi
        warnuser floppykey
        chkfortty
        sysadm floppykey
        continue
        ;;
      6)
        /usr/local/diagdisk
        echo "\nPress <RETURN> ... \c"
        read RTN
        continue
        ;;
      7)
        chkfortty
        /usr/local/lastreboot
        echo "Press <RETURN> ... \c"
        read RTN
        continue
        ;;
    q|Q)
        exit 0
        ;;
      *)
        echo "\n\t\t\t\tInvalid response!\c"
        sleep 2
        continue
        ;;
   esac
 done
