#! /bin/sh
#
# postinst script for jwchat
#

set -e

configfile='/etc/jwchat/config.js'
configsource='/usr/share/jwchat/config.js'
apachefile='/etc/apache2/sites-available/jwchat'
apachesource='/usr/share/jwchat/apache.conf'
wwwdir='/usr/share/jwchat/www'
tempfile=$(mktemp)

. /usr/share/debconf/confmodule

# chown everything to nobody:nogroup to make suphp happy
fixperms()
{
	find $wwwdir -type d -exec chmod 0755 {} \;
	find $wwwdir -type f -exec chmod 0644 {} \;
	find $wwwdir -type f -exec chown nobody:nogroup {} \;
}

setup_apache()
{
	db_get jwchat/ApacheServerName
	apache_server_name="$RET"
	sed -e "s|__ApacheServerName__|$apache_server_name|g" $configsource > \
		$tempfile
	ucf --three-way --debconf-ok $tempfile $configfile
	chown www-data:www-data $configfile
	if [ -x /usr/sbin/apache2 -a "$apache_server_name" != "none" ]; then
		db_get jwchat/JabberAddress
		jabber_address="$RET"
		sed -e "s|__ApacheServerName__|$apache_server_name|g" \
		    -e "s|__JabberAddress__|$jabber_address|g" \
		    $apachesource > $tempfile
		ucf --three-way --debconf-ok $tempfile $apachefile
		db_stop
		a2enmod proxy
		a2enmod proxy_http
		a2ensite jwchat
		invoke-rc.d apache2 force-reload
	fi
}

case $1 in
	configure|reconfigure)

	fixperms
	setup_apache
	rm -f $tempfile
	exit 0
	;;

	*)
	echo "postinst called with unknown argument \`$1'" 1>&2
	exit 1
	;;
esac

#DEBHELPER#

