#!/bin/sh
#
# Author Preston Landers <planders@mail.utexas.edu>
# Support for /etc/time_remaining by Stanislav Voronyi.
#

if [ -r /etc/time_remaining ]
then
	ttl=`cat /etc/time_remaining`
	if [ $ttl -eq 0 ]
		then
			ttl=39; #if giving autonomy not supported by UPS
					# default to 30 minutes before shutdown
	fi
fi

case $1 in
start)
    touch /etc/power_is_failing
    /sbin/shutdown -h $((ttl-9))m "*** DON'T PANIC! LOSING POWER RAPIDLY ***"  &
;;
stop)
    rm -f /etc/power_is_failing
    /sbin/shutdown -c "*** POWER HAS RETURNED, EVERYTHING A.O.K. ***"
;;
now)
    touch /etc/power_is_failing
    if [ -f /var/run/shutdown.pid ]; then
        /sbin/shutdown -c
    fi
    /sbin/shutdown -h now "*** POWER NEARLY DRAINED, SHUTTING DOWN NOW ***"

;;
*)
        echo Usage: $0 start\|stop\|now
;;
esac
exit 0

