#!/bin/sh
#
# jserver	This shell script takes care of starting and stopping
#               jabber server.
#
# chkconfig: - 70 30
# description: JABBER  is an instant messaging System.  \
#
# processname: jabberd
# config: /etc/jabber/jabber.xml
# pidfile: /var/run/jabber/jabberd.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

JABBER_CONF=/etc/jabber/jabber.xml
JABBER_HOME=/usr/lib/jabber
JABBER_USER=jabber
LOCKFILE=/var/lock/subsys/jabber
PIDFILE=/var/run/jabber/jabberd.pid

JABBERD=/usr/sbin/jabberd
JADC2S=/usr/sbin/jadc2s

JADC2S_CMDLINE='-c /etc/jabber/jadc2s.xml -b'

# Check that networking is up.
[ $NETWORKING = "no" ] && exit 0

SourceIfExists /etc/jabber/jabber.cfg

CMDLINE=-B
if [ x"$JABBER_SPOOL" != x"" ]; then
	CMDLINE="$CMDLINE -s $JABBER_SPOOL"
fi
if [ x"$JABBER_CONF" != x"" ]; then
        CMDLINE="$CMDLINE -c $JABBER_CONF"
fi
if [ x"$JABBER_HOME" != x"" ]; then
        CMDLINE="$CMDLINE -H $JABBER_HOME"
fi
if [ x"$PIDFILE" != x"" ]; then
        CMDLINE="$CMDLINE -p $PIDFILE"
fi
if [ x"$JABBER_FLAGS" != x"" ]; then
        CMDLINE="$CMDLINE $JABBER_FLAGS"
fi

RETVAL=0

start_jadc2s()
{
        # start jadc2s component (external part)
	if [ x"$START_JADC2S" = x"yes" -a -x "$JADC2S" ]; then
	    start_daemon --user "$JABBER_USER" -- "$JADC2S" $JADC2S_CMDLINE
	    RETVAL=$?
	fi
}

stop_jadc2s()
{
        # stops jadc2s component (external part)
	if [ x"$START_JADC2S" = x"yes" -a -x "$JADC2S" ]; then
	    stop_daemon --expect-user "$JABBER_USER" -- "$JADC2S"
	fi
}

start()
{
	# start daemons
	if [ -f "$PIDFILE" ] &&
	    ! status \
		--pidfile "$PIDFILE" \
		--expect-user "$JABBER_USER" \
		-- "$JABBERD" >/dev/null
	then
	    /bin/rm -f "$PIDFILE"
	fi
	start_daemon \
	    --lockfile "$LOCKFILE" \
	    --user "$JABBER_USER" \
	    --pidfile "$PIDFILE" \
	    -- "$JABBERD" $CMDLINE
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
	    start_jadc2s
	fi
}

stop()
{
	stop_jadc2s
	stop_daemon \
	    --lockfile "$LOCKFILE" \
	    --pidfile "$PIDFILE" \
	    --expect-user "$JABBER_USER" \
	    -- "$JABBERD"
	RETVAL=$?
}

reload()
{
	# cause the service configuration to be reread, either with kill -HUP:
	echo -n "Rereading Jabber configuration: "
	stop_daemon -HUP \
	    --pidfile "$PIDFILE" \
	    --expect-user "$JABBER_USER" \
	    -- "$JABBERD"
	RETVAL=$?
	# or by simple restarting the daemons in a manner similar to restart above.
}


# See how we were called.
case "$1" in
    start)
	start
        ;;
    stop)
	stop
        ;;
    reload)
	reload
	;;
    restart)
	stop
	start
	;;
    condstop)
	# Stop the servce if it is already running, for example:
	if [ -e "$LOCKFILE" ]; then
	    stop
	fi
	;;
    condrestart)
	# Restart the servce if it is already running, for example:
	if [ -e "$LOCKFILE" ]; then
	    stop
	    start
	fi
	;;
    status)
	status \
	    --pidfile "$PIDFILE" \
	    --expect-user "$JABBER_USER" \
	    -- "$JABBERD"
	RETVAL=$?
	;;
    *)
	echo "Usage: ${0##*/} {start|stop|reload|restart|condstop|condrestart|status}"
	RETVAL=1

esac

exit $RETVAL
