#!/bin/sh
# $FreeBSD: ports/sysutils/apcupsd/files/apcupsd.sh.sample,v 1.2 2001/12/02 13:07:25 lkoeller Exp $
# startup scripts for APCUPSD.

if ! PREFIX=$(expr $0 : "\(/.*\)/etc/rc\.d/$(basename $0)\$"); then
	echo "$0: Cannot determine the PREFIX" >&2
	exit 1
fi

# If there is a global system configuration file, suck it in.
if [ -r /etc/defaults/rc.conf ]; then
	. /etc/defaults/rc.conf
	source_rc_confs
elif [ -r /etc/rc.conf ]; then
	. /etc/rc.conf
fi

apcupsd_enable=${apcupsd_enable:-YES}
apcupsd_program=${apcupsd_program:-${PREFIX}/sbin/apcupsd}
apcupsd_flags=${apcupsd_flags:-"--kill-on-powerfail"}
apcupsd_pidfile=${apcupsd_pidfile:-/var/run/apcupsd.pid}
apcupsd_lockfile=${apcupsd_pidfile:-/var/spool/lock/apcupsd.lock}

case $1 in
    start)
	case "${apcupsd_enable}" in
	    [Yy][Ee][Ss])
		rm -f /var/run/powerfail
		rm -f /var/run/nologin
		if [ -f ${apcupsd_program} ]; then
		    echo -n " apcupsd"
		    ${apcupsd_program} ${apcupsd_flags} || return="  Failed."
		    touch ${apcupsd_lockfile}
		fi
		;;
	esac
	;;

    stop)
	if [ -f ${apcupsd_pidfile} ]; then
	    PID=`cat ${apcupsd_pidfile}`
	    kill -KILL $PID || return="  Failed."
	    rm -f ${apcupsd_pidfile}
	    # some slaves won't die
	    killall apcupsd
	else
	    return="  Failed."
	fi
	;;

    restart)
	$0 stop
	$0 start;
	;;

    status)
	${PREFIX}/sbin/apcaccess status
	;;

    *)
	echo "usage: $0 {start|stop|restart|status}" 1>&2
	;;
esac

exit 0;
