# iAPX286 @(#)powerfail.sh	1.2
#
# Modified on: 03-23-87 by ACW - changed grace period from 6 to 3 minutes
#
# **** Insert powerfail commands here
if [ -f /etc/ac_off ]
then
	su - adm -c /etc/wall <<!
	Power is fluctuating. It is advisable to shut the system down manually.
!
else
	echo "ac_off" > /etc/ac_off
	for i in 2 1 0
	do
		if [ $i = 2 ]
		then
			su - adm -c /etc/wall <<!

			AC Power failure detected! 
			System will shutdown automatically in 
			$i minutes, unless power is restored.
!
		else
			if [ $i != 0 ]
			then
				su - adm -c /etc/wall <<!

			Power has not yet been restored.
			System still shutting down automatically
			in $i minutes, unless power is restored.
!
			fi
		fi
		sleep 60
		/etc/powerstat > /dev/null
		if [ $? -ne 0 ]
		then
			su - adm -c /etc/wall <<!

			AC Power has been restored, system shutdown aborted.
!
			rm -f /etc/ac_off
			exit 0
		fi
		if [ $i = 0 ]
		then
			rm -f /etc/ac_off
			/etc/shutdown -g0 -y
		fi
	done
fi
