#! /bin/sh

# ratbox-services-pgsql	Start/stop the ratbox services.

# 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:	ratbox-services  2.2.6-1   21-Aug-2007  acornet@debian.org
# Version:	ratbox-services 1.1.2-1 28-Aug-2007  acornet@debian.org

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

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/ratbox-services
DEFAULT=/etc/default/ratbox-services
NAME="ratbox-services-<%= name %>"
DESC="ratbox IRC Services"
BASEDIR="/var/lib/ratbox-services/<%= name %>"
UID=$(id -u irc)
GID=$(id -g irc)
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 $BASEDIR/var/run/ratbox-services ] ; then
		mkdir -p $BASEDIR/var/run/ratbox-services
		chown irc:irc $BASEDIR/var/run/ratbox-services
	fi
	start-stop-daemon --start --quiet \
		--pidfile $BASEDIR/var/run/ratbox-services/ratbox-services.pid \
		--exec $DAEMON -- \
		-g $GID -u $UID -r $BASEDIR > /dev/null \
		|| return 2
	return 0
}

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

case "$1" in
start)
	log_daemon_msg "Starting ratbox-services" "ratbox-services"
	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
	;;
restart|force-reload|reload)
	log_daemon_msg "Restarting $NAME" "$NAME"
	ratbox_stop
	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

