#!/bin/sh
#
# Copyright (C) 1996-99 Andre M. Hedrick
# Copyright (C) 1999 Riccardo Facchetti <fizban@tin.it>
#
# Generated automatically from powersc.in by configure.
#

#
# These variables are needed for set up the autoconf other variables.
#
prefix=/usr
exec_prefix=${prefix}

POWERPID=/var/run/power.pid
APCPID=/var/run/apcupsd.pid
APCUPSD=${exec_prefix}/sbin/apcupsd
SHUTDOWN=/sbin/shutdown
WALL=/usr/bin/wall

if test "$(whoami)" != "root"; then
	printf "Sorry, you must be root to run this script." | $WALL
	exit 1
fi

case "$1" in
	POWEROUT)
		printf "Warning!  There are power problems." | $WALL
		;;
	ONBATTERY)
		printf "Power Failure! Running on UPS." | $WALL
		;;
	FAILING)
		printf "Battery Power Failed -- Normal Shutdown.\n" | $WALL
		;;
	TIMEOUT)
		printf "On-line battery timeout -- Normal Shutdown.\n" | $WALL
		;;
	LOADLIMIT)
		printf "Battery load limits reached -- Normal Shutdown.\n" | $WALL
		;;
	RUNLIMIT)
		printf "Battery run time limits reached -- Normal Shutdown.\n" | $WALL
		;;
	DOSHUTDOWN)
		printf "Beginning Shutdown Sequence!" | $WALL
		ps x | gawk '{ if (($5 == "init") && ($1 == "1")) print $6 }' \
			| cut -f2 -d[ | cut -f1 -d] \
			> /tmp/run.level.power
		if [ -f $POWERPID ]
		then
			exit 0
		else
			echo $$ > $POWERPID
			$SHUTDOWN -h now
		fi
		;;
	MAINSBACK | WAITASEC)
		printf "Power has returned." | $WALL
		if [ -f $POWERPID ]; then
			printf "Attempting to cancel shutdown." | $WALL
			kill $(cat $POWERPID)
			rm -f $POWERPID
			init $(cat /tmp/run.level.power)
		fi
		;;
	ANNOYME)
		printf "Power Problems!  Please Log Off Now!" | $WALL
		;;
	EMERGENCY)
		printf "Possible battery failure -- Emergency Shutdown!" | $WALL
		$SHUTDOWN -h now
		;;
	CHANGEME)
		printf "Emergency -- Batteries Have Failed!\nChange Them NOW!" | $WALL
		;;
	REMOTE)
		printf "Remote Calls -- Normal Shutdown.\nBeginning Shutdown Sequence!" | $WALL
		$SHUTDOWN -h now
		;;
	KILL)	sleep 1
		$APCUPSD killpower
		sleep 10
		;;
	INIT)	rm -f $POWERPID
		rm -f /etc/powerfail
		rm -f /etc/powerstatus
		rm -f /etc/nologin
                [ "$2" = "" ] && echo -n "Starting APCUPSD Power Management: "
		$APCUPSD
		[ "$2" = "" ] && echo "apcupsd"
		;;
	STOP)
		[ "$2" = "" ] && echo -n "Stopping APCUPSD Power Management: "
		if [ -f $APCPID ]; then
			kill $(cat $APCPID)
			rm -f $APCPID
		fi
		[ "$2" = "" ] && echo "apcupsd"
		;;
	RESTARTME)
		[ "$2" = "" ] && echo -n "Restarting APCUPSD Power Management: "
		kill $(cat $APCPID)
		rm -f $APCPID
		rm -f $POWERPID
		rm -f /etc/powerfail
		rm -f /etc/powerstatus
		rm -f /etc/nologin
		sleep 10
		$APCUPSD
		[ "$2" = "" ] && echo "apcupsd"
		;;
	CONFIG)
		echo "Updating Configuration Settings."
		$APCUPSD -c
		;;
	NAME)
		echo "Changing UPS Name"
		$APCUPSD -n
		;;
	BATTERY)
		echo "Changing Battery Replacement Date"
		$APCUPSD -b
		;;
	*)	echo "Usage:   ${0##*/} [POWEROUT | ONBATTERY | FAILING | TIMEOUT | LOADLIMIT | RUNLIMIT | DOSHUTDOWN | MAINSBACK | WAITASEC | ANNOYME | EMERGENCY | CHANGEME | REMOTE | KILL | INIT | STOP | RESTARTME | CONFIG | NAME | BATTERY]"
		exit 1
		;;
esac
exit 0
