#!/bin/sh

send_signal() {
	sig="$1"
	pid=`/sbin/pidof esehttpd`
	if [ -z "$pid" ]; then
		exit 0
	fi
	echo -n "Shutting down esehttpd (signal $1 pid $pid) ... "
	for i in $pid; do
		kill "-$1" "$i" 2> /dev/null
	done
	while [ -n "`/sbin/pidof esehttpd`" ]; do
		if [ -x /bin/usleep ]; then
			/bin/usleep 100000
		else
			sleep 1
		fi
	done
	echo "OK"
}

case "$1" in
start)
	echo -n "Starting /usr/local/sbin/esehttpd ... "
	if ( "/usr/local/sbin/esehttpd" ); then
		echo "OK"
		exit 0
	else
		echo "FAILED"
		exit 1
	fi
	;;
graceful)
	send_signal HUP
	;;
stop)
	send_signal TERM
	;;
status)
	pid=`/sbin/pidof esehttpd`
	if [ -z "$pid" ]; then
		echo "Esehttpd is stopped"
		exit 1
	else
		echo "Esehttpd is running (pid $pid)"
		exit 0
	fi
	;;
configtest)
	if ( "/usr/local/sbin/esehttpd" -t ); then
		exit 0
	fi
	exit 1
	;;
restart)
	if ! $0 configtest ; then
		exit 1
	fi
	$0 graceful
	$0 start
	;;
*)
	echo "Usage: $0 {start|stop|restart|status|configtest}"
	exit 1
esac

exit 0

