#! /bin/sh
# skeleton	example file to build /etc/init.d/ scripts.
#		This file should be used to construct scripts for /etc/init.d.
#
#		Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#		Modified for Debian GNU/Linux
#		by Ian Murdock <imurdock@gnu.ai.mit.edu>.
#
# Version:	@(#)skeleton  1.6  11-Nov-1996  miquels@cistron.nl
#
# This file by Peter S Galbraith <psg@debian.org> for the powstatd package.

PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/sbin/powstatd
NAME=powstatd
DESC="UPS Monitoring Daemon"

test -f $DAEMON || exit 0
test -f /etc/powstatd.conf || exit 0
if grep '^#!Unconfigured!' /etc/powstatd.conf >/dev/null; then
   echo "$NAME: unconfigured.  See \"man 8 powstatd\" for help about configuration."
   exit 0
fi

set -e

case "$1" in
  start)
    echo -n "Starting $DESC: $NAME"
    if $DAEMON > /dev/null 2>&1
    then
        echo "."
    else
        echo "... failed."
    fi
    ;;
  stop)
    echo -n "Stopping $DESC: $NAME"
    start-stop-daemon --stop --oknodo --quiet --exec $DAEMON
    echo "."
    ;;
  poweroff)
    # This is called from /etc/init.d/halt
    #  `/sbin/powstatd -k` queries the UPS for powerline status and 
    #  sends the kill signal to the UPS only if there is a powerline failure.
    echo -n "Checking to see if UPS should be shut down..."
    if $DAEMON -k > /dev/null 2>&1
    then
        echo " Done."
    else
        echo " No."
    fi
    ;;
  force-reload|restart)
    echo -n "Restarting $DESC: $NAME"
    start-stop-daemon --stop --oknodo --quiet --exec $DAEMON
    sleep 1
    $DAEMON
    echo "."
    ;;
  *)
    echo "Usage: /etc/init.d/$NAME {start|stop|poweroff|restart|force-reload}"
    exit 0
    ;;
esac

exit 0
