#! /bin/sh
#
# genpower	This script is used to start the UPS management daemon.	 When
#		called with "poweroff", this script will attempt to shutdown
#		the UPS in order to preserve the battery.  This should only
#		be done when going to run-level 0 ("system halt").
#
# Version:	/etc/init.d/genpower
#
# Author:	Brian White <bcwhite@pobox.com>
#

enabled=0
upsport=/dev/ups
upstype=apc-pnp

genpowerd=/sbin/genpowerd
genwrite=/sbin/genwrite
PATH=/usr/bin:/bin:/usr/sbin:/sbin


if [ ! -x $genpowerd ]
then
    exit 0
fi

if [ "$enabled" = 0 ]
then
    echo "/etc/init.d/genpower: UPS monitor daemon not enabled"
    exit 0
fi


function setdumbsignal {
    if [ "$1" = "apc-pnp" ]; then
	echo -n "(switch to dumb-signalling mode) "
	$genwrite -d $upsport "R"
    fi
}


case "$1" in

    start)
	echo -n "Starting UPS monitor: genpowerd "
	killall genpowerd >/dev/null 2>&1
#	setdumbsignal $upstype
	$genpowerd $upsport $upstype
	echo "."
	;;

    stop)
	echo -n "Stopping UPS monitor: genpowerd "
	killall genpowerd >/dev/null 2>&1
	echo "."
	;;

    poweroff)
	echo "Sending power-down signal to the UPS..."
	$genpowerd -k $upsport $upstype >/dev/null 2>&1
	;;

    *)
	echo "Usage: /etc/init.d/genpower {start|stop|poweroff}"
	exit 1
	;;

esac


exit 0
