#!/bin/sh
#PKG apcupsd
#INIT 15
. /etc/rc.conf

pidfile=$(echo "$apcupsd_flags" | \
	  sed -n 's/.*\(-P\|--pid-file\) \([^ ]\+\).*/\2/p')
[ "$pidfile" ] || pidfile="/var/run/apcupsd.pid"

case $1 in
autostop) ;;
autostart)
	test x"${apcupsd:-NO}" = x"NO" && exit 0
	test x"$apcupsd" = x"DAEMON" && test -x /bin/mksh && exec mksh -T- $0 start
	exec sh $0 start
	;;
start)
	mkdir -p /var/lock
	/usr/sbin/apcupsd $apcupsd_flags
	;;
stop)
	if [ -f "$pidfile" ]; then
		kill $(<$pidfile)
		rm -f $pidfile
	else
		kill $(pgrep -f /usr/sbin/apcupsd)
	fi
	;;
restart)
	sh $0 stop
	sleep 1
	sh $0 start
	;;
*)
	echo "usage: $0 (start | stop | restart)"
	exit 1
esac
exit $?