#!/bin/sh
#
# jabber-jit	This shell script takes care of starting and stopping
#               jabber-jit standalone Jabber component.
#
# chkconfig: - 71 29
# description: ICQ transport for Jabber  \
#
# processname: jabberd-jit
# config: /etc/jabberd2/jabber-jit.xml
# pidfile: /var/run/jabberd2/jabber-jit.pid

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

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

JIT_CONF=/etc/jabberd2/pyicq.xml
LOCKFILE=/var/lock/subsys/jabberd2-pyicq
PIDFILE=/var/lib/jabberd2/pid/pyicq.pid
JIT_USER=jabberd2
#JIT_EXEC=/usr/libexec/jabberd2/jabberd-jit
JIT_EXEC=/usr/sbin/pyicq-t
#JIT_DIR=/usr/shlibexec/pyicq-t
OPTIONS="-D --log /var/log/pyicq/pyicq.log"
# Check that networking is up.
[ $NETWORKING = "no" ] && exit 0

RETVAL=0

dstart()
{
#	"$JIT_EXEC" -D -H "$JIT_DIR" -c "$JIT_CONF"
	start_daemon \
	    --lockfile "$LOCKFILE" \
	    --pidfile "$PIDFILE" \
	    --user "$JIT_USER" \
	    --name pyicq-t \
	    --displayname "$JIT_EXEC" \
 	    -- "$JIT_EXEC" -D -c "$JIT_CONF"\
		"$OPTIONS"
	RETVAL=$?
}

start()
{
        # start daemon
#	    --lockfile "$LOCKFILE" \
	start_daemon \
	    --pidfile "$PIDFILE" \
	    --lockfile "$LOCKFILE" \
	    --user "$JIT_USER" \
	    --name pyicq-t \
	    --displayname PyICQ-t \
	    -- "$JIT_EXEC" -b -c "$JIT_CONF" "$OPTIONS"
	RETVAL=$?
}

stop()
{
	stop_daemon \
	    --pidfile "$PIDFILE" \
	    --lockfile "$LOCKFILE" \
	    --expect-user "$JIT_USER" \
	    --displayname PyICQ-t \
	    -- python
	RETVAL=$?
}

stat()
{
	status \
	    --pidfile "$PIDFILE" \
	    --lockfile "$LOCKFILE" \
	    --expect-user "$JIT_USER" \
	    --displayname PyICQ-t \
	    -- python
	RETVAL=$?
}

reload()
{
	# cause the service configuration to be reread, either with kill -HUP:
#	echo -n "Rereading PyICQ-t configuration: "
#	stop_daemon -HUP \
#	    --pidfile "$PIDFILE" \
#	    --expect-user "$JIT_USER" \
#	    --name pyicq \
#	    -- "/usr/bin/python"
	RETVAL=$?
	# or by simple restarting the daemons in a manner similar to restart above.
}

# See how we were called.
case "$1" in
	dstart)
	dstart
		;;
    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)
      stat
	;;
    *)
	echo "Usage: ${0##*/} {start|stop|reload|restart|condstop|condrestart|status}"
	RETVAL=1

esac

exit $RETVAL
