:	Shutdown command
:
cd /
PATH=:/bin:/usr/bin
if test x$1 = x-s; then
	GOSINGLE=TRUE
	shift
fi
if test "$1" -gt 0 -a "$1" -lt 31; then
	COUNTDOWN=$1; shift
else
	COUNTDOWN=5
fi
MESS="$*"
set x `who am i </dev/tty`
if test "$2" != root -o "$3" != console; then
	echo shutdown: ERROR: Sorry, you must login as \'root\' on \'console\' to kill system.
	exit 1
fi
kill -3 1
while test "$COUNTDOWN" -gt 0; do
	HACKERS=`(who | while read USER TTY REST; do
		if test "$TTY" != console; then
			echo TRUE;
			exit
		fi
	done)`
	if test "$HACKERS" = TRUE; then
		echo 'shutdown: INFO: Waiting for following users to log off:

			USER	TTY'
		who | while read USER TTY REST; do
			if test "$TTY" != console; then
				echo \	\	\	$USER\	$TTY
			fi
		done
		echo
		echo "
		******************************************
		  $MESS
		  System SHUTDOWN in $COUNTDOWN minutes...       
		  Please clean up and log off.          
		******************************************" | /etc/wall
	else
		break
	fi
	for j in 15 15 15 15; do
		sleep $j
		HACKERS=`(who | while read USER TTY REST; do
			if test "$TTY" != console; then
				echo TRUE;
				exit;
			fi
		done)`
		if test "$HACKERS" = FALSE; then
			break 2 
		fi
	done
	COUNTDOWN="`expr $COUNTDOWN - 1`"
done
echo 'shutdown: INFO: All users logged off, doing cleanup'
TASKS=`(ps ax | while read UID TTY PID REST; do
	if test "$PID" -gt 1 -a "$TTY" != console; then
		echo $PID
	fi
done)`
if test "$TASKS"; then
	echo 'shutdown: INFO: Sending SIGHUP to following processes:'
	echo $TASKS
	kill -1 $TASKS >/dev/null 2>&1
	sleep 10
	TASKS=`(ps ax | while read UID TTY PID REST; do
		if test "$PID" -gt 1 -a "$TTY" != console; then
			echo $PID
		fi
	done)`
	if test "$TASKS"; then
		echo 'shutdown: INFO: Sending SIGTERM to following processes:'
		echo $TASKS
		kill $TASKS >/dev/null 2>&1
		sleep 5
		TASKS=`(ps ax | while read UID TTY PID REST; do
			if test "$PID" -gt 1 -a "$TTY" != console; then
				echo $PID
			fi
		done)`
		if test "$TASKS"; then
			echo 'shutdown: INFO: Sending SIGKILL to following processes:'
			echo $TASKS
			kill -9 $TASKS >/dev/null 2>&1
			sleep 5
		fi
	fi
fi
if test -f /usr/lib/net/tcboot; then
	/usr/lib/net/tcboot -S -d /dev/tc0
fi
if test -f /etc/accton; then
	/etc/accton
fi
sync
/etc/mount | sort -r +8 | while read x1 PID x3 x4 x5 DEV x7 FS REST; do
	if test "$PID" != 0 -o "$FS" = "/"; then
		continue
	fi
	DEV="/dev/$DEV"
	if /etc/umount $DEV; then
		echo shutdown: INFO: File system $FS on device $DEV removed
	else
		echo shutdown: ERROR: Unable to remove file system $FS on device $DEV
	fi
done
sleep 5
if test $GOSINGLE; then
	echo 'shutdown: INFO: Cleanup done, system will enter SINGLE USER mode...'
	kill -1 1
else
	echo 'shutdown: INFO: Cleanup done, system will HALT!'
	kill 1
fi
sleep 100
