#!/bin/sh

set -e

. /usr/share/debconf/confmodule
. /usr/share/dbconfig-common/dpkg/postinst

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>

lighttpd_install() {
        if [ ! -f /etc/lighttpd/conf-available/50-smbind.conf ] ; then
                if [ ! -x /usr/sbin/lighty-enable-mod ] ; then
                        echo "Lighttpd not installed, skipping"
                else
                        ln -s /etc/smbind/lighttpd.conf /etc/lighttpd/conf-available/50-smbind.conf
                        lighty-enable-mod smbind
                fi
        fi
}

apache_install() {
        webserver=$1
        if [ -d /etc/$webserver/conf.d ] && [ ! -e /etc/$webserver/conf.d/smbind ]; then
               ln -s /etc/smbind/apache.conf /etc/$webserver/conf.d/smbind
        fi
}

case "$1" in
    configure)

	dbc_generate_include=php:/etc/smbind/config-db.php
	# Make sure the passwords match
	if [ -z "$2" ]; then
		local pass1 pass2
		while :; do
			db_input high smbind/password1 || true
			db_go
			db_get smbind/password1
			pass1="$RET"
			db_input high smbind/password2 || true
			db_go
			db_get smbind/password2
			pass2="$RET"
			if [ "$pass1" = "$pass2" ]; then
				break
			fi

			db_fset smbind/password1 seen false
			db_fset smbind/password2 seen false
			db_fset smbind/password_mismatch seen false
			db_input critical smbind/password_mismatch
			db_go
		done
		password="`echo -n $pass1 | md5sum | cut -d' ' -f1`"
		tempfile=$(mktemp -p /tmp smbind.XXXXXXXX)
		cat /usr/share/dbconfig-common/data/smbind/install/mysql | sed -s s/21232f297a57a5a743894a0e4a801fc3/$password/ > $tempfile
		cat $tempfile > /usr/share/dbconfig-common/data/smbind/install/mysql
		cat /usr/share/dbconfig-common/data/smbind/install/pgsql | sed -s s/21232f297a57a5a743894a0e4a801fc3/$password/ > $tempfile
		cat $tempfile > /usr/share/dbconfig-common/data/smbind/install/pgsql
		rm $tempfile
	fi
	dbc_go smbind $@
	chown -R www-data:www-data /usr/share/smbind/php
	chown -R www-data:bind /etc/smbind
	chown -R www-data:www-data /etc/smbind/smbind.conf
	chmod 774 /etc/smbind/zones/
	chown www-data:www-data /etc/smbind/config-db.php
	chmod 640 /etc/smbind/config-db.php
	db_get smbind/reconfigure-webserver || true
        webservers="$RET"
        restart=""
	
	for webserver in $webservers; do
            webserver=${webserver%,}
            if [ "$webserver" = "lighttpd" ] ; then
                lighttpd_install
            else
                apache_install $webserver
            fi
            test -x /usr/sbin/$webserver || continue
            restart="$restart $webserver"
        done
	
	db_get smbind/restart-webserver || true
        res="$RET"
        db_stop || true
        if [ "$res" = "true" ]; then
            for webserver in $restart; do
                webserver=${webserver%,}
                # Redirection of 3 is needed because Debconf uses it and it might
                # be inherited by webserver. See bug #446324.
                if [ -x /usr/sbin/invoke-rc.d ]; then
                    invoke-rc.d $webserver reload 3>/dev/null || true
                else
                    /etc/init.d/$webserver reload 3>/dev/null || true
                fi
            done
        fi
	

        ;;

    abort-upgrade|abort-remove|abort-deconfigure)

        ;;

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

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

