#!/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

[ -n "$PORT" ] && OPTIONS="-p $PORT"
[ -n "$TCP_PORT" ] && OPTIONS="-t $TCP_PORT"
[ -n "$BIND_ADDRESS" ] && OPTIONS="$OPTIONS -i $BIND_ADDRESS"

# 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/lib/nut/upsdrvctl start
		if [ $? -eq 0 ]; then
			msg_starting "UPS network daemon"
			daemon "upsd $OPTIONS"
			RETVAL=$?
			touch /var/lock/subsys/ups
		fi
	else
		msg_Already_Running "UPS drivers and network daemon"
	fi
	;;
  stop)
  	if [ -f /var/lock/subsys/ups ]; then
		msg_stopping "UPS daemon"
		killproc upsd
		show "Stopping UPS drivers"
		daemon /usr/lib/nut/upsdrvctl stop
		RETVAL=$?
		rm -f /var/lock/subsys/ups
	else
		msg_Not_Running "UPS daemon"
	fi
	;;
  restart)
	$0 stop
	$0 start
	;;
  reload)
  	msg_reloading "UPS drivers"
	daemon /usr/lib/nut/upsdrvctl reload
	msg_reloading "UPS network daemon"
	daemon upsd -c reload
  	;;
  force-reload)
	$0 restart
	exit $?
	;;
  status)
	status upsd
	/usr/lib/nut/upsdrvctl status
	;;
  *)
	msg_Usage "$0 {start|stop|status|restart|reload|force-reload}"
	exit 1
	;;
esac 
exit $RETVAL

