#!/bin/sh

### BEGIN INIT INFO
# Provides:          sbnc
# Required-Start:    $local_fs $remote_fs $network $time
# Required-Stop:     $local_fs $remote_fs $network $time
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: An modular IRC proxy.
# Description:       shroudBNC (short: sbnc) is an modular IRC proxy with
#                    features like OpenSSL encrypted connections for the client
#                    and the server, IPv6 and support for scripting extensions.
#                    The daemon is able to handle more than 2000 connections at
#                    the same time.
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin
NAME=/usr/sbin/sbnc
PIDFILE=/var/run/sbnc/sbnc.pid
USER=sbnc
OPTIONS="--config /etc/sbnc/ --log /var/log/sbnc/ --pid $PIDFILE"
. /lib/lsb/init-functions

# See if the daemons are there
test -f ${NAME} || exit 0

# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
if [ -r "/lib/lsb/init-functions" ]; then
	. "/lib/lsb/init-functions"
	. "/lib/init/vars.sh"
else
	echo "Could not read file /lib/lsb/init-functions. Please install lsb-base."
	exit 1
fi

# Check for default file of sbnc.
if [ -f /etc/default/sbnc ] ; then
	. /etc/default/sbnc
else
	echo "Could not read file /etc/default/sbnc."
	exit 1
fi

# Create needed dir in /var/run.
if [ ! -d /var/run/sbnc ] ; then
	mkdir /var/run/sbnc
	chown sbnc:sbnc /var/run/sbnc
	chmod 0760 /var/run/sbnc
fi

case "$1" in
	start)
		if [ "$AUTOSTART_SBNC" != "1" ] ; then
			log_warning_msg "Not starting sbnc, it is disabled in /etc/default/sbnc"
			exit 0
		fi
		echo -n "Starting sbnc daemon:"
		echo -n " sbnc"
		start-stop-daemon --start --background --chuid ${USER} --exec ${NAME} -- $OPTIONS
		echo "."
		;;
	stop)
		echo -n "Stopping sbnc daemon:"
		echo -n " sbnc"
		start-stop-daemon --stop --quiet --chuid ${USER} --exec ${NAME}
		echo "."
		;;
	reload|restart|force-reload)
		if [ "$AUTOSTART_SBNC" != "1" ] ; then
			log_warning_msg "Not starting sbnc, it is disabled in /etc/default/sbnc"
			exit 0
		fi
		echo -n "Restarting sbnc daemon:"
		echo -n " sbnc"
		start-stop-daemon --stop --quiet --chuid ${USER} --exec ${NAME}
		sleep 2
		start-stop-daemon --start --quiet --background --chuid ${USER} --exec ${NAME}
		echo "."
		;;
	status)
        	status_of_proc -p $PIDFILE "sbnc" "sbnc"
        	exit $?
        	;;
        *)
		echo "Usage: $0 {start|stop|restart|reload|force-reload}"
		exit 1
		;;
esac

exit 0
