#! /bin/sh
#
# jabber-j2j    XMPP transport for jabber servers.
#
# chkconfig:    - 90 10
# description:  XMPP transport is a external component for a Jabber server \
#               which allows to aggregate contsacts from several jabber accounts
# processname:  python
# config:       /etc/j2j.conf
# pidfile:      /var/lib/jabber-j2j/pid

WITHOUT_RC_COMPAT=1

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

PYTHON=/usr/bin/python
EXE=/usr/lib64/j2j/main.py
PIDFILE=/var/lib/jabber-j2j/pid
LOCKFILE=/var/lock/subsys/j2j
RETVAL=0

start()
{
    if start-stop-daemon --start --test --quiet --pidfile "$PIDFILE" \
	--user _j2j --exec "$PYTHON" >/dev/null; then
	action "Starting J2J component:" \
	    start-stop-daemon --start --pidfile "$PIDFILE" --make-pidfile \
	    --background --chuid _j2j --exec "$PYTHON" -- "$EXE"
    else
	msg_already_running jabber-j2j
	passed "jabber-j2j startup"
	echo
    fi
    RETVAL=$?
    [ "$RETVAL" -ne 0 ] || touch "$LOCKFILE"
    return $RETVAL
}

stop()
{
    action "Stopping J2J component:" \
	start-stop-daemon --stop --pidfile "$PIDFILE" --user _j2j --exec "$PYTHON"
    RETVAL=$?
    [ "$RETVAL" -ne 0 ] || rm -f -- "$LOCKFILE" "$PIDFILE"
    return $RETVAL
}

restart()
{
    stop
    start
}

# See how we were called.
case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    restart|reload)
    restart
    ;;
    condstop)
    if [ -e "$LOCKFILE" ]; then
	stop
    fi
    ;;
    condrestart|condreload)
    if [ -e "$LOCKFILE" ]; then
	restart
    fi
    ;;
    status)
    status --pidfile "$PIDFILE" --expect-user _j2j --displayname jabber-j2j python
    RETVAL=$?
    ;;
    *)
    msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
    RETVAL=1
esac

exit $RETVAL
