#!/bin/sh
#
# jubjube		An XMPP (Jabber) component for server side logging of XMPP packages
#
# chkconfig: 345 71 29
# description: JubJub is a daemon for server side logging of XMPP packages.
#
# processname: perl
# config:  /etc/jabber-jubjub/jabber-jubjub.xml
# pidfile: /var/run/jabber-jubjub/jubjub.pid
#
### BEGIN INIT INFO
# Provides: jabber-alice
# Required-Start: $network
# Required-Stop:  $network
# Should-Start:   $jabber
# Default-Start:  3 4 5
# Default-Stop:   0 1 2 6
# Short-Description: start and stop Alice XMPP component
# Description: JubJub is a daemon for server side logging of XMPP packages.
#              It based upon module architecture so it is possible to flexible
#              reassign handlers for different types of XMPP packages.
### END INIT INFO


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

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

[ -r /etc/sysconfig/jabber-jubjub ] && . /etc/sysconfig/jabber-jubjub

LOCKFILE=/var/lock/subsys/jabber-jubjub
PIDFILE=/var/run/jabber-jubjub/jubjub.pid
BASEDIR=/usr/share/jabber-jubjub
EXECFILE=$BASEDIR/jubjub.pl

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

RETVAL=0

start()
{
	if [ ! -r "$CONFIG_FILE" ]; then
		echo "JubJub is not configured - no config file $CONFIG_FILE found"
		# LSB 3.1.0: 6 - program is not configured
		RETVAL=6
	fi
	start_daemon \
		--pidfile "$PIDFILE" \
		--lockfile "$LOCKFILE" \
		--user "$USERNAME" \
		--name perl \
		--displayname jabber-jubjub \
		-- "$EXECFILE" --config="$CONFIG_FILE"
	RETVAL=$?
}

stop()
{
	stop_daemon \
		--pidfile "$PIDFILE" \
		--lockfile "$LOCKFILE" \
		--expect-user "$USERNAME" \
		--displayname jabber-jubjub \
		-- perl
	RETVAL=$?
}

stat()
{
	status \
		--pidfile "$PIDFILE" \
		--lockfile "$LOCKFILE" \
		--expect-user "$USERNAME" \
		--displayname jabber-jubjub \
		-- perl
	RETVAL=$?
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop|condstop)
		stop
		;;
	restart|force-reload)
		stop
		start
		;;
	try-restart|condrestart|condreload)
		stat 2&>/dev/null
		[ "$RETVAL" == "0" ] && (stop; start);
		;;
	status)
		stat
		;;
	*)
	msg_usage "${0##*/} {start|stop|restart|status|try-restart|force-reload|condstop|condrestart|condreload|help}"
	RETVAL=1

esac

exit $RETVAL
