#! /bin/sh

### BEGIN INIT INFO
# Provides:          atheme-services
# Required-Start:    $syslog
# Required-Stop:     $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Atheme-services daemon init.d script
# Description:       Use to manage the Atheme services daemon.
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/atheme-services
NAME="atheme-services"
DESC="Atheme IRC Services"

test -x $DAEMON || exit 0

# Include atheme defaults if available
if [ -f /etc/default/atheme-services ] ; then
	. /etc/default/atheme-services
fi

if [ "$ENABLED" != "1" ] ; then
	echo "Please set ENABLED to 1 in /etc/default/atheme-services"
fi

set -e

case "$1" in
  start)
	if [ "$ENABLED" = "1" ]; then
		echo -n "Starting $DESC: $NAME"
		if [ -f "/var/run/atheme/atheme.pid" ]; then
			echo -n "Already running!"
		else
			start-stop-daemon -u irc -c irc --start --quiet --pidfile /var/run/atheme/atheme.pid \
				--exec $DAEMON -- $DAEMON_OPTS
		fi
		echo "."
	fi
	;;
  stop)
	if [ "$ENABLED" = "1" ]; then
		echo -n "Stopping $DESC: $NAME"
		if [ -f "/var/run/atheme/atheme.pid" ]; then
			start-stop-daemon --stop --quiet --pidfile /var/run/atheme/atheme.pid \
				--exec $DAEMON --signal 15
		fi
		echo "."
	fi
	;;
  reload|force-reload)
	if [ "$ENABLED" = "1" ]; then
		if [ -f "/var/run/atheme/atheme.pid" ]; then
			echo "Reloading $DESC configuration files."
			start-stop-daemon --stop --signal 1 --quiet --pidfile \
				/var/run/atheme/atheme.pid --exec $DAEMON
		else
			echo "$NAME not running!"
		fi
	fi
	;;
  restart)
	if [ "$ENABLED" = "1" ]; then
    	echo -n "Restarting $DESC: $NAME"
		if [ -f "/var/run/atheme/atheme.pid" ]; then
			start-stop-daemon --stop --quiet --pidfile \
				/var/run/atheme/atheme.pid --exec $DAEMON --signal 15
		fi
		sleep 1
		start-stop-daemon -u irc -c irc --start --quiet --pidfile \
			/var/run/atheme/atheme.pid --exec $DAEMON -- $DAEMON_OPTS
		echo "."
	fi
	;;
  *)
	N=/etc/init.d/$NAME
	# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
