#!/bin/sh
#
# cherokee	Start the cherokee HTTP server.
#
# chkconfig:	345 20 80
#
# description:	Cherokee is Fast, Flexible and Lightweight Web server
#

# Source function library
. /etc/rc.d/init.d/functions

# Get network config
. /etc/sysconfig/network

# Get service config - may override defaults
[ -f /etc/sysconfig/cherokee ] && . /etc/sysconfig/cherokee

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network ]; then
		msg_network_down "Cherokee Web Server"
		exit 1
	fi
else
	exit 0
fi

# configtest itself
# must return non-zero if check failed
# output is discarded if checkconfig is ran without details
configtest() {
	local test_result="$(/usr/sbin/cherokee -t 2>&1)"
	# exit status is not usable here, parse the output
	if [ "${test_result##Test on*: }" = "OK" ] ; then
		return 0
	else
		echo $test_result >&2
		return 1
	fi
}

# wrapper for configtest
# with $details = 1 will report ok/fail status to output (status action)
# with $details = 0 will silently discard ok output, and with fail exit script with failure
checkconfig() {
	local details=${1:-0}

	if [ $details = 1 ]; then
		# run config test and display report (status action)
		show "Checking %s configuration" "Cherokee Web Server"; busy
		local out
		out=$(configtest 2>&1)
		RETVAL=$?
		if [ $RETVAL = 0 ]; then
			ok
		else
			fail
		fi
		[ "$out" ] && echo >&2 "$out"
	else
		# run config test and abort with nice message if failed
		# (for actions checking status before action).
		configtest >/dev/null 2>&1
		RETVAL=$?
		if [ $RETVAL != 0 ]; then
			show "Checking %s configuration" "Cherokee Web Server"; fail
			nls 'Configuration test failed. See details with %s "checkconfig"' $0
			exit $RETVAL
		fi
	fi
}

start() {
	# Check if the service is already running?
	if [ -f /var/lock/subsys/cherokee ]; then
		msg_already_running "Cherokee Web Server"
		return
	fi

	msg_starting "Cherokee Web Server"
	daemon cherokee -d
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
		touch /var/lock/subsys/cherokee
	fi
}

stop() {
	if [ ! -f /var/lock/subsys/cherokee ]; then
		msg_not_running "Cherokee Web Server"
		return
	fi

	# Stop daemons.
	msg_stopping "Cherokee Web Server"
	killproc cherokee
	RETVAL=$?
	rm -f /var/lock/subsys/cherokee >/dev/null 2>&1
}

condrestart() {
	if [ ! -f /var/lock/subsys/cherokee ]; then
		msg_not_running "Cherokee Web Server"
		RETVAL=$1
		return
	fi

	checkconfig
	stop
	start
}

reload() {
	if [ ! -f /var/lock/subsys/cherokee ]; then
		msg_not_running "Cherokee Web Server"
		RETVAL=7
	fi

	checkconfig
	msg_reloading "Cherokee Web Server"
	pid="$(pidofproc cherokee)"
	if [ -n "$pid" ] && kill -HUP $(pidofproc cherokee); then
		ok
	elif [ "$1" = "force-reload" ] ; then
		fail
		stop
		start
		RETVAL=$?
	else
		fail
		RETVAL=1
	fi
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	checkconfig
	stop
	start
	;;
  reload|force-reload)
	reload $1
	;;
  try-restart)
	condrestart 0
	;;
  checkconfig|configtest)
  	checkconfig 1
	RETVAL=$?
	;;
  status)
	status cherokee
	RETVAL=$?
	;;
  *)
	msg_usage "$0 {start|stop|restart|try-restart|force-reload|configtest|status}"
	exit 3
esac

exit $RETVAL
