#!/bin/sh
#
# $Id$
#
# PROVIDE: lighttpd
# REQUIRE: DAEMON
# KEYWORD: shutdown

# -----------------------------------------------------------------------------
#
# This script supports running multiple instances of lighttpd.
# To run additional instance link this script to something like
# % ln -s lighttpd lighttpd_foo
# and define additional lighttpd_foo_* variables in one of
# /etc/rc.conf, /etc/rc.conf.local or /etc/rc.conf.d/lighttpd_foo
#
# Below NAME should be substituted with the name of this script. By default
# it is lighttpd, so read as lighttpd_enable. If you linked the script to
# lighttpd_foo, then read as lighttpd_foo_enable etc.
#
# The following variables are supported (defaults are shown).
# You can place them in any of
# /etc/rc.conf, /etc/rc.conf.local or /etc/rc.conf.d/NAME
#
# NAME_enable="NO"		# set to YES to enable lighttpd
#
# # optional:
# NAME_user="(user)"		# set username for running lighttpd
# NAME_program="(path)"		# set full path to lighttpd program
# NAME_conf="(path)"		# set full path to config file
# NAME_pidfile="(path)"		# set full path to pid file
#

. /etc/rc.subr

case "$0" in
/etc/rc*)
	# during boot (shutdown) $0 is /etc/rc (/etc/rc.shutdown),
	# so get the name of the script from $_file
	name=$(basename "$_file" .sh)
	;;
*)
	name=$(basename "$0" .sh)
	;;
esac

rcvar=$(set_rcvar)

load_rc_config $name

eval ": \${${name}_enable:=\"NO\"}"
eval ": \${${name}_user:=\"www\"}"
eval ": \${${name}_program:=\"/usr/local/sbin/lighttpd\"}"
eval ": \${${name}_conf:=\"/usr/local/etc/${name}.conf\"}"
eval ": \${${name}_pidfile:=\"/var/run/${name}.pid\"}"

lighttpd_user="$(eval echo \${${name}_user})"
lighttpd_program="$(eval echo \${${name}_program})"
lighttpd_conf="$(eval echo \${${name}_conf})"
lighttpd_pidfile="$(eval echo \${${name}_pidfile})"

command=/usr/local/sbin/lighttpd
command_args="-f ${lighttpd_conf}"
pidfile=${lighttpd_pidfile}
required_files=${lighttpd_conf}
stop_postcmd=stop_postcmd
restart_precmd="checkconfig"
reload_precmd=reload_precmd
reload_postcmd=reload_postcmd
sig_reload="-INT"
check_cmd="checkconfig"
extra_commands="reload check"

checkconfig()
{
	echo "Performing sanity check on ${name} configuration:"
	eval "${command} ${command_args} -t"
}

stop_postcmd()
{
	rm -f ${pidfile}
}

reload_precmd()
{
	echo "Stoping ${name} and start gracefully."
}

reload_postcmd()
{
	rm -f ${pidfile}
	run_rc_command start
}

run_rc_command "$1"
