#! /bin/sh
# Mooix init script for Debian.

DAEMON=/usr/sbin/mood
NAME=mood
DESC="Mooix"
PIDFILE="/var/run/$NAME.pid"

# Let's mot inherit this.
unset DISPLAY

test -f $DAEMON || exit 0

set -e

# Umask for the moo. Important that this *not* be 002; group-writables
# files have special semantics in the moo.
umask 022

# Source defaults file; edit that file to configure this script.
PARAMS=""
if [ -e /etc/default/mooix ]; then
	. /etc/default/mooix
fi
# Source conffile, for starthook and stophook.
if [ -e /etc/mooix.conf ]; then
	. /etc/mooix.conf
fi

case "$1" in
  start)
	printf "Starting $DESC: "
	
	# Check to see if it's running before starting it.
	if start-stop-daemon --stop --signal 0 --quiet --pidfile $PIDFILE >/dev/null 2>&1; then
		echo "already running."
		exit
	fi
	
	# For robustness.
	install -d /var/run/mooix
	
	printf $NAME
	start-stop-daemon --start --quiet --pidfile $PIDFILE \
		--exec $DAEMON -- $PARAMS
	printf " ring"
	eval $STARTHOOK >/dev/null
	echo .
	;;
  stop)
	printf "Stopping $DESC: "
	
	# Check to see if it's running before running the stophook.
	if ! start-stop-daemon --stop --signal 0 --quiet --pidfile $PIDFILE >/dev/null 2>&1; then
		echo "not running."
		exit
	fi

	printf ring
	eval $STOPHOOK >/dev/null
	printf " $NAME"
	# Make sure it goes down.
	start-stop-daemon --stop --quiet --pidfile $PIDFILE \
		--retry TERM/2/TERM/4/TERM/8/TERM/16/KILL/4  \
		--exec $DAEMON -- $PARAMS
	echo .
	;;
  restart|force-reload)
  	$0 stop
	$0 start
	;;
  *)
	echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
