#!/bin/sh
#
# This shell script if placed in /etc/apcupsd-multiups
# will be called by /etc/apcupsd-multiups/apccontrol via
# via some symlinks when some events occur.
#
# Script send an email message to root to notify us.
#

SYSADMIN="root"
MAIL="mail"

HOSTNAME=`hostname`
COLOR=""
ISTEST=

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

case $NAME in
    onbattery)
        MSG="Server: \"${HOSTNAME}\", UPS: \"${1}\", event: Power Failure !!!"
        ;;
    offbattery)
        MSG="Server: \"${HOSTNAME}\", UPS: \"${1}\", event: Power has returned."
        ;;
    commok)
        MSG="Server: \"${HOSTNAME}\", UPS: \"${1}\", event: Serial port communications with UPS restored."
        ;;
    commfailure)
        MSG="Server: \"${HOSTNAME}\", UPS: \"${1}\", event: Serial port communications with UPS lost."
        ;;
    changeme)
        MSG="Server: \"${HOSTNAME}\", UPS: \"${1}\", event: UPS battery needs changing NOW."
        ;;
    masterconnect)
        MSG="slave Server: \"${HOSTNAME}\", UPS: \"${1}\", event: received connection from master"
        ;;
    mastertimeout)
        MSG="slave Server: \"${HOSTNAME}\", UPS: \"${1}\", event: lost contact with master"
        ;;
    upsdmessages)
        MSG="Server: \"${HOSTNAME}\", event: test upsdmessages."
        ISTEST="YES"
        ;;
    *)
        MSG="Server: \"${HOSTNAME}\", event: unknown excecute upsdmessages."
        ISTEST="UNKNOWN"
        ;;
esac

[ "x${ISTEST}" = "x" ] && NISPORT=`egrep "^NISPORT" /etc/apcupsd-multiups/apcupsd.${1}.conf | sed "s/.* //"`

#
(
   echo "$MSG"
   echo " "
   [ "x${ISTEST}" = "x" ] && /sbin/apcaccess status localhost:$NISPORT
) | $MAIL -s "$MSG" $SYSADMIN

exit 0
