#! /bin/sh

# ircd-ratbox	Start/stop the ratbox IRC server.

# This file is based on debian's ircd-hybrid init script
# Version:	ircd-hybrid  7.0rc9-1  03-Mar-2003  joshk@triplehelix.org
# Version:	ircd-hybrid  7.2.2-2   10-Sep-2006  ag@roxor.cx
# Version:	ircd-ratbox  2.2.6-1   21-Aug-2007  acornet@debian.org

### BEGIN INIT INFO
# Provides:          ircd-ratbox
# Required-Start:    $syslog
# Required-Stop:     $syslog
# Should-Start:      $local_fs $network $named
# Should-Stop:       $local_fs $network $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: ircd-ratbox daemon init.d script
# Description:       Control ircd-ratbox IRC server daemon.
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/ircd-ratbox
DEFAULT=/etc/default/ircd-ratbox
NAME="ircd-<%= name %>"
DESC="ratbox IRC Server"
BASEDIR="/var/lib/ratbox/<%= name %>"
ENABLED=1

test -f $DAEMON || exit 0

if [ -e $DEFAULT ]; then
	. $DEFAULT
fi

test "$ENABLED" != "0" || exit 0

[ -f /etc/default/rcS ] && . /etc/default/rcS
. /lib/lsb/init-functions

set -e

ratbox_start()
{
	if [ ! -d /var/run/ircd ] ; then
		mkdir /var/run/ircd
		chown irc:irc /var/run/ircd
	fi
	if [ -f /var/run/ircd/$NAME.pid ] ; then
		echo -n " already running"
		return 0
	fi
	start-stop-daemon --start --quiet \
		-u irc -c irc --pidfile /var/run/ircd/$NAME.pid \
		--exec $DAEMON -- \
		-basedir $BASEDIR -configfile $BASEDIR/ircd.conf -logfile /var/log/ircd-ratbox/$NAME.log -pidfile /var/run/ircd/$NAME.pid > /dev/null \
		|| return 2
	return 0
}

ratbox_stop()
{
	start-stop-daemon --oknodo --stop --quiet \
		--pidfile /var/run/ircd/$NAME.pid \
		--signal 15 --exec $DAEMON -- -pidfile /var/run/ircd/$NAME.pid \
		|| return 2
	return 0
}

ratbox_reload()
{
	if [ -f "/var/run/ircd/$NAME.pid" ]; then
		kill -s HUP $(cat /var/run/ircd/$NAME.pid)
		return 0
	else
		return 2
	fi
}

case "$1" in
start)
	log_daemon_msg "Starting $NAME" "$NAME"
	ratbox_start
	case "$?" in
	0) log_end_msg 0 ;;
	1|2) log_end_msg 1 ;;
	esac
	;;
stop)
	log_daemon_msg "Stopping $NAME" "$NAME"
	ratbox_stop
	case "$?" in
	0|1) log_end_msg 0 ;;
	2) log_end_msg 1 ;;
	esac
	;;
reload)
	log_daemon_msg "Reloading $NAME" "$NAME"
	ratbox_reload
	case "$?" in
	0|1) log_end_msg 0 ;;
	2) log_end_msg 1 ;;
	esac
	;;
restart|force-reload)
	log_daemon_msg "Restarting $NAME" "$NAME"
	ratbox_stop
	sleep 2
	ratbox_start
	case "$?" in
	0) log_end_msg 0 ;;
	1|2) log_end_msg 1 ;;
	esac
	;;
*)
	echo "Usage: $0 {start|stop|restart|reload|force-reload}" >&2
	exit 1
	;;
esac

exit 0

