#!/bin/sh

# jabber-config: automatic deployment for Jabber servers and components
# Copyright (C) 2007-2008  Mikhail Yakshin <greycat@altlinux.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

SERVER_DIR=/usr/share/jabber/server
COMPONENT_DIR=/usr/share/jabber/component

warn()
{
	echo "jabber-config: $@" >&2
}

verbose()
{
	[ -n "$verbose" ] && echo "jabber-config: $@"
}

show_usage()
{
	cat <<__EOF__
$0: automatic deployment for Jabber servers and components

Usage: $0 [options]

-v, --verbose   show verbose information on actions performed
-h, --help      show this usage information
-V, --version   show version information
__EOF__
}

show_version()
{
	cat <<__EOF__
$0 version 0.3
Written by Mikhail Yakshin <greycat@altlinux.org>

Copyright (C) 2007-2008  Mikhail Yakshin <greycat@altlinux.org>
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

__EOF__
}

verbose=

TEMP=$(getopt -n "$0" -o h,V,v -l help,version,verbose -- "$@") || { show_usage; exit 1; }
eval set -- "$TEMP"

while :; do
	case "$1" in
	--help|-h) show_usage; exit 0
		;;
	--version|-V) show_version; exit 0
		;;
	--verbose|-v) verbose=1
		;;
	--) shift; break
		;;
	esac
	shift
done

for S in $SERVER_DIR/*; do
	server=$(basename $S)
	[ "$server" = '*' ] && continue
	verbose "autoconfiguring server $server..."
	if [ ! -x "$S" ]; then
		warn "server $server adapter is non-executable, skipping"
		continue
	fi
	policy=$("$S" --policy)
	if [ $? != 0 ]; then
		warn "server $server adapter is broken, skipping"
		continue
	fi
	verbose "server's policy is '$policy'"
	case "$policy" in
	multi)
		for C in $COMPONENT_DIR/*; do
			component=$(basename $C)
			[ "$component" = '*' ] && continue
			verbose "	autoconfiguring component $component..."
			if [ ! -x "$C" ]; then
				warn "component $component adapter is non-executable, skipping"
				continue
			fi

			port=$("$C" --port)
			if [ $? != 0 ] || [ -z "$port" ]; then
				warn "component $component adapter is broken - bad port '$port', skipping"
				continue
			fi

			host=$("$C" --host)
			if [ $? != 0 ] || [ -z "$host" ]; then
				warn "component $component adapter is broken - bad host '$host', skipping"
				continue
			fi

			password=$("$C" --password)
			if [ $? != 0 ] || [ -z "$password" ]; then
				warn "component $component adapter is broken - bad password, skipping"
				continue
			fi

			verbose "		setting server's port to $port, host to '$host' and password"
			"$S" "--port=$port" "--host=$host" "--password=$password" ||
				warn "server $server adapter is broken - unable to register component $component"
			verbose "		done"
		done
		;;
	single)
		port=$("$S" --port)
		if [ $? != 0 ] || [ -z "$port" ]; then
			warn "server $server adapter is broken - bad port '$port', skipping"
			continue
		fi

		password=$("$S" --password)
		if [ $? != 0 ] || [ -z "$password" ]; then
			warn "server $server adapter is broken - bad password, skipping"
			continue
		fi

		for C in $COMPONENT_DIR/*; do
			[ "$C" = '*' ] && continue
			component=$(basename $C)
			verbose "	autoconfiguring component $component..."
			if [ ! -x "$C" ]; then
				warn "component $component adapter is non-executable, skipping"
				continue
			fi
			verbose "		setting component's port to $port and password"
			"$C" "--set-port=$port" "--set-password=$password" ||
				warn "component $component adapter is broken - unable to register server $server"
			verbose "		done"
		done
		;;
	*)
		warn "server $server adapter is broken - unknown policy '$policy', skipping"
	esac
done
