#!/usr/bin/sh
#
# Copyright (C) 1999-2002 Riccardo Facchetti <riccardo@master.oasi.gpa.it>
#
#  for apcupsd release 3.14.10 (13 September 2011) - suse
#
# platforms/apccontrol.  Generated from apccontrol.in by configure.
#
#  Note, this is a generic file that can be used by most
#   systems. If a particular system needs to have something
#   special, start with this file, and put a copy in the
#   platform subdirectory.
#

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

APCPID=/var/run/apcupsd.pid
APCUPSD=/usr/sbin/apcupsd
HIBERNATE=/usr/sbin/pm-hibernate
SHUTDOWN=/sbin/shutdown
SCRIPTSHELL=/usr/bin/sh
SCRIPTDIR=/etc/apcupsd
WALL=wall
NOTIFY=/usr/lib/powersave/powersave-notify

#
# Concatenate all output from this script to the events file
#  Note, the following kills the script in a power fail situation
#   where the disks are mounted read-only.
# exec >>/var/log/apcupsd.events 2>&1

#
# This piece is to substitute the default behaviour with your own script,
# perl, or C program.
# You can customize every single command creating an executable file (may be a
# script or a compiled program) and calling it the same as the $1 parameter
# passed by apcupsd to this script.
#
# After executing your script, apccontrol continues with the default action.
# If you do not want apccontrol to continue, exit your script with exit 
# code 99. E.g. "exit 99".
#
# WARNING: the apccontrol file will be overwritten every time you update your
# apcupsd, doing `make install'. Your own customized scripts will _not_ be
# overwritten. If you wish to make changes to this file (discouraged), you
# should change apccontrol.sh.in and then rerun the configure process.
#
if [ -f ${SCRIPTDIR}/${1} -a -x ${SCRIPTDIR}/${1} ]
then
    ${SCRIPTDIR}/${1} ${2} ${3} ${4}
    # exit code 99 means he does not want us to do default action
    if [ $? = 99 ] ; then
	exit 0
    fi
fi

function notify() {
    ${NOTIFY} "$1"
    echo "$1" | ${WALL}
}

case "$1" in
    killpower)
	notify "Apccontrol doing: ${APCUPSD} --killpower on UPS ${2}"
	sleep 10
	${APCUPSD} --killpower
	notify "Apccontrol has done: ${APCUPSD} --killpower on UPS ${2}"
    ;;
    commfailure)
	notify "Warning communications lost with UPS ${2}"
    ;;
    commok)
	notify "Communications restored with UPS ${2}"
    ;;
#
# powerout, onbattery, offbattery, mainsback events occur
#   in that order.
#
    powerout)
    ;;
    onbattery)
	notify "Power failure on UPS ${2}. Running on batteries."
    ;;
    offbattery)
	notify "Power has returned on UPS ${2}..."
    ;;
    mainsback)
	if [ -f /etc/apcupsd/powerfail ] ; then
	   notify "Continuing with shutdown."
	fi
    ;;
    failing)
	notify "Battery power exhaused on UPS ${2}. Doing shutdown."
    ;;
    timeout)
	notify "Battery time limit exceeded on UPS ${2}. Doing shutdown."
    ;;
    loadlimit)
	notify "Remaining battery charge below limit on UPS ${2}. Doing shutdown."
    ;;
    runlimit)
	notify "Remaining battery runtime below limit on UPS ${2}. Doing shutdown."
    ;;
    doreboot)
	notify "UPS ${2} initiating Reboot Sequence"
	${SHUTDOWN} -r now "apcupsd UPS ${2} initiated reboot"
    ;;
    doshutdown)
	. /etc/sysconfig/apcupsd
	if test "$APCUPSD_POWERFAIL_BEHAVIOR" = "hibernate" ; then
		notify "UPS ${2} initiated hibernation"
		$HIBERNATE
	else
		notify "UPS ${2} initiated Shutdown Sequence"
		${SHUTDOWN} -h now "apcupsd UPS ${2} initiated shutdown"
	fi
    ;;
    annoyme)
	notify "Power problems with UPS ${2}. Please logoff."
    ;;
    emergency)
	notify "Emergency Shutdown. Possible battery failure on UPS ${2}."
    ;;
    changeme)
	notify "Emergency! Batteries have failed on UPS ${2}. Change them NOW"
    ;;
    remotedown)
	notify "Remote Shutdown. Beginning Shutdown Sequence."
    ;;
    startselftest)
    ;;
    endselftest)
    ;;
    battdetach)
    ;;
    battattach)
    ;;
    *)	echo "Usage: ${0##*/} command"
	echo "	     warning: this script is intended to be launched by"
	echo "	     apcupsd and should never be launched by users."
	exit 1
    ;;
esac
