#!/bin/sh
#
# chkconfig: 2345 30 90
# description: The NUT daemon
# processname: upsd
# config: /etc/ups/
# pidfile: /var/run/upsd.pid

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

UPSD=/sbin/upsd
UPSDRVCTL=/sbin/upsdrvctl
LOCKFILE=/var/lock/subsys/upsd
RETVAL=0

start() {
    action "Starting UPS drivers: " $UPSDRVCTL start
    RETVAL=$?
    [ "$RETVAL" = 0 ] || exit 1

    echo -n $"Starting UPS daemon: "
    daemon $UPSD
    RETVAL=$?
    echo
	
    [ "$RETVAL" = 0 ] && touch "$LOCKFILE"
}

stop() {
    echo -n $"Stopping UPS daemon: "
    killproc $UPSD
    RETVAL=$?
    echo

    action "Stopping UPS drivers: " $UPSDRVCTL stop

    [ "$RETVAL" = 0 ] && rm -f "$LOCKFILE"
}

restart() {
	stop
	start
}

# See how we are called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	restart
	;;
  condstop)
  	if [ -e "$LOCKFILE" ]; then
		stop
	fi
	;;
  condrestart)
	if [ -e "$LOCKFILE" ]; then
	    restart
	fi    
	;;
  status)
	action "Cheching UPS drivers:" $UPSDRVCTL status
	status $UPSD
	;;
  *)
	echo $"Usage: ${0##*/} {start|stop|restart|condstop|condrestart|status}"
	RETVAL=1
esac

exit $RETVAL
