#! /bin/sh
#
# apcupsd      This shell script takes care of starting and stopping
#	       the apcupsd UPS monitoring daemon.
#
# chkconfig: 2345 60 99
# description: apcupsd monitors power and takes action if necessary
#

### BEGIN INIT INFO
# Provides: apcupsd
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start: 2 3 5
# Default-Stop:
# Description: Start the apcupsd daemon
### END INIT INFO

APCPID=@PIDDIR@/apcupsd.pid

# Source function libarary
. /etc/rc.d/init.d/functions

if [ ! -x @sbindir@/apcupsd -o ! -r @sysconfdir@/apcupsd.conf ]; then
    exit 0
fi

case "$1" in
    start)
       rm -f @PWRFAILDIR@/powerfail
       rm -f @nologdir@/nologin
# check for stale lockfiles
       LOCKDIR=`grep "^LOCKFILE" @sysconfdir@/apcupsd.conf | awk '{ print $2}'`
       DEVICE=`grep "^DEVICE" @sysconfdir@/apcupsd.conf | awk '{ print $2 }' | sed -e 's@/dev/@@g'`
       if [ -z "${DEVICE}" ]; then
          DEVICE=usb
       fi
       LOCKFILE="${LOCKDIR}/LCK..${DEVICE}"
       if [ -f $LOCKFILE ]; then
	  PID=`cat $LOCKFILE | awk '{ printf("%d",$1) }'`
       else
	  PID=""
       fi
       PIDREAL=`ps -e | awk '{print $1}' | grep "${PID}"`
       if [ ! -z "$PID" -a ! "$PID" = "$PIDREAL" ]; then
	  echo "Stale lockfile, removing $LOCKFILE"
	  if [ -f $LOCKFILE ]; then
	     rm -f $LOCKFILE
	  fi
       fi
       echo -n "Starting UPS monitoring:" 
       @sbindir@/apcupsd -f @sysconfdir@/apcupsd.conf && success || failure
       echo
       touch @LOCKDIR@/subsys/apcupsd
       ;;
    stop)
       echo -n "Shutting down UPS monitoring:"
       killproc apcupsd
       echo
       rm -f $APCPID
       rm -f @LOCKDIR@/subsys/apcupsd
       ;;
    restart|reload)
       $0 stop
       sleep 15
       $0 start
       ;;
    status)
       @sbindir@/apcaccess status
       ;;
    *)
       echo "Usage: $0 {start|stop|restart|reload|status}"
       exit 1
       ;;
esac
exit 0
