#! /bin/sh

# Exit if a single command fails
set -e

# Run as sauron
NAME=sauron
# Store the pid file in an appropriate place
PIDFILE=/var/run/$NAME.pid
# Run sauron, currently with no options
DAEMON=sauron-daemon
DAEMON_OPTS=""

export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"

# Test whether a process is running
function running() {
	local pid=$1
	echo `ps -p $pid | grep $pid | wc -l`
}

function start() {
	# If the pid file exists
	if [ -e $PIDFILE ]; then
		if [ $(running `cat $PIDFILE`) == "1" ]; then
			echo "$NAME is already running with pid `cat $PIDFILE`"
			exit 1
		fi
	fi
	# In case we don't have permissions to write to /var/run/, let's fail early
	echo "0" > $PIDFILE
	# Start the thing, obviously
	echo "Starting daemon: "$NAME
	nohup $DAEMON $DAEMON_OPTS > /dev/null &
	# Now save the pid to a file
	echo $! > $PIDFILE
}

function stop() {
	# And stop it
	if [ -e $PIDFILE ]; then
		while [ $(running `cat $PIDFILE`) ]; do
			echo "Attempting to kill $NAME (`cat $PIDFILE`)"
			kill `cat $PIDFILE`
			sleep 1
			if [ running `cat $PIDFILE` ]; then
				sleep 5
			else
				echo "Stopped."
			fi
		done
	fi
}

case "$1" in
	start)
		# Invoke the start function
		start
	;;
	stop)
		# Invoke the stop function
		stop
	;;
	restart)
		# First invoke the stop function
		stop
		# And then start it
		start
	;;
	*)
		# Didn't get any of the expected commands
		echo "Usage: "$1" {start|stop|restart}"
		exit 1
esac

exit 0