#!/bin/bash
#
# Raymond 25DEC2003 support@bigriverinfotech.com
# /etc/rc.d/init.d/jabberd2
# init script for jabberd2 processes
# Tested under jabberd-2.0rc2 and Fedora 1.0 only
#
# processname: jabberd2
# description: jabberd2 is the next generation of the jabberd server
# chkconfig: 2345 85 15
#
if [ -f /etc/init.d/functions ]; then
	. /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ]; then
	. /etc/rc.d/init.d/functions
else
	echo -e "\ajabberd2: unable to locate functions lib. Cannot continue."
	exit -1
fi
#
progs="router sm c2s s2s"
progsPath="/usr/local/bin"
confPath="/usr/local/etc/jabberd"
pidPath="/usr/local/var/jabberd/pid"
statusCol="echo -ne \\033[60G"
statusColorOK="echo -ne \\033[1;32m"
statusColorFailed="echo -ne \\033[1;31m"
statusColorNormal="echo -ne \\033[0;39m"
retval=0
#
StatusOK ( ) {
	${statusCol}
	echo -n "[  "
	${statusColorOK}
	echo -n "OK"
	${statusColorNormal}
	echo "  ]"
	return 0
}
#
StatusFailed ( ) {
	echo -ne "\a"
	${statusCol}
	echo -n "["
	${statusColorFailed}
	echo -n "FAILED"
	${statusColorNormal}
	echo "]"
	return 0
}
#
ReqBins ( ) {
	for prog in ${progs}; do
		if [ ! -x ${progsPath}/${prog} ]; then
			echo -n "jabberd2 binary [${prog}] not found."
			StatusFailed
			echo "Cannot continue."
			return -1
		fi
	done
	return 0
}
#
ReqConfs ( ) {
	for prog in ${progs}; do
		if [ ! -f ${confPath}/${prog}.xml ]; then
			echo -n "jabberd2 configuration [${prog}.xml] not found."
			StatusFailed
			echo "Cannot continue."
			return -1
		fi
	done
	return 0
}
#
ReqDirs ( ) {
	if [ ! -d ${pidPath} ]; then
		echo -n "jabberd2 PID directory not found. Cannot continue."
		StatusFailed
		return -1
	fi
	return 0
}
#
Start ( ) {
	for req in ReqBins ReqConfs ReqDirs; do
		${req}
		retval=$?
		[ ${retval} == 0 ] || return ${retval}
	done
	echo "Initializing jabberd2 processes ..."
	for prog in ${progs}; do
		if [ $( pidof -s ${prog} ) ]; then
			echo -ne "\tprocess [${prog}] already running"
			StatusFailed
			sleep 1
			continue
		fi
		echo -ne "\tStarting ${prog}: "
		if [ ${prog} == "router" ]; then
			ports="5347"
		elif [ ${prog} == "c2s" ]; then
			ports="5222 5223"
		elif [ ${prog} == "s2s" ]; then
			ports="5269"
		else
			ports=""
		fi
		for port in ${ports}; do
			if [ $( netstat --numeric-ports --listening --protocol=inet |
	 				gawk '{ print $4 }' |
						gawk -F : '{ print $NF }' |
							grep -c ${port}$ ) -ne "0" ]; then
				StatusFailed
				echo -e "\tPort ${port} is currently in use. Cannot continue"
				echo -e "\tIs a Jabber 1.x server running?"
				Stop
				let retval=-1
				break 2
			fi
		done		
		rm -f /var/lock/subsys/${prog}
		rm -f ${pidPath}/${prog}.pid
		args="-c ${confPath}/${prog}.xml"
		${progsPath}/${prog} ${args} >/dev/null 2>&1 <&1 &
		retval=$?
		if [ ${retval} == 0 ]; then
			StatusOK
			touch /var/lock/subsys/${prog}
		else
			StatusFailed
			Stop
			let retval=-1
			break
		fi
		sleep 1
	done
	return ${retval}
}
#
Stop ( ) {
	echo "Terminating jabberd2 processes ..."
	for prog in ${progs}; do
		echo -ne "\tStopping ${prog}: "
		killproc ${prog}
		retval=$?
		if [ ${retval} == 0 ]; then
			rm -f /var/lock/subsys/${prog}
			rm -f ${pidPath}/${prog}.pid
		fi
		echo
		sleep 1
	done
	return ${retval}
}
#
case "$1" in
	start)
		Start
		;;
	stop)
		Stop
		;;
	restart)
		Stop
		Start
		;;
	condrestart)
		if [ -f /var/lock/subsys/${prog} ]; then
			Stop
			sleep 3
			Start
		fi
		;;
	*)
		echo "Usage: $0 {start|stop|restart|condrestart}"
		let retval=-1
esac
exit ${retval}
#
# eof
