#! /bin/sh
# $FreeBSD: ports/net-im/ejabberd/files/ejabberd.sh.in,v 1.4 2006/05/08 12:37:38 garga Exp $

# PROVIDE: ejabberd
# REQUIRE: DAEMON
# BEFORE: LOGIN
# KEYWORD: shutdown

# Define these ejabberd_* variables in one of these files:
#       /etc/rc.conf
#       /etc/rc.conf.local
#       /etc/rc.conf.d/ejabberd
#
# DO NOT CHANGE THESE DEFAULT VALUES HERE
#
ejabberd_enable="${ejabberd_enable-NO}"

. %%RC_SUBR%%

name=ejabberd
rcvar=`set_rcvar`

reload_cmd="ejabberd_reload"
restart_cmd="ejabberd_reload"
start_cmd="ejabberd_start"
status_cmd="ejabberd_status"
stop_cmd="ejabberd_stop"

cd %%PREFIX%%		# Why is this needed!?

# Include ejabberd defaults if available
[ -f %%PREFIX%%/etc/ejabberd/ejabberd.defaults ] && . %%PREFIX%%/etc/ejabberd/ejabberd.defaults

PATH=/sbin:/bin:/usr/sbin:/usr/bin:%%PREFIX%%/bin:%%PREFIX%%/sbin
EJABBERD=%%PREFIX%%/bin/ejabberd
EJABBERDCTL=%%PREFIX%%/bin/ejabberdctl
EJABBERDUSER=ejabberd

ejabberd_status()
{
    su $EJABBERDUSER -c "$EJABBERDCTL ejabberd@`hostname -s` status >/dev/null"
}

ejabberd_start()
{
    echo -n "Starting $name: "
    su $EJABBERDUSER -c "$EJABBERD -s -noshell -detached"
    echo "$name."
}

ejabberd_stop()
{
    echo -n "Stopping $name: "
    if su $EJABBERDUSER -c "$EJABBERDCTL ejabberd@`hostname -s` stop"; then
        sleep 2
        killall -u ejabberd -kill
    else
        echo -n " failed "
    fi
    echo "$name."
}

ejabberd_reload()
{
    echo -n "Restarting $name: "
    if ejabberd_status; then
        su $EJABBERDUSER -c "$EJABBERDCTL ejabberd@`hostname -s` restart"
    else
        ejabberd_start
    fi
    echo "$name."
}

load_rc_config $name
run_rc_command "$1"
