#!/bin/sh
#
# chkconfig: 2345 52 88
# description: The UPS information server
# processname: upsd
# config: /etc/ups/
# pidfile: /var/run/upsd/upsd.pid

WITHOUT_RC_COMPAT=1

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

# Source service configuration.
SourceIfNotEmpty /etc/sysconfig/upsd

PIDDIR=/var/lib/upsd
PIDFILE=$PIDDIR/upsd.pid
LOCKFILE=/var/lock/subsys/upsd
RETVAL=0

start()
{
	msg_starting $"UPS information"
	if ! service upsdrv status >/dev/null 2>&1; then
		msg_not_running $"UPS driver"
		failure "upsd startup"
		RETVAL=$?
		echo
		return $RETVAL
	fi
	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user upsd --no-announce -- upsd $UPSD_OPTIONS
	RETVAL=$?
	return $RETVAL
}

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

restart()
{
	stop
	start
}

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

# See how we are called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status --pidfile "$PIDFILE" --expect-user upsd -- upsd
		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
