#!/bin/sh

PREFIX=$(expr `realpath $0` : "\(/.*\)/etc/rc\.d/`basename $0`\$")
PROG=$PREFIX/sbin/AquaGatekeeper
CFG=$PREFIX/etc/agk.cfg
TIMEOUT=30

case "$1" in
start)
	shift
	su -m agk -c "[ -x $PROG -a -r $CFG ] &&
	  echo -n ' AquaGatekeeper' && $PROG -d --configuration=$CFG $@"
	;;
stop)
	shift
	signal=-TERM
	[ -n "$1" ] && signal="$1"
	killall $signal AquaGatekeeper
	[ $signal != -2 -a $signal != -INT ] && exit 0
	i=1
	while [ $i -le $TIMEOUT ]
	do
	  ps -axc | grep -q AquaGatekeeper || break
	  i=$(($i+1))
	  sleep 1
	done
	;;
restart)
	`realpath $0` stop -INT
	`realpath $0` start
	;;
*)
	echo "Usage: `basename $0` {start|stop|restart}" >&2
	;;
esac

exit 0
