#! /bin/sh
#
# @failscript@	This script is run when the UPS tells the system the power has
#		gone. Tell everybody and start the shutdown based on the failure
#		type.  This script will also being run when the power comes up
#		again.
#
#
# Author:	Tom Webster <webster@kaiwan.com>
# Modified-By:	Brian White <bcwhite@verisim.com>
#               Mitch Blevins <mblevin@debian.org>
#               Christopher Craig <ccraig@ccraig.org>



failtime=+5	# shutdown delay from initial power failure
scramtime=now	# shutdown delay from low-battery warning

failmsg="LINE POWER FAILURE -- SWITCHED TO BATTERY BACKUP"
scrammsg="BACKUP BATTERY LOW -- EMERGENCY SHUTDOWN"
okaymsg="LINE POWER RESTORED -- RESUMING NORMAL OPERATION"



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

# Set location of file containing PID of running shutdowns
spidpath="@shutdown@"



# See what happened.
case "$1" in

    start)
	# Called with a powerfail event, check to see if a shutdown is running
	if [ -f $spidpath ]
	then
	    # Shutdown is running, kill it to process the new event
	    shutdown -c >/dev/null 2>&1
	fi

	shutdown -h $failtime "$failmsg" &
	;;


    now)
	# Called with a powerfail event, check to see if a shutdown is running
	if [ -f $spidpath ]
	then
	    # Shutdown is running, kill it to process the new event
	    shutdown -c >/dev/null 2>&1
	fi

	shutdown -h $scramtime "$scrammsg" &
	;;


    stop)
	# Ok, power is good again. Say so on the console.
	if [ -f $spidpath ]
	then
	    # Only cancel if shutdown is running (system boot will call this)
	    shutdown -c "$okaymsg"
	fi
	;;


    *)
	echo "Usage: @initd@/@failscript@ {start|now|stop}" >&2
	exit 1
	;;

esac


exit 0
