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

set -e 

# $PATH to go
PATH=/sbin:/bin:/usr/sbin:/usr/bin
          
# where the irc-daemon is
IRCD=/usr/sbin/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

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

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

       restart)
		check_pid_dir
               echo -n "Restarting irc server daemon:"
               echo -n " ircd"
               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|restart|reload|force-reload}"
               exit 1
               ;;
esac

exit 0
