#!/bin/sh
#
# chkconfig: - 10 90
# description: The ups daemon automatically starts a shutdown
# processname: upsd
# config: /etc/ups/

# Source function library.
if [ -f /etc/init.d/functions ]; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ]; then
  . /etc/rc.d/init.d/functions
else
 exit 0
fi

# Get config.
if [ -f /etc/sysconfig/ups ]; then
	. /etc/sysconfig/ups
else
	SERVER="no"
fi

start() {
    if [ "$SERVER" = "yes" ]; then
	echo -n "Starting $MODEL: "
	daemon /usr/bin/$MODEL $OPTIONS $DEVICE 
	echo

	echo -n "Starting UPS daemon: "
	daemon /usr/bin/upsd
	RETVAL=$?
	echo

	echo -n "Starting UPS monitor (master): "
	daemon /usr/bin/upsmon
	echo
    else
	echo -n "Starting UPS monitor (slave): "
	daemon /usr/bin/upsmon
	echo
    fi

    [ "$RETVAL" = 0 ] && touch /var/lock/subsys/ups
}

stop() {
    echo -n "Stopping UPS monitor: "
    killproc upsmon
    echo

    if [ "$SERVER" = "yes" ]; then
	echo -n "Shutting down UPS daemon: "
	killproc upsd
	RETVAL=$?
	echo

	echo -n "Shutting down $MODEL: "
	killproc $MODEL
	echo
    fi
    [ "$RETVAL" = 0 ] && rm -f /var/lock/subsys/ups
}

restart() {
	stop
	start
}

# See how we are called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	restart
	;;
  condrestart)
	[ -f /var/lock/subsys/ups ] && restart || :
	;;
  status)
	if [ "$HOST" = "localhost" ]; then
	    status upsd
	    status $MODEL
	fi
	status upsmon
	;;
  *)
	echo "Usage: ups {start|stop|restart|condrestart|status}"
	exit 1
esac

exit $RETVAL
