#!/bin/sh
# kFreeBSD do not accept scripts as interpreters, using #!/bin/sh and sourcing.
if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then
    set "$0" "$@"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script
fi
### BEGIN INIT INFO
# Provides:          munin-httpd
# Required-Start:    $local_fs $network $remote_fs $syslog
# Required-Stop:     $local_fs $network $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Munin HTTPD
# Description:       The munin HTTPD is used to graph and present metrics
#                    gathered by the munin master from munin nodes
### END INIT INFO

DAEMON=/usr/bin/munin-httpd
USER=munin-httpd
START_ARGS="--make-pidfile --background --user $USER --chuid $USER"
STOP_ARGS="--user $USER"

# the separate service "munin-rrdcached" starts an rrdcached instance with this socket
RRDCACHED_ADDRESS=/run/munin/flush.sock
export RRDCACHED_ADDRESS

# allow reload via SIGUSR1
alias do_reload=do_reload_sigusr1

# difference compared to "do_start_cmd": remove "--exec" parameter
# see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=843419
do_start_cmd_override() {
	start-stop-daemon --start --quiet ${PIDFILE:+--pidfile ${PIDFILE}} \
		$START_ARGS --startas $DAEMON --name $NAME --test > /dev/null || return 1
	start-stop-daemon --start --quiet ${PIDFILE:+--pidfile ${PIDFILE}} \
		$START_ARGS --startas $DAEMON --name $NAME -- $DAEMON_ARGS || return 2
}

# difference compared to "do_stop_cmd": remove "--exec" parameter
# see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=843419
do_stop_cmd_override() {
	local RETVAL
	start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 \
		$STOP_ARGS \
		${PIDFILE:+--pidfile ${PIDFILE}} --name $NAME
	RETVAL="$?"
	[ "$RETVAL" = 2 ] && return 2
	start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 \
		$STOP_ARGS \
		${PIDFILE:+--pidfile ${PIDFILE}}
	[ "$?" = 2 ] && return 2
	rm -f "$PIDFILE"
	return "$RETVAL"
}
