#	Copyright (c) 1984, 1987 AT&T
#	  All Rights Reserved

#	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
#	The copyright notice above does not evidence any
#	actual or intended publication of such source code.

#ident	"@(#)shutdown.sh	1.9"

#	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. (/etc/rc1 for state S)

#	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=0
while [ $# -gt 0 ]
do
	case $1 in
	-g[0-9]* )
		grace=`expr "$1" : '-g\(.*\)'`
		;;
	-i[Ss0156] )
		initstate=`expr "$1" : '-i\(.*\)'`
		;;
	-i[234] )
		initstate=`expr "$1" : '-i\(.*\)'`
		echo "$0:  Initstate $initstate 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 '
Shutdown started.    \c'
date
echo

sync
cd /

trap "exit 1"  1 2 15

a="`who  |  wc -l`"
if [ ${a} -gt 1  -a  ${grace} -gt 0 ]
then
	su adm -c /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.

!
sleep ${grace}

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/rc1
esac
/etc/init ${initstate}

