#!/bin/sh
#
# Copyright (C) 1999-2000 Riccardo Facchetti <riccardo@master.oasi.gpa.it>
#
# Generated automatically from apccontrol.sh.in by configure.
#
# Debian modifications by G. Morehouse <spambox@evernex.com> April 2001

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

APCPID=/var/run/apcupsd.pid
APCUPSD=/sbin/apcupsd
SHUTDOWN=/sbin/shutdown
SCRIPTSHELL=/bin/sh
SCRIPTDIR=/etc/apcupsd

#
# 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

case "$1" in
        killpower)
                echo "Apccontrol doing: ${APCUPSD} --killpower"       
                ${APCUPSD} --killpower
        ;;
        commfailure)
                printf "apcupsd: Warning: Serial communications with UPS lost.\n" | wall
        ;;
        commok)
                printf "apcupsd: Notice: Serial communications with UPS restored.\n" | wall
        ;;
        powerout)
                printf "apcupsd: Warning: Power loss detected.\n" | wall
        ;;
        onbattery)
                printf "apcupsd: Warning: Running on UPS batteries.\n" | wall
        ;;
        failing)
                printf "apcupsd: ALERT: UPS battery power exhausted.  Attempting shutdown...\n" | wall
        ;;
        timeout)
                printf "apcupsd: ALERT: UPS runtime limit exceeded.  Attempting shutdown...\n" | wall
        ;;
        loadlimit)
                printf "apcupsd: ALERT: UPS battery discharge limit reached.  Attempting shutdown...\n" | wall
        ;;
        runlimit)
                printf "apcupsd: ALERT: UPS battery runtime percentage reached.  Attempting shutdown...\n" | wall
        ;;
        doreboot)
                printf "apcupsd: ALERT: Beginning reboot sequence..." | wall
                ${SHUTDOWN} -r now "apcupsd-initiated reboot"
        ;;
        doshutdown)
                printf "apcupsd: ALERT: Beginning shutdown sequence..." | wall
                ${SHUTDOWN} -h now "apcupsd-initiated shutdown"
        ;;
        mainsback)
                printf "apcupsd: Notice: Power has been restored." | wall
                if [ -f /etc/apcupsd/powerfail ] ; then
                   printf "apcupsd: Notice: Attempting to cancel shutdown..." | wall
                   ${SHUTDOWN} -c
                   ${SHUTDOWN} -r now "apcupsd initiated reboot"
                fi
        ;;
        annoyme)
                printf "apcupsd: Warning: Power failure detected. Please save work and log off." | wall
        ;;
        emergency)
                printf "apcupsd: ALERT: Emergency Shutdown. Possible UPS battery failure." | wall
                ${SHUTDOWN} -h now "apcupsd emergency shutdown"
        ;;
        changeme)
                printf "apcupsd: ALERT: UPS batteries have failed.  Change them NOW!" | wall
        ;;
        remotedown)
                printf "apcupsd: ALERT: Remote shutdown. Beginning shutdown sequence..." | wall
                ${SHUTDOWN} -h now "apcupsd remote shutdown"
        ;;
        restartme)
                echo -n "Restarting APCUPSD Power Management: "
                THEPID=`cat ${APCPID}`
                kill ${THEPID}
                rm -f ${APCPID}
                rm -f /etc/apcupsd/powerfail
                rm -f /etc/nologin
                sleep 5
                `${APCUPSD}`
                echo "apcupsd."
        ;;
        startselftest)
        ;;
        endselftest)
        ;;
        *)      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
