#!/bin/sh
#
# ups		NUT - Network UPS Tools daemon
#
# chkconfig:	2345 10 90
#
# description:	The ups daemon monitors an UPS and makes information about \
#		it's status available to other programs
# processname:	upsd
# config:	/etc/ups/

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

# Get network config
. /etc/sysconfig/network

# Get config.
[ -f /etc/sysconfig/ups ] && . /etc/sysconfig/ups

RETVAL=0
# See how we are called.
case "$1" in
  start)
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/ups ]; then
		show "Starting UPS drivers"
		daemon /usr/sbin/upsdrvctl start
		RETVALDRV=$?
		msg_starting "UPS network daemon"
		daemon /usr/sbin/upsd $UPSD_OPTIONS
		RETVAL=$?
		if [ $RETVAL -eq 0 ]; then
			touch /var/lock/subsys/ups
			RETVAL=$RETVALDRV
		fi
	else
		msg_already_running "UPS drivers and network daemon"
	fi
	;;
  stop)
	if [ -f /var/lock/subsys/ups ]; then
		msg_stopping "UPS daemon"
		/usr/sbin/upsd -c stop
		run_cmd "Stopping UPS drivers" /usr/sbin/upsdrvctl stop
		rm -f /var/lock/subsys/ups
	else
		msg_not_running "UPS daemon"
	fi
	;;
  restart)
	$0 stop
	$0 start
	exit $?
	;;
  reload|force-reload)
	if [ -f /var/lock/subsys/ups ]; then
		show "Reloading UPS drivers"
		/usr/sbin/upsdrvctl stop
		/usr/sbin/upsdrvctl start
		[ $? -ne 0 ] && RETVAL=7
		msg_reloading "UPS network daemon"
		/usr/sbin/upsd -c reload
		[ $? -ne 0 ] && RETVAL=7
	else
		msg_not_running "UPS daemon" >&2
		exit 7
	fi
	;;
  powerdown)
	show "Switching the power off"; busy
	/usr/sbin/upsdrvctl shutdown
	sleep 60
	fail
	;;
  status)
	status upsd
	RETVAL=$?
	;;
  *)
	msg_usage "$0 {start|stop|restart|reload|force-reload|status|powerdown}"
	exit 3
esac

exit $RETVAL
