#!/bin/sh
### BEGIN INIT INFO
# Provides:          ircd-irc2
# Required-Start:    $network $remote_fs
# Required-Stop:     $network $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      
# Short-Description: Starts/stops the irc daemon
# Description: Provide IRC service with the IRC daemon
### END INIT INFO

set -e 

. /lib/lsb/init-functions

# $PATH to go
PATH=/sbin:/bin:/usr/sbin:/usr/bin
          
# where the irc-daemon is
IRCD=/usr/sbin/ircd
NAME=ircd
PIDFILE=/var/run/ircd/ircd.pid                  

check_pid_dir() {
	if [ ! -d /var/run/ircd ]; then
		mkdir /var/run/ircd
		chown irc:irc /var/run/ircd
		chmod 775 /var/run/ircd
	fi
}


# Gracefully exit if the package has been removed.
test -x $IRCD || exit 0

status_ircd() {
	status_of_proc -p "${PIDFILE}" "${IRCD}" "${NAME}"
}

case "$1" in
       
       start)
		check_pid_dir
                       echo -n "Starting irc server daemon:"
               echo -n " ${NAME}"
               start-stop-daemon --start --quiet --pidfile ${PIDFILE} \
                       --chuid irc --exec ${IRCD} --oknodo
               echo "."
               ;;
 
       stop)
               echo -n "Stopping irc server daemon:"
               echo -n " ${NAME}"
               start-stop-daemon --stop --quiet --oknodo \
                       --pidfile ${PIDFILE} --exec ${IRCD}
               echo "."  
               ;;
       status)
	       status_ircd
               ;;

       reload|force-reload)
               echo -n "Reloading irc server daemon:"
               echo -n " ${NAME}"
               start-stop-daemon --stop --signal 1 --quiet \
                       --pidfile ${PIDFILE} --exec ${IRCD}
               echo "."
               ;;

       restart)
		check_pid_dir
               echo -n "Restarting irc server daemon:"
               echo -n " ${NAME}"
               start-stop-daemon --stop --quiet --oknodo \
                       --pidfile ${PIDFILE} --exec ${IRCD}
               sleep 1
               start-stop-daemon --start --quiet --pidfile ${PIDFILE} \
                       --chuid irc --exec ${IRCD}
               echo "."
               ;;
       *)
               echo "Usage: $0 {start|stop|status|restart|reload|force-reload}"
               exit 1
               ;;
esac

exit 0
