#!/bin/sh
#
# spectrum (this script) - manage multiple spectrum (XMPP Transport) instances
# Copyright (C) 2009 Thilo Cestonaro <thilo@cestona.ro> and 
# Mathias Ertl <mati@fsinf.at>
# 
# Distributable under the terms of the GNU GPL version 2.
#

### BEGIN INIT INFO
# Provides:             spectrum
# Required-Start:       $network $local_fs $remote_fs
# Required-Stop:
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Spectrum XMPP Transport Service
### END INIT INFO

SPECTRUM_USER=spectrum
SPECTRUM_CONFIG_DIR=/etc/spectrum/
SPECTRUM_DEBUG=no

test -f /etc/default/spectrum && . /etc/default/spectrum

# Check that user exists
check_user() {
	if ! getent passwd $SPECTRUM_USER >/dev/null; then
		exit 1;
	fi
}

. /lib/lsb/init-functions

start() {
	if [ "$SPECTRUM_DEBUG" = "yes" ]; then
		ctl_args="--debug"
	fi

	for i in $SPECTRUM_CONFIG_DIR/*.cfg ; do
		CONF=`basename $i`
		log_daemon_msg "Starting spectrum instance:" "$CONF"
		output=$(spectrumctl $ctl_args -c $i start)
		if [ "$?" -ne 0 ]; then
			echo $output
		fi
		log_end_msg $?
	done
}

stop() {
	for i in $SPECTRUM_CONFIG_DIR/*.cfg ; do
		CONF=`basename $i`
		log_daemon_msg "Stopping spectrum instance:" "$CONF"
		spectrumctl -q -c $i stop
		log_end_msg $?
	done
}

reload() {
	for i in $SPECTRUM_CONFIG_DIR/*.cfg ; do
		CONF=`basename $i`
		log_daemon_msg "Reloading spectrum instance:" "$CONF"
		spectrumctl -q -c $i reload
		log_end_msg $?
	done
}

case "$1" in
	start)
		check_user
		start
		;;
	stop)
		stop
		;;
	restart)
		stop
		start
		;;
	reload)
		reload
		;;
	force-reload)
		reload
		;;
	*)
		log_action_msg "Usage: /etc/init.d/spectrum {start|stop|restart|reload|force-reload}"
		exit 1
esac
		  
exit 0;
