#!/bin/sh

# jive-messengerd
#
# chkconfig: 2345 20 80
# description: Used to start and stop the jive messenger XMPP server
# 
# Script used to start jive messenger as daemon
# The script has currently been tested on Redhat Fedora Core 3,
# but should theoretically work on most UNIX like systems
#
# before running this script make sure $MESSENGER_HOME/bin/messenger is
# executable by the user you want to run messenger as
# (chmod +x $MESSENGER_HOME/bin/messenger)
#
# This script should be copied into /etc/init.d and linked into
# your default runlevel directory.
# You can find your default runlevel directory by typing: 
# grep default /etc/inittab
#
# Link to the directory like follows
# cd /etc/rc<num>.d
# ln -s ../init.d/jive-messengerd $90jive-messengerd
#

# Set this to tell this script where messenger lives
# If this is not set the script will look for /opt/jive_messenger, then /usr/local/jive_messenger
#export MESSENGER_HOME=

# If there is a different user you would like to run this script as,
# change the following line
export MESSENGER_USER=jive

# -----------------------------------------------------------------

# If a messenger home variable has not been specified, try to determine it
if [ ! $MESSENGER_HOME ]; then 
	if [ -d "/opt/jive_messenger" ]; then
		MESSENGER_HOME="/opt/jive_messenger"
	elif [ -d "/usr/local/jive_messenger" ]; then
		MESSENGER_HOME="/usr/local/jive_messenger"
	else
		echo "Could not find Jive Messenger installation under /opt or /usr/local" 
		echo "Please specify the Jive Messenger installation location in environment variable MESSENGER_HOME"
		exit 1
	fi
fi


function execCommand() {
	OLD_PWD=`pwd`
	cd $MESSENGER_HOME/bin
	CMD="./messenger $1"
	su -c "$CMD" $MESSENGER_USER &
	cd $OLD_PWD
}


start() {
	execCommand "start"
}

stop() {
	execCommand "stop"
}


case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	*)
		echo "Usage $0 {start|stop}"
		exit 1
esac

exit 0