#!/bin/sh
#
# ejabberd	XMPP server
#
# chkconfig: 	- 70 30
# description:	Fault-tolerant XMPP server.
#
# processname:	so many
# config: 	/etc/ejabberd/ejabberd.cfg
# pidfile: 	nope

WITHOUT_RC_COMPAT=1

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

LOCKFILE=/var/lock/subsys/ejabberd
RETVAL=0

start()
{
    action "Starting ejabberd service: " \
	"su -s /bin/sh -c '/usr/sbin/ejabberdctl start' -l ejabberd"
    RETVAL=$?
    [ "$RETVAL" -ne 0 ] || touch "$LOCKFILE"
    return $RETVAL
}

stop()
{
    action "Stopping ejabberd service: " ejabberdctl stop
    RETVAL=$?
    [ $RETVAL -eq 0 ] || return
    sleep 3
    action "Stopping erlang portmapper: " epmd -kill
    RETVAL=$?
    [ "$RETVAL" -ne 0 ] || rm -f -- "$LOCKFILE"
    return $RETVAL
}

restart()
{
    stop
    sleep 2
    start
}

status()
{
    ejabberdctl status
    RETVAL=$?
    return $RETVAL
}

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
    ;;
    *)
    msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
    RETVAL=1
    ;;
esac

exit $RETVAL
