#!/sbin/sh

#init.d script to start nut services

NUT_DIR="@prefix@"
CONFIG=$NUT_DIR/etc/nut.conf

if [ -f $CONFIG ] ; then
        . $CONFIG
fi

ups_stop () {
	pkill -n upsmon
	pkill -n upsd
	${NUT_DIR}/sbin/upsdrvctl stop > /dev/null 2>&1
}

ups_start () {
	if [ "$MODE" = "none" ];then
		echo No mode set
		exit 1
	fi

	if [ ! "$MODE" = "netclient" ];then
		$NUT_DIR/sbin/upsdrvctl start #> /dev/null 2>&1
		$NUT_DIR/sbin/upsd #> /dev/null 2>&1
	fi
	$NUT_DIR/sbin/upsmon #> /dev/null 2>&1
}

case $1 in
'start')
	ups_start
	;;

'stop')
	ups_stop
	;;

'restart')
	ups_stop
	while pgrep upsd > /dev/null
	do
		sleep 1
	done
	ups_start
	;;
'poweroff')
	$NUT_DIR/sbin/upsmon -K  >/dev/null 2>&1
	if [ $? = 0 ]; then
        	echo "Shutting down the UPS ..."
		#$NUT_DIR/sbin/upsdrvctl shutdown
	fi
	;;
*)
	echo ""
	echo "Usage: '$0' {start | stop | restart }"
	echo ""
	exit 64
	;; 

esac
exit $?