#! /bin/sh
#
# jabberd2 init script to start jabberd2 daemon
#
#     Created from Bernd Eckenfels <ecki@lina.inka.de>
#
#     Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#     Modified for Debian GNU/Linux
#     by Ian Murdock <imurdock@gnu.ai.mit.edu>.
#
# Version:  @(#)skeleton  1.8  03-Mar-1998  miquels@cistron.nl
#
# This file was automatically customized by dh-make on Sun, 21 May 2000 12:31:20 +0200

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/jabberd
NAME=jabberd2
DESC=jabberd2
PIDDIR=/var/run/jabber
CONFDIR=/etc/jabberd2
DAEMONPATH=/usr/bin
CMDLINE=""

# test -f $DAEMON || exit 0

# /var/run could be a tmpfs
test -d /var/run/jabber || (mkdir -p /var/run/jabber; chown jabber /var/run/jabber)

LOAD_COMPONENTS=false
DAEMONS=""

# include defaults
if [ -e /etc/default/jabberd2 ]; then
   . /etc/default/jabberd2
fi

#if [ x"$JABBER_HOSTNAME" != x"" ]; then
#   CMDLINE="$CMDLINE -h $JABBER_HOSTNAME"
#fi
#if [ x"$JABBER_SPOOL" != x"" ]; then
#   CMDLINE="$CMDLINE -s $JABBER_SPOOL"
#fi

#if [ x"$CMDLINE" != x"" ]; then
#   CMDLINE="-- $CMDLINE"
#fi

# XXX: Work this out better when things work..
#      We really should use jabberd.cfg to get the daemons and their configs
# DAEMONS="router resolver sm s2s c2s"

set -e

case "$1" in
   start)
      echo -n "Starting $DESC:"
      for i in $DAEMONS; do
      echo -n " $i"
      start-stop-daemon -b -c jabber:adm --start --quiet \
         --pidfile $PIDDIR/$i.pid --exec $DAEMONPATH/$i -- $CONFDIR/$i.xml $CMDLINE
      done
      echo "."
      if [ -d /etc/jabber/jabber.d -a x$LOAD_COMPONENTS = xtrue ]; then
         run-parts --arg=$1 /etc/jabber/jabber.d
      else
         echo "Not loading components."
      fi
      ;;
   stop)
      ERROR=0
      if [ -d /etc/jabber/jabber.d -a x$LOAD_COMPONENTS = xtrue ]; then
         run-parts --arg=$1 /etc/jabber/jabber.d
      fi
      echo -n "Stopping $DESC:"
      for i in $DAEMONS; do
      echo -n " $i"
      start-stop-daemon -o --stop --retry 5 --quiet --pidfile $PIDDIR/$i.pid \
           --exec $DAEMONPATH/$i || ( echo -n "<Failed>" && ERROR=1 )
      done
      echo "."
      if [ $ERROR -eq 1 ]; then exit 1; fi
      ;;
   restart)
      $0 stop
      sleep 3
      $0 start
      ;;
   *)
      N=/etc/init.d/$NAME
      echo "Usage: $N {start|stop|restart}" >&2
      exit 1
      ;;
esac

exit 0
