#! /bin/sh
#
# |-----------------------------------------------------------|
# | Copyright (c) 1991, 1990 MIPS Computer Systems, Inc.      |
# | All Rights Reserved                                       |
# |-----------------------------------------------------------|
# |          Restricted Rights Legend                         |
# | Use, duplication, or disclosure by the Government is      |
# | subject to restrictions as set forth in                   |
# | subparagraph (c)(1)(ii) of the Rights in Technical        |
# | Data and Computer Software Clause of DFARS 252.227-7013.  |
# |         MIPS Computer Systems, Inc.                       |
# |         950 DeGuigne Avenue                               |
# |         Sunnyvale, California 94088-3650, USA             |
# |-----------------------------------------------------------|
#
# $Header: shutdown,v 1.12.1.1 92/06/24 13:35:51 marker Exp $

#	Sequence performed to change the init stat of a machine.

#	This procedure checks to see if you are permitted and allows an
#	interactive shutdown.  The actual change of state, killing of
#	processes and such are performed by the new init state, say 0,
#	and its /etc/rc0.

#	Usage:  shutdown [ -y ] [ -g<grace-period> ] [ -i<init-state> ]

#!	chmod +x ${file}

if [ `pwd` != / ]
then
	echo "$0:  You must be in the / directory to run /etc/shutdown."
	exit 1
fi

#	Check the user id.
if [ -x /usr/bin/id ]
then
	eval `id  |  sed 's/[^a-z0-9=].*//'`
	if [ "${uid:=0}" -ne 0 ]
	then
	        echo "$0:  Only root can run /etc/shutdown."
		exit 2
	fi
fi

grace=60
askconfirmation=yes
initstate=s
while [ $# -gt 0 ]
do
	case $1 in
	-g[0-9]* )
		grace=`expr "$1" : '-g\([0-9]*\)$'`
		case "$grace" in
			"")
				echo "$0:  Grace must be a positive integer"
				exit 1
				;;
		esac
		;;
	-i[Ss0156] )
		initstate=`expr "$1" : '-i\(.*\)'`
		;;
	-i[234] )
		initstate=`expr "$1" : '-i\(.*\)'`
		echo "$0:  Initstate $i is not for system shutdown"
		exit 1
		;;
	-y )
		askconfirmation=
		;;
	-* )
		echo "Illegal flag argument '$1'"
		exit 1
		;;
	* )
		echo "Usage:  $0 [ -y ] [ -g<grace> ] [ -i<initstate> ]"
		exit 1
	esac
	shift
done

if [ -n "${askconfirmation}" -a -x /etc/ckbupscd ]
	then
	#	Check to see if backups are scheduled at this time
	BUPS=`/etc/ckbupscd`
	if [ "$BUPS" != "" ]
		then
		echo "$BUPS"
		echo "Do you wish to abort this shutdown and return to 
command level to do these backups? [y, n] \c"
		read YORN
		if [ "$YORN" = "y" -o "$YORN" = "Y" ]
			then 
			exit 1
		fi
	fi
fi

if [ -z "${TZ}"  -a  -r /etc/TIMEZONE ]
then
	. /etc/TIMEZONE
fi

echo '\nShutdown started.     \c'
date
echo

sync
cd /

trap "exit 1"  1 2 15

a=`who | sed '1d'`
if [ "$a" != ""  -a  ${grace} -gt 0 ]
then
	/etc/wall<<-!
		The system will be shut down in ${grace} seconds.
		Please log off now.

	!
	sleep ${grace}
fi

/etc/wall <<-!
	THE SYSTEM IS BEING SHUT DOWN NOW ! ! !
	Log off now or risk your files being damaged.

!
if [ ${grace} -ne 0 ]
then
	sleep 60
fi

if [ ${askconfirmation} ]
then
	echo "Do you want to continue? (y or n):   \c"
	read b
else
	b=y
fi
if [ "$b" != "y" ]
then
	/etc/wall <<-\!
		False Alarm:  The system will not be brought down.
	!
	echo 'Shut down aborted.'
	exit 1
fi
case "${initstate}" in
s | S )
	. /etc/rc0
esac
/etc/init ${initstate}
