#!/bin/sh
# Slackware startup script for Network UPS Tools
# Copyright 2010 V'yacheslav Stetskevych

# UPS drivers live here
DRIVERPATH=/usr/libexec/nut
export PATH=$DRIVERPATH:$PATH

POWERDOWNFLAG=/etc/killpower
NUTUSER=nut
NUTGROUP=nut
UPSDCONF=/etc/ups/upsd.conf
UPSCONF=/etc/ups/ups.conf
UPSMONCONF=/etc/ups/upsmon.conf

# Check for existense of the nut user and group
# For slackbuilds.org, assigned nut uid/gid are 218/218.
# See http://slackbuilds.org/uid_gid.txt
if ! grep -q ^$NUTGROUP: /etc/group; then
	echo "  You must have a \"$NUTGROUP\" group to run this script."
	echo "    # groupadd -g 218 $NUTGROUP"
	exit 1
elif ! grep -q ^$NUTUSER: /etc/passwd; then
	echo "  You must have a \"$NUTUSER\" user to run this script."
	echo "    # useradd -u 218 -g $NUTGROUP -s /bin/false $NUTUSER"
	exit 1
fi

start_driver() {
	upsdrvctl -u $NUTUSER start || exit 1
}

start_upsd() {
	upsd -u $NUTUSER || exit 1
}

start_upsmon() {
	upsmon -u $NUTUSER || exit 1
}

stop() {
	echo "Stopping the UPS services... "
	if pgrep upsd 2>&1 >/dev/null; then
		upsd -c stop; fi
	if pgrep upsmon 2>&1 >/dev/null; then
		upsmon -c stop; fi
	upsdrvctl stop
}

case "$1" in
	start)	# starts everything (for a ups server box)
		start_driver
		start_upsd
		start_upsmon
		;;
	start_upsmon) # starts upsmon only (for a ups client box)
		start_upsmon
		;;
	stop) # stops all UPS-related daemons
		stop	
		;;
	shutdown) # shuts down the UPS
		echo "Killing inverter..."
		upsdrvctl shutdown
		;;
	reload)
		echo "Reloading config files..."
		upsd -c reload
		upsmon -c reload
		;;
	*)
		echo "Usage: $0 {start|start_upsmon|stop|shutdown|reload}"
esac