:
#
#	%Z% %M% %I% %D% %Q%
#
#	Copyright (C) Microsoft Corporation, 1983
#
#	This Module contains Proprietary Information of Microsoft
#	Corporation and AT&T, and should be treated as Confidential.
#
#***	shutdown -- bring the system down gracefully and ruthlessly
#
#	arw 3/22/81
#
#	shutdown [delay] [su]
#
#	Bring the system down gracefully and ruthlessly.
#	1.  Warn users.
#	2.  Kill off any user or daemon tasks.
#	3.  Dismount disks.
#	4.  sync and halt processor, or go single user
#
#	SIGINT		causes things to go faster by skipping a minute.
#	SIGQUIT		terminates the program.  Be careful if it has
#			already started to kill off things.
#
#	MODIFICATION HISTORY
#	M000	26 Oct 83	andyp	3.0 upgrade
#	- ps ax ==> ps -e
#	- /etc/mount ==> /etc/mount | fgrep -v root
#	- /etc/mount /dev/$i ==> /etc/mount $i
#
#	I000	October 1984	Vince Slyngstad
#		1. Parameters are now checked for vailidity
#		2. Wait for normal shutdown message not printed
#		   for shutdown su
#		3. Default to 5 minutes implemented to match man page
#	I001	March 1986	Ken Masden
#		1. do nfs off/abort, nfc off/abort, and vts off/abort so
#		   shutdown does not hang if network active.
#		2. do write all (wall) to users on local node only.
#       I002    August 1986     George L. Lundrigan
#               1. Change message displayed if not at console when
#                  using the 'shutdown' login.
#	I003	August 1986	Ed Reeder
#		1. Warn user if "pending transactions" exist
#		2. Warn user if "Building units of work"


PATH=/bin:/usr/bin
cd /
if expr "`who am i`" : "shutdown *console.*" >/dev/null
then	: okay
else	echo "
You must login at the console to shut down this system."
	exit 1
fi

num=''
case $# in
  0) ;;
  1) case $1 in
       [0-9]|[0-9][0-9]) ;;
       su)               ;;
       *)                echo 'Invalid argument: "'$1'"'
                         exit 1 ;;
     esac ;;
  2) case $1 in
       [0-9]|[0-9][0-9]) ;;
       *)                echo 'Invalid argument: "'$1'"'
                         exit 1 ;;
     esac
     case $2 in
       su) ;;
       *)  echo 'Invalid argument: "'$2'"'
           exit 1 ;;
     esac ;;
  *) echo "usage: shutdown [delay] [su]"
     exit 1 ;;
esac
case $1 in
  [0-9]|[0-9][0-9]) num=$1
                    shift ;;
esac
case $num in
  '') echo -n "Minutes till shutdown? (default 5):  "
      read num
      case $num in
        [0-9]|[0-9][0-9]) ;;
        '')               num=5 ;;
        *)                echo 'Invalid argument: "'$num'"'
                          exit 1 ;;
     esac
esac
if [ $num -gt 15 ]
then	echo "Delay too long (15 minutes max)"
	exit 1
fi
num=`expr $num + 0`

# I002 vvvvv
# check if "pending transactions exist"
if [ -f /usr/tmp/*.idisP ] ; then
   echo "
There are iDIS \"Pending Transactions\" in existence.  These transactions are
for such actions as adding users, modifying network information, etc.

If you bring the system down at this time, the information will be lost,
necessitating corrective action by the system administrator.

The transactions were created by the following login ID(s):
"
   (cd /usr/tmp; ls *.idisP) | sed -e 's/^\([^.]*\).idisP/\1/'
echo "
It is suggested that you have the login ID(s) schedule their transactions
prior to your shutting down the system.  If this is done the information
will not be lost."

   while :
   do
      echo -n "
Do you want to go ahead with the shutdown?  If you answer 'y' the information
will be lost.  If you answer 'n' the information can be saved.
Enter (y or n): "
      read resp
      case $resp in
	 y*|Y*) echo "Continuing with the shutdown"
		break ;;
	 n*|N*) echo "Shutdown canceled"
		exit 0 ;;
	 *)	echo "Invalid response <$resp>, please respecify" ;;
      esac
   done
fi

# if this is a standalone system or the network master node,
# make sure that we aren't "Building Units of Work"

iDIS=/usr/iDIS		# HARD-CODED PATHNAME ! ! ! !

# see if can access with //$PHYSYS, if so OpenNET is installed
PHYSYS=`cat /etc/systemid`
if [ -f //$PHYSYS/etc/systemid ] ; then
   MASNODE=`sed -n -e 's/^\/\/\([^/]*\)\/etc\/passwd$/\1/p' /net/self`
   MASNODEP=//$MASNODE
   # if isn't the master node we don't have any net synch files here
   case $PHYSYS in
      $MASNODE) MASTER=y ;;
      *)	MASTER=n ;;
   esac
else
   MASNODE=$PHYSYS
   MASNODEP=
   MASTER=y
fi

# If the net synch files are on this node, see what is going on
if [ "$MASTER" = "y" ] ; then
   MASSYNCH=$MASNODEP/$iDIS/synch
   cd $MASSYNCH
   
   # see if we are "Building Units of Work"
   if [ -f .Sched.Batch/BatchNumber -o -f *.Batch/BatchNumber ] ; then
   echo "
This system is currently in the middle of building iDIS transactions.  These
transactions are for such actions as adding users, modifying network
information, etc.

If you bring the system down at this time, the information will be lost,
necessitating corrective action by the system administrator.

It is suggested that you wait until this critical activity is completed (a
few minutes)."

   while :
   do
      echo -n "
Do you want to go ahead with the shutdown?  If you answer 'y' the information
will be lost.  If you answer 'n' the information can be saved.
Enter (y or n): "
      read resp
      case $resp in
	 y*|Y*) echo "Continuing with the shutdown"
		break ;;
	 n*|N*) echo "Shutdown canceled"
		exit 0 ;;
	 *)	echo "Invalid response <$resp>, please respecify" ;;
      esac
   done
   fi
fi
cd /
# I002 ^^^^^
# I001 vvvvv
if [ -f /etc/nfs ]
  then
    /etc/nfc off &
    sleep 5
    /etc/nfs off &
    sleep 5
fi
if [ -f /etc/vts ]
  then
    /etc/vts off &
    sleep 5
fi
if [ -f /etc/vtc ]
  then
    /etc/vtc off &
    sleep 5
fi
# I001 ^^^^^

trap "continue" 2
while [ $num -ne 0 ]
do
  num=`expr $num - 1`
  if expr $num \< 5 \| $num % 5 == 0 >/dev/null;
    then
      # I001 vvvvv
      if [ -f /net/self ]
        then
          # XENIX OpenNET present
          lnodename=`net lname`
          echo "XENIX Shutdown in `expr $num + 1` minutes.\nClean up and log off." | /etc/wall -n $lnodename &
        else
          echo "XENIX Shutdown in `expr $num + 1` minutes.\nClean up and log off." | /etc/wall &
      fi
      # I001 ^^^^^
  fi
  users=`who | fgrep -v " console"`
  if [ -z "$users" ]
  then
    echo "All logged off now."
    break
  else
    echo "$users"
  fi
  sleep 60
  if [ $num -le 2 ]
  then
    kill -3 1
  fi   
done

trap 2
kill -3 1
if [ ! "$1" ]
then
	echo '\nPlease wait for the "**  Normal System Shutdown  **" message.\n'
fi
# I001 vvvvv
if [ -f /net/self ]
  then
    #   XENIX OpenNET installed
    lnodename=`net lname`
    echo "XENIX will now terminate." | /etc/wall -n $lnodename
  else
    echo "XENIX will now terminate." | /etc/wall
fi
if [ -f /etc/nfs ]
  then
    /etc/nfc abort &
    sleep 5
    /etc/nfs abort &
    sleep 5
fi
if [ -f /etc/vts ]
  then
    /etc/vts abort &
    sleep 5
fi
if [ -f /etc/vtc ]
  then
    /etc/vtc abort &
    sleep 5
fi
# I001 ^^^^^
for sig in -15 -9
do
  pids=`ps -e | tail +4 | grep -v "^.......co" | sed -e 's/ *\([0-9]*\) .*/\1/'`
  if [ ! -z "$pids" ]
  then
    echo kill $sig $pids
    kill $sig $pids
    sleep 5
  fi
done

if [ ! -z "$pids" ]
then
  ps -e
fi

if [ -f /etc/accton ]
then
  /etc/accton
fi

sync
mnt=`/etc/mount | fgrep -v root | sort +2r | sed -e "s/ on .*//"`
for i in $mnt
do
  /etc/umount $i
done

sync
if [ ! -z "$mnt" ]
then
  /etc/mount
  sync
fi

if test $1; then
	kill -1 1
else
	sync
	sync
	/etc/haltsys
	echo "Haltsys failed! going single user ...."
	kill -1 1
fi

