#! /bin/sh
#
# apcupsd      This shell script takes care of starting and stopping
#	       the apcupsd UPS monitoring daemon as monitoring only
#
# chkconfig: 2345 33 89
# description: apcupsd monitors power without takes any action
#

### BEGIN INIT INFO
# Provides:          apcupsd-multiups
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:
# Default-Stop:
# Short-Description: Start apcupsd at boot time
# Description:       Run the apcupsd UPS monitoring daemon.
### END INIT INFO

RETVAL=0

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

[ x`ls /etc/apcupsd-multiups/apcupsd.*.conf 2>/dev/null` == x ] && exit

start()
{
	for CFG_FILE in /etc/apcupsd-multiups/apcupsd.*.conf ; do
	    CFG_NAME=`echo $CFG_FILE|sed "s/.*apcupsd\.\(.*\)\.conf/\1/"`
	    APCPID=/var/run/apcupsd-$CFG_NAME.pid
	    LOCKFILE=/var/lock/subsys/apcupsd-$CFG_NAME
	    msg_starting "UPS monitoring ($CFG_NAME)"
	    start_daemon --no-announce --pidfile "$APCPID" --lockfile "$LOCKFILE" -- /sbin/apcupsd -f $CFG_FILE -P $APCPID
	done
}

stop()
{
	for CFG_FILE in /etc/apcupsd-multiups/apcupsd.*.conf ; do
	    CFG_NAME=`echo $CFG_FILE|sed "s/.*apcupsd\.\(.*\)\.conf/\1/"`
	    APCPID=/var/run/apcupsd-$CFG_NAME.pid
	    LOCKFILE=/var/lock/subsys/apcupsd-$CFG_NAME
	    msg_stopping "UPS monitoring ($CFG_NAME)"
	    stop_daemon --no-announce --pidfile "$APCPID" --lockfile "$LOCKFILE" -- /sbin/apcupsd -f $CFG_FILE -P $APCPID
	done
}

restart()
{
	stop
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart|reload)
		restart
		;;
	condstop)
		stop
		;;
	condrestart)
		restart
		;;
	condreload)
		restart
		;;
	status)
		/bin/true
		RETVAL=$?
		;;
	*)
		echo "Usage: ${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
