#!/bin/sh
#
# This shell script if placed in /etc/apcupsd
# will be called by /etc/apcupsd/apccontrol via
# symlinks generated by makesymlinks script
# when the UPS goes on batteries.
# We send an email message to root to notify him.
#

SYSADMIN=root
MAIL="mail"

HOSTNAME=`hostname`
COLOR=""

NAME=`echo $0 | sed 's|^.*/||'`

case $NAME in
    onbattery)
        TIMELEFT=`/sbin/apcaccess|grep TIMELEFT`
        MSG="$HOSTNAME Power Failure !!! "$TIMELEFT
        ;;
    offbattery)
        MSG="$HOSTNAME Power has returned"
        ;;
    commok)
        MSG="$HOSTNAME Serial port communications with UPS restored"
        ;;
    commfailure)
        MSG="$HOSTNAME Serial port communications with UPS lost"
        ;;
    changeme)
        MSG="$HOSTNAME UPS battery needs changing NOW."
        ;;
    masterconnect)
        MSG="slave $HOSTNAME received connection from master"
        ;;
    mastertimeout)
        MSG="slave $HOSTNAME lost contact with master"
        ;;
    upsdmessages)
        TIMELEFT=`/sbin/apcaccess|grep TIMELEFT`
        MSG="$HOSTNAME test upsdmessages. "$TIMELEFT
        ;;
    *)
        TIMELEFT=`/sbin/apcaccess|grep TIMELEFT`
        MSG="$HOSTNAME unknown excecute upsdmessages. "$TIMELEFT
esac

MSG=${MSG}" "`date`

#
(
   echo "$MSG"
   echo " "
   /sbin/apcaccess status
) | $MAIL -s "$MSG" $SYSADMIN

# additional actions can be placed to /etc/sysconfig/apcupsdmessages
[ -f /etc/sysconfig/apcupsdmessages ] && . /etc/sysconfig/apcupsdmessages

exit 0
