#!/bin/sh

# PROVIDE: upsd
# REQUIRE: NETWORK syslogd mountcritremote

# You need to set some parameters for this script in /etc/rc.conf,
# examples are:
#upsd=YES	upsd_flags=""	
#		upsd_dev="/dev/tty01"
#		upsd_driver="apcsmart"
#upsmon=YES	upsmon_host="localhost"

. /etc/rc.conf

if [ -z "$1" -o "x$1" = xstart ]; then
	if [ "x${upsd}" = xYES ]; then
		%%PREFIX%%/bin/${upsd_driver} ${upsd_flags} ${upsd_dev}
		%%PREFIX%%/sbin/upsd
	fi

	if [ "x${upsmon}" = xYES ]; then
		%%PREFIX%%/sbin/upsmon ${upsmon_host}
	fi
elif [ "x$1" = xstop ]; then
	echo "Stopping upsd - caveat, this may not work if multiple instances are running"
	dpid=`ps ax | fgrep %%PREFIX%%/bin/${upsd_driver} | fgrep -v fgrep | awk '{print $1}'`
	upid=`ps ax | fgrep %%PREFIX%%/sbin/upsd | fgrep -v fgrep | awk '{print $1}'`
	if [ ! -z "${upid}" ]; then
		kill ${upid}
	fi
	if [ ! -z "${dpid}" ]; then
		kill ${dpid}
	fi
	if [ -r /var/run/upsmon.pid ]; then
		mpid=`/bin/cat /var/run/upsmon.pid`
		kill ${mpid}
	fi
fi
