#! /bin/sh
#
# start/stop Cherokee web server

### BEGIN INIT INFO
# Provides:          cherokee
# Required-Start:    $network
# Required-Stop:     $network
# Should-Start:      $named
# Should-Stop:       $named
# Default-Start:     S 2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start the Cherokee Web server
# Description:       Start the Cherokee Web server
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/cherokee
NAME=cherokee

. /lib/lsb/init-functions

test -x $DAEMON || exit 0

PIDFILE=$(grep -i '^PidFile' /etc/cherokee/cherokee.conf | sed -e 's/pidfile[\t ]\+//i')
if [ "x$PIDFILE" = "x" ];
then
        PIDFILE=/var/run/cherokee.pid
fi

set -e


case "$1" in
  start)	
	echo -n "Starting web server: $NAME "
	start-stop-daemon --start --oknodo --pidfile $PIDFILE --exec $DAEMON -b
	;;

  stop)
	echo -n "Stopping web server: $NAME "
	start-stop-daemon --stop --oknodo --pidfile $PIDFILE --exec $DAEMON
	rm -f $PIDFILE
	# Try harder to guess if Cherokee might still be alive (this extra step
	# will be removed in the future - Late 0.4 / early 0.5 versions didn't
	# always behave properly regarding the PIDFILE, so we better 
	# double-check here to avoid trouble.
	# Why the redirection and the || true? Redirection, to avoid cluttering
	# up our output. || true because we have had reports of this script 
	# bombing because of a failed kill to a process that no longer exists 
	# (remember we run with set -e).
	for PID in $(pidofproc cherokee)
	  do
	  echo Cherokee still running without registering PID $PID - Terminating
	  kill $PID 2>/dev/null || true
	  done
	;;

  restart|reload|force-reload)
	$0 stop
	sleep 1
	$0 start
	;;

# Upstream asks us not to signal HUP for simple reloads, as it's broken due to
# several leaks. Will be fixed for the 0.6 series.
#   reload|force-reload)
# 	start-stop-daemon --stop --signal HUP --pidfile $PIDFILE --name $NAME
# 	;;

  status)
	# Strictly, LSB mandates us to return indicating the different statuses,
	# but that's not exactly Debian compatible - For further information:
	# http://www.freestandards.org/spec/refspecs/LSB_1.3.0/gLSB/gLSB/iniscrptact.html
	# http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=208010
	# ...So we just inform to the invoker and return success.
	echo -n "$NAME web server status: "
	if [ -e $PIDFILE ] ; then
	    PROCNAME=$(ps -p $(cat $PIDFILE) -o comm=)
	    if [ "x$PROCNAME" = "x" ]; then
		echo -n "Not running, but PID file present"
	    else
		if [ "$PROCNAME" = "$NAME" ]; then
		    echo -n "Running"
		else
		    echo -n "PID file points to process '$PROCNAME', not '$NAME'"
		fi
	    fi
	else
	    if PID=$(pidofproc cherokee); then
		echo -n "Running (PID $PID), but PIDFILE not present"
	    else
		echo -n "Not running"
	    fi
	fi
	;;

  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
	exit 1
	;;
esac

if [ $? = 0 ]; then
        echo .
        exit 0
else
        echo failed
        exit 1
fi

exit 0
