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

DISTVER=`cat /etc/nortel-release | grep release | cut -f 5 -d ' '`

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

case "$1" in
    start)
#	grep 'Manufacturer=American Power Conversion' /proc/bus/usb/devices > /dev/null 2>&1
#	if [ $? -ne  0 ]  
#       then
#          /nn/sbin/monit unmonitor apcupsd
#	   echo -n "UPS-USB cable is not connected to M50"  
#	   echo
#	   exit 1 
#       fi
#	grep 'If#= 0 Alt= 0 #EPs= 1 Cls=03(HID  ) Sub=00 Prot=00 Driver=hid' /proc/bus/usb/devices > /dev/null 2>&1
#  	if [ $? -ne 0 ]  
#	then
#	   /nn/sbin/monit unmonitor apcupsd
#	   echo -n "UPS-USB HIDDEV driver is not available"  
#           echo
#	   exit 1 
#       fi
#       /nn/sbin/monit monitor apcupsd


       rm -f /opt/etc/apcupsd/powerfail
       rm -f /opt/etc/nologin
       echo -n "Starting UPS monitoring:" 
       daemon /opt/sbin/apcupsd -f /opt/etc/apcupsd/apcupsd.conf
       RETVAL=$?
       [ $RETVAL -eq 0 ] && touch /var/lock/subsys/apcupsd
       ;;
    stop)
       echo -n "Shutting down UPS monitoring:"
       killproc apcupsd
       rm -f $APCPID
       rm -f /var/lock/subsys/apcupsd
       ;;
    restart)
       $0 stop
       sleep 15
       $0 start
       ;;
    status)
       /opt/sbin/apcaccess status
       ;;
    *)
       echo "Usage: $0 {start|stop|restart|status}"
       exit 1
       ;;
esac
exit 0
