#! /bin/sh
#
# jabber-jmc    Jabber mail component
#
# chkconfig:    - 90 10
# description:  JMC is a external component for a Jabber server \
#               which allows to check or retrieve email from POP3/IMAP servers
# processname:  python
# config:       /etc/jmc.conf
# pidfile:      /var/run/jmc/pid

WITHOUT_RC_COMPAT=1

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

PYTHON=/usr/bin/python
EXE=/usr/lib/python2.7/site-packages/jmc/runner.py
CONFIG=/etc/jmc.conf
PIDFILE=/var/run/jmc/pid
LOCKFILE=/var/lock/subsys/jmc
RETVAL=0

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

stop()
{
    action "Stopping JMC component:" \
	start-stop-daemon --stop --pidfile "$PIDFILE" --user _jmc --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 _jmc --displayname jabber-jmc python
    RETVAL=$?
    ;;
    *)
    msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
    RETVAL=1
esac

exit $RETVAL
