#!/bin/sh
#
# chkconfig: 2345 53 89
# description: The UPS monitor and shutdown controller
# processname: upsmon
# config: /etc/ups/
# pidfile: /var/run/upsmon.pid

WITHOUT_RC_COMPAT=1

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

PIDFILE=/var/run/upsmon.pid
LOCKFILE=/var/lock/subsys/upsmon
RETVAL=0

start()
{
	egrep -qs '^[[:space:]]*MONITOR' /etc/nut/upsmon.conf || return 0
	msg_starting $"UPS monitor"
	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user upsmon --no-announce -- upsmon
	RETVAL=$?
	return $RETVAL
}

stop()
{
	msg_stopping $"UPS monitor"
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user upsmon --no-announce -- upsmon
	RETVAL=$?
	return $RETVAL
}

restart()
{
	stop
	start
}

reload()
{
	msg_reloading $"UPS monitor"
	stop_daemon --pidfile "$PIDFILE" --expect-user upsmon -HUP -- upsmon
	RETVAL=$?
	return $RETVAL
}

# See how we are called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status --pidfile "$PIDFILE" --expect-user upsmon -- upsmon
		RETVAL=$?
		;;
	reload)
		reload
		;;
	restart)
		restart
		;;
	condstart)
		if [ ! -e "$LOCKFILE" ]; then
			start
		fi
		;;
	condstop)
	  	if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condreload)
		if [ -e "$LOCKFILE" ]; then
			reload
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	*)
		msg_usage "${0##*/} {start|stop|status|reload|restart|condstart|condstop|condreload|condrestart}"
		RETVAL=1
esac

exit $RETVAL
