# --- T2-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# 
# T2 SDE: package/.../jabberd/jabberd.conf
# Copyright (C) 2004 - 2006 The T2 SDE Project
# 
# More information can be found in the files COPYING and README.
# 
# 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; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- T2-COPYRIGHT-NOTE-END ---

# Unfortunately jabberd uses different package names for the same
# packages. So we need a way to convert T2 package names to jabberd
# package names. Just pass the T2 package name and the jabberd 
# packaage name will be returned.
get_jabberd_package_name() {
	case "$1" in
		postgresql)	echo "pgsql" ;;
		bdb)		echo "db" ;;
		openldap)	echo "ldap" ;;
		openssl)	echo "ssl" ;;
		libidn)		echo "idn" ;;
		*)		echo "$1" ;;
	esac
}

# Jabber wants its own directory with the proper group rights.
prefix=opt/jabber
set_confopt

extraincdir="$(pkgprefix includedir glibc)"
extralibdir="$(pkgprefix libdir glibc)" 

# Add support for the following packages if they are available.
for pkgname in openssl libidn postgresql mysql bdb pam openldap; do
	# unfortunately the configure option sometimes uses different names.
	confpkgname=$(get_jabberd_package_name $pkgname)
	# Is this package available?
	if pkginstalled $pkgname; then
		# Yuck, mysql header files and libs can not be found in the
		# normal include and lib dir, but in include dir / mysql.
		include_dir=$(pkgprefix includedir $pkgname)
		library_dir=$(pkgprefix libdir $pkgname)
		if [ "$pkgname" == mysql ]; then
			include_dir="$include_dir/mysql"
			library_dir="$library_dir/mysql"
		fi

		# Include this package properly in the configure options.
		var_append extraconfopt " " "--enable-$confpkgname"
		var_append extraincdir ":" "$include_dir" 
		var_append extralibdir ":" "$library_dir" 
	else
		var_append extraconfopt " " "--disable-$confpkgname"
	fi
done

# Add additional include dirs if available.
if [ -n $extraincdir ]; then
	# Now add the extra include dirs to the configure options.
	var_append extraconfopt " " "--with-extra_include_path=$extraincdir"
fi

# Same for additional library dirs.
if [ -n $extralibdir ]; then
	# Now add the extra library dirs to the configure options.
	var_append extraconfopt " " "--with-extra_library_path=$extralibdir"
fi

jabber_preconf() {
	# Make sure the prefix directory has the proper access rights.
	chgrp jabber $root/$prefix 
	chmod a+rx,g+ws $root/$prefix 

	# Also ensure the state dir has the proper access rights.
	chgrp jabber $localstatedir 
	chmod a+rx,g+ws $localstatedir 
}

hook_add preconf 5 "jabber_preconf"