#! /bin/sh
### BEGIN INIT INFO
# Provides:          eehttpd
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts the EEHTTPD asset server
### END INIT INFO
#

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/eehttpd
DAEMON_DIR=/var/lib/tawd
NAME=tawd
DESC="EEHTTP Asset Server"
PIDFILE=/var/run/eehttpd.pid

test -x $DAEMON || exit 0

set -e

. /lib/lsb/init-functions

case "$1" in
  start)
	echo -n "Starting $DESC: "
	start-stop-daemon --start --quiet --chdir $DAEMON_DIR --exec $DAEMON --pidfile $PIDFILE -- -c /etc/tawd -d -p $PIDFILE 
	echo "$NAME."
	;;
  stop)
	echo -n "Stopping $DESC: "
	# Send TERM after 5 seconds, wait at most 30 seconds.
	if start-stop-daemon --stop --oknodo --retry TERM/5/KILL/30 --exec $DAEMON --pidfile $PIDFILE ; then  
		rm -f $PIDFILE
	fi
	echo "$NAME."
	;;
 status)
         status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $?
        ;;
  restart)
	echo -n "Restarting $DESC: "
	if start-stop-daemon --stop --oknodo --retry TERM/5/KILL/30 --exec $DAEMON --pidfile $PIDFILE ; then 
		rm -f $PIDFILE
	fi
	start-stop-daemon --start --quiet --chdir $DAEMON_DIR --exec $DAEMON -- -c /etc/tawd -d -p $PIDFILE
	echo "$NAME."
	;;
  *)
	echo "Usage: $0 {start|stop|restart}" >&2
	exit 1
	;;
esac

exit 0
