#!/bin/sh

### BEGIN INIT INFO
# Provides:          monitorix
# Required-Start:    $local_fs
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start Monitorix daemon
### END INIT INFO

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin

DESC="Monitorix"
NAME="monitorix"
CONF="/etc/monitorix/monitorix.conf"
DAEMON="/usr/bin/$NAME"
PIDFILE="/var/run/$NAME.pid"
LOCKFILE="/var/lock/$NAME"
SCRIPTNAME="/etc/init.d/$NAME"
DAEMON_ARGS="-c $CONF -p $PIDFILE"


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

# 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

lock_monitorix() {
	if [ -x /usr/bin/lockfile-create ]; then
		lockfile-create $LOCKFILE
		lockfile-touch $LOCKFILE &
		LOCKTOUCHPID="$!"
	fi
}

unlock_monitorix() {
	if [ -x /usr/bin/lockfile-create ] ; then
		kill $LOCKTOUCHPID
		lockfile-remove $LOCKFILE
	fi
}

case $1 in
	start)
		[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC $NAME"
		lock_monitorix
  		start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --startas $DAEMON -- -p $PIDFILE $DAEMON_ARGS
		status=$?
		unlock_monitorix
		log_end_msg $status
  		;;
	stop)
		[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC $NAME"
  		start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
		log_end_msg $?
		rm -f $PIDFILE
  		;;
	restart|force-reload)
		$0 stop && sleep 2 && $0 start
  		;;
	try-restart)
		if $0 status >/dev/null; then
			$0 restart
		else
			exit 0
		fi
		;;
	reload)
		exit 3
		;;
	status)
		status_of_proc $DAEMON "$DESC"
		;;
	*)
		echo "Usage: $0 {start|stop|restart|try-restart|force-reload|status}"
		exit 2
		;;
esac
