#! /bin/sh
#
# powerfail	This script is run when the UPS tells the system
#		the power has gone. Tell everybody and halt the
#		system. In rc.halt we try to turn off the UPS or
#		we just sync sync and pray.
#
# Version:	@(#) /etc/rc.d/powerfail 1.60	10-Oct-1994
#

# Set the path.
PATH=/sbin:/usr/sbin:/bin:/usr/bin

# See what happened.
case "$1" in
    fail)
	# We are running on the UPS now, so tell everybody.
	wall << @EOF

	POWER OUTAGE, RUNNING ON UPS NOW!

@EOF
	# Don't allow users to login.
	echo "POWER FAILURE, RUNNING ON UPS" > /etc/nologin
	;;
    lowbat)
	# Tell everybody.
	wall << @EOF

	THE UPS BATTERY IS EMPTY! SHUTTING DOWN SYSTEM!

@EOF
	# A little hint to the "rc.halt" script.
	> /etc/power_is_failing
	# And goto the "halt" runlevel 0.
	init 0
	;;
    ok)
	# Ok, power is good again. Say so on the console.
	echo "THE POWER IS BACK, NOT RUNNING ON UPS ANYMORE"

	# Allow users to log in.
	rm -f /etc/nologin
	rm -f /etc/power_is_failing
	
	# See what runlevel we are in.
	set `runlevel`
	if [ $2 = 0 ]
	then
		# We are in runlevel 0, which means that the UPS
		# was never turned off. Go back to multi user mode.
		. /etc/rc.d/bcheckrc
		init default
	fi
	;;
  *)
	echo "Usage: /etc/rc.d/powerfail fail|lowbat|ok"
	exit 1
	;;
esac

exit 0
