#!/bin/sh -e

CONFIG=/etc/dbmail/dbmail.conf
CONFIG_EX=/usr/share/doc/dbmail/examples/dbmail.conf

DEBIAN_CONFIG=/etc/default/dbmail
DEBIAN_CONFIG_EX=/usr/share/doc/dbmail/examples/default.dbmail

test $DEBIAN_SCRIPT_DEBUG && set -v -x

# 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>
# for details, see /usr/doc/packaging-manual/
#
# quoting from the policy:
#     Any necessary prompting should almost always be confined to the
#     post-installation script, and should be protected with a conditional
#     so that unnecessary prompting doesn't happen if a package's
#     installation fails and the `postinst' is called with `abort-upgrade',
#     `abort-remove' or `abort-deconfigure'.

case "$1" in
    install|upgrade)
    ;;
    configure)

	. /usr/share/debconf/confmodule
	db_get dbmail/do_debconf || true; DO_DEBCONF=$RET
	db_get dbmail/dbmail/authdriver || true; AUTHDRIVER=$RET
	
	if [ "$DO_DEBCONF" = "true" ]; then
		# fetch debconf values
		db_get dbmail/dbmail/host || true; DB_HOST=$RET
		db_get dbmail/dbmail/db || true; DB_DATABASE=$RET
		db_get dbmail/dbmail/user || true; DB_USER=$RET
		db_get dbmail/dbmail/pass || true; DB_PASS=$RET

		db_get dbmail/dbmail/postmaster || true; POSTMASTER=$RET
	
		db_get dbmail/start_imapd || true; START_IMAPD=$RET
		db_get dbmail/start_lmtpd || true; START_LMTPD=$RET
		db_get dbmail/start_pop3d || true; START_POP3D=$RET
		db_get dbmail/start_sieve || true; START_SIEVE=$RET

		if [ "$AUTHDRIVER" = "ldap" ]; then
			db_get dbmail/ldap/hostname || true; LDAP_HOST=$RET
			db_get dbmail/ldap/post || true; LDAP_PORT=$RET
			db_get dbmail/ldap/base_dn || true; LDAP_BASE_DN=$RET
			db_get dbmail/ldap/bind_dn || true; LDAP_BIND_DN=$RET
			db_get dbmail/ldap/bind_pw || true; LDAP_BIND_PW=$RET
			db_get dbmail/ldap/bind_anonymous || true; LDAP_BIND_ANONYMOUS=$RET
			db_get dbmail/ldap/field_uid || true; LDAP_UID=$RET
			db_get dbmail/ldap/field_cid || true; LDAP_CID=$RET

			if [ -z "$LDAP_BASE_DN" ]; then
				# For the domain really.argh.org we create the basedn
				# dc=really,dc=argh,dc=org with the dc entry dc: really
				db_get slapd/domain && SLAPD_DOMAIN=$RET
				if [ -n "$SLAPD_DOMAIN" ]; then
					LDAP_BASE_DN="dc=`echo $SLAPD_DOMAIN|sed 's/\./,dc=/g'`"
				fi
			fi

			if [ -n "$LDAP_BASE_DN" ] && [ -z "$LDAP_BIND_DN" ] && [ "$LDAP_BIND_ANONYMOUS" = "false" ]; then
				LDAP_BIND_DN="cn=dbmail,$LDAP_BASE_DN";
				db_set dbmail/ldap/bind_dn "$LDAP_BIND_DN"
				if [ -x /usr/bin/pwgen ]; then
					LDAP_BIND_PW=`pwgen -n`
					db_set dbmail/ldap/bind_pw "$LDAP_BIND_PW"
				fi
			fi

			if [ -z $LDAP_UID ]; then
				LDAP_UID="uid"
			fi

			if [ -z $LDAP_CID ]; then
				LDAP_CID="gidNumber"
			fi
		fi

	fi
	
	# protect the config-file
	oldmask=`umask`
	umask 026
	
	if [ -e "${CONFIG_EX}.gz" ]; then
		gunzip ${CONFIG_EX}.gz 2>/dev/null || true
	fi

	if [ -e "${DEBIAN_CONFIG_EX}.gz" ]; then
		gunzip ${DEBIAN_CONFIG_EX}.gz 2>/dev/null || true
	fi

	if [ ! -e "$CONFIG" ]; then	
		ucf --debconf-ok $CONFIG_EX $CONFIG
		# activate the sqlite driver by default 
		sed -i 's/\(^driver\W*=\)\(\W*$\)/\1 sqlite/' $CONFIG
	fi

	if [ ! -d "$DEBIAN_CONFIG" ]; then
		ucf --debconf-ok $DEBIAN_CONFIG_EX $DEBIAN_CONFIG
	fi

	if [ "$DO_DEBCONF" = "true" ] && [ -e "$CONFIG" ] && [ -e "$DEBIAN_CONFIG" ]; then
		# edit the configs
		sed -i -e "s,\(^host\W*=\)\(.*\$\),\1 $DB_HOST,i" \
			-e "s,\(^user\W*=\)\(.*\$\),\1 $DB_USER,i" \
			-e "s/\(^pass\W*=\)\(.*\$\)/\1 $DB_PASS/i" \
			-e "s,\(^db\W*=\)\(.*\$\),\1 $DB_DATABASE,i" \
                	-e "s,\(^#postmaster\W*=\)\(.*\$\),\1 $POSTMASTER,i" \
                	-e "s,\(^postmaster\W*=\)\(.*\$\),\1 $POSTMASTER,i" \
			-e "s,\(^EFFECTIVE_USER\W*=\)\(.*\$\),\1 dbmail,i" \
			-e "s,\(^EFFECTIVE_GROUP\W*=\)\(.*\$\),\1 dbmail,i" \
                        $CONFIG
		
		if [ "$AUTHDRIVER" = "ldap" ]; then
			sed -i -e "s/\(^BASE_DN\W*=\)\(.*\$\)/\1 $LDAP_BASE_DN/i" \
				-e "s/\(^HOSTNAME\W*=\)\(.*\$\)/\1 $LDAP_HOST/i" \
				-e "s/\(^BIND_PW\W*=\)\(.*\$\)/\1 $LDAP_BIND_PW/i" \
				-e "s/\(^BIND_DN\W*=\)\(.*\$\)/\1 $LDAP_BIND_DN/i" \
				-e "s/\(^FIELD_UID\W*=\)\(.*\$\)/\1 $LDAP_UID/i" \
				-e "s/\(^FIELD_CID\W*=\)\(.*\$\)/\1 $LDAP_CID/i" \
				-e "s/\(^authdriver\W*=\)\(.*\$\)/\1 ldap/" \
				$CONFIG
		else
			sed -i -e "s/\(^authdriver\W*=\)\(.*\$\)/\1 sql/" \
				$CONFIG
		fi
	
		umask $oldmask
	
		if [ "$START_IMAPD" = "true" ]; then
			sed -i -re 's/^.*START_IMAPD=.*/START_IMAPD=true/i' $DEBIAN_CONFIG
		else
			sed -i -re 's/^.*START_IMAPD=.*/#START_IMAPD=true/i' $DEBIAN_CONFIG
		fi
		if [ "$START_LMTPD" = "true" ]; then
			sed -i -re 's/^.*START_LMTPD=.*/START_LMTPD=true/i' $DEBIAN_CONFIG
		else
			sed -i -re 's/^.*START_LMTPD=.*/#START_LMTPD=true/i' $DEBIAN_CONFIG
		fi
		if [ "$START_POP3D" = "true" ]; then
			sed -i -re 's/^.*START_POP3D=.*/START_POP3D=true/i' $DEBIAN_CONFIG
		else
			sed -i -re 's/^.*START_POP3D=.*/#START_POP3D=true/i' $DEBIAN_CONFIG
		fi
		if [ "$START_SIEVE" = "true" ]; then
			sed -i -re 's/^.*START_SIEVE=.*/START_SIEVE=true/i' $DEBIAN_CONFIG
		else
			sed -i -re 's/^.*START_SIEVE=.*/#START_SIEVE=true/i' $DEBIAN_CONFIG
		fi
	
	fi
	
	db_stop
	if [ -e /usr/sbin/dbmail-deliver ]; then
		chown dbmail:dbmail $CONFIG /usr/sbin/dbmail-deliver
		chmod 4755 /usr/sbin/dbmail-deliver
	fi
	[ -e "$CONFIG" ] && chmod 0600 $CONFIG
	
    ;;
    abort-upgrade)
    ;;
    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 0
    ;;
esac

#DEBHELPER#

exit 0
