:
#	      UNIX is a registered trademark of AT&T
#		Portions Copyright 1976-1989 AT&T
#	Portions Copyright 1980-1989 Microsoft Corporation
#    Portions Copyright 1983-1989 The Santa Cruz Operation, Inc
#		      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.


#	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.



#	Usage:  
#	shutdown [-y] [-g[hh:]mm] [-i0156sS] [-f"MESG"] [-fFILE] [su]
#
#	The is no need to run shutdown from the console and the root directory

usage() {
	echo "Usage : shutdown [-y] [-g[hh:]mm] [-i0156sS] [-f\"MESG\"] [-fFILE] [su]"
}


#	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."
		usage
		exit 2
	fi
fi


grace=60
askconfirmation=yes
hour=0
minute=0
su=
initstate=0
file=
mesg=
while [ $# -gt 0 ]
do
	case $1 in
	-g* )
		period=`expr "$1" : '-g\(.*\)'`
		case $period in
		[0-9]:*|[0-9][0-9]:*)
			hour=`expr "$period" : "\([0-9]\{1,2\}\):.*"`
			[ $hour -ge 0 -a $hour -le 72 ] || {
				echo "Grace period should be <= 72 hours."
				usage
				exit 1
			}
			minute=`expr "$period" : ".*:\([0-9]\{1,2\}\)"`
			[ $minute ] || minute=0
			;;
		[0-9]|[0-9][0-9]|:[0-9]|:[0-9][0-9])
			hour=0
			minute=`expr "$period" : "[:]\{0,1\}\(.*\)"`
			;;
		*)
			echo "Please enter graced period in form of hh:mm where hh is hours and mm is minutes."
			usage
			exit 1
			;;
		esac
			[ $minute -ge 0 -a $minute -le 59 ] || {
				echo "Value for minutes out of range (0-59)"
				usage
				exit 1
			}
			grace=`expr $hour \* 3600 + $minute \* 60`
		;;
	-i[Ss0156] )
		initstate=`expr "$1" : '-i\(.*\)'`
		;;
	-i[234] )
		initstate=`expr "$1" : '-i\(.*\)'`
		echo "$0:  Initstate $initstate is not for system shutdown"
		usage
		exit 1
		;;
	-y )
		askconfirmation=
		;;
	su )
		initstate=1
		;;
	-f* )
		mesg=`expr "$1" : "-f\(.*\)"`
		[ -f "$mesg" ] && file=yes
		;;
	-* )
		echo "Illegal flag argument '$1'"
		usage
		exit 1
		;;
	* )
		usage
		exit 1
		;;
	esac
	shift
done


while [ "$grace" -gt 60 ]
do
	left="`who | wc -l`"
	if [ $left -gt 1 ]
	then
		if [ $file ]
		then
			cat $mesg | /etc/wall
		elif [ -n "$mesg" ]
		then
			echo $mesg | /etc/wall
		else
			/etc/wall<<-!
				The system will be shut down in $hour hours and $minute minutes. Please log off before shut-down.
				!
		fi
	fi
	if [ $hour -gt 0 ]
	then
		hour=`expr $hour - 1`
		grace=`expr $grace - 3600`
		sleep 3600
	else
		if [ $minute -ge 15 ]
		then
			minute=`expr $minute - 15`
			grace=`expr $grace - 900`
			sleep 900
		elif [ $minute -gt 0 ]
		then
			minute=`expr $minute - 1`
			grace=`expr $grace - 60`
			sleep 60
		fi
	fi
done


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
	if [ $file ]
	then
		cat $mesg | /etc/wall
	elif [ -n "$mesg" ]
	then
		echo $mesg | /etc/wall
	else
		/etc/wall<<-!
			The system will be shut down in ${grace} seconds.
			Please log off now.

		!
	fi
	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
/etc/init ${initstate}
echo "Shutdown proceeding. Please wait .............."
sleep 60

