#! /bin/sh
### BEGIN INIT INFO
# Provides:          munin-async
# Required-Start:    $network $named $local_fs $remote_fs munin-node
# Required-Stop:     $network $named $local_fs $remote_fs munin-node
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Munin asynchronous server
# Description:       Asynchronous munin node
### END INIT INFO

# Author: Jorne Kandziora <jorne@quarantainenet.nl>
#
# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Munin asynchronous server"
NAME=munin-asyncd
DAEMON=/usr/share/munin/$NAME
DAEMON_ARGS=""
DAEMON_USER="munin-async"
PIDFILE=/var/run/munin/$NAME.pid
SCRIPTNAME=/etc/init.d/munin-async

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started
	start-stop-daemon --start --background --make-pidfile --pidfile $PIDFILE --chuid $DAEMON_USER --exec $DAEMON --test > /dev/null \
		|| return 1
	start-stop-daemon --start --background --make-pidfile --pidfile $PIDFILE --chuid $DAEMON_USER --exec $DAEMON -- \
		$DAEMON_ARGS \
		|| return 2
}

#
# Function that stops the daemon/service
#
do_stop()
{
        # killproc() doesn't try hard enough if the pid file is missing,
        # so create it is gone and the daemon is still running
        if [ ! -r $PIDFILE ]; then
                pid=$(pidofproc -p $PIDFILE $DAEMON)
                if [ -z "$pid" ]; then
                        [ "$VERBOSE" != no ] && log_progress_msg "stopped beforehand"
                        log_end_msg 0
                        return 0
                fi
                echo $pid 2>/dev/null > $PIDFILE
                if [ $? -ne 0 ]; then
                        log_end_msg 1
                        return 1
                fi
        fi
        killproc -p $PIDFILE /usr/bin/munin-node
        ret=$?
        # killproc() isn't thorough enough, ensure the daemon has been
        # stopped manually
        attempts=0
        until ! pidofproc -p $PIDFILE $DAEMON >/dev/null; do
                attempts=$(( $attempts + 1 ))
                sleep 0.05
                [ $attempts -lt 20 ] && continue
                log_end_msg 1
                return 1
        done
        rm "$PIDFILE"
        [ $ret -eq 0 ] && [ "$VERBOSE" != no ] && log_progress_msg "done"
        log_end_msg $ret
        return $ret
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
	#
	# If the daemon can reload its configuration without
	# restarting (for example, when it is sent a SIGHUP),
	# then implement that here.
	#
	start-stop-daemon --stop --signal 1 --background --make-pidfile --quiet --pidfile $PIDFILE --exec $DAEMON
	return 0
}

case "$1" in
  start)
	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
	do_start
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  stop)
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
	do_stop
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  #reload|force-reload)
	#
	# If do_reload() is not implemented then leave this commented out
	# and leave 'force-reload' as an alias for 'restart'.
	#
	#log_daemon_msg "Reloading $DESC" "$NAME"
	#do_reload
	#log_end_msg $?
	#;;
  restart|force-reload)
	#
	# If the "reload" option is implemented then remove the
	# 'force-reload' alias
	#
	log_daemon_msg "Restarting $DESC" "$NAME"
	do_stop
	case "$?" in
	  0|1)
		do_start
		case "$?" in
			0) log_end_msg 0 ;;
			1) log_end_msg 1 ;; # Old process is still running
			*) log_end_msg 1 ;; # Failed to start
		esac
		;;
	  *)
	  	# Failed to stop
		log_end_msg 1
		;;
	esac
	;;
  status)
    pid=$(pidofproc -p $PIDFILE $DAEMON)
  ret=$?
  pid=${pid% } # pidofproc() supplies a trailing space, strip it
  if [ $ret -eq 0 ]; then
    log_success_msg "Munin-Async is running (PID: $pid)"
    exit 0
  # the LSB specifies that I in this case (daemon dead + pid file exists)
  # should return 1, however lsb-base returned 2 in this case up to and
  # including version 3.1-10 (cf. #381684).  Since that bug is present
  # in Sarge, Ubuntu Dapper, and (at the time of writing) Ubuntu Etch,
  # and taking into account that later versions of pidofproc() do not
  # under any circumstance return 2, I'll keep understanding invalid
  # return code for the time being, even though the LSB specifies it is
  # to be used for the situation where the "program is dead and /var/lock
  # lock file exists".
  elif [ $ret -eq 1 ] || [ $ret -eq 2 ]; then
    log_failure_msg "Munin-Async is dead, although $PIDFILE exists."
    exit 1
  elif [ $ret -eq 3 ]; then
    log_warning_msg "Munin-Async is not running."
    exit 3
  fi
  log_warning_msg "Munin-Async status unknown."
  exit 4
        ;;
  *)
	#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload|status}" >&2
	exit 3
	;;
esac

:
