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

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

[ -x /sbin/apcupsd -a -s /etc/apcupsd/apcupsd.conf ] || exit

start()
{
	rm -f /etc/apcupsd/powerfail /etc/nologin
        start_daemon --pidfile "$APCPID" --lockfile "$LOCKFILE" -- /sbin/apcupsd -f /etc/apcupsd/apcupsd.conf
        RETVAL=$?
}

stop()
{
        stop_daemon --pidfile "$APCPID" --lockfile "$LOCKFILE" -- /sbin/apcupsd
        RETVAL=$?
}

restart()
{
	stop
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart|reload)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	status)
		/sbin/apcaccess status
		;;
	*)
		echo "Usage: ${0##*/} {start|stop|reload|restart|condstop|condrestart|status}"
		RETVAL=1
esac

exit $RETVAL
