#! /bin/bash
# preinst script for apache2
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <new-preinst> `install'
#        * <new-preinst> `install' <old-version>
#        * <new-preinst> `upgrade' <old-version>
#        * <old-preinst> `abort-upgrade' <new-version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

FIXUP_TEMPFILE=


# During the migration of conffiles from apache2.2-common to apache2,
# some things may have gone wrong.
# * Some conffiles may have been left with obsolete content. These
#   have an md5sum in this list.
# * Some other conffiles may have been removed but dpkg still thinks that
#   they belong to apache2.2-common. A few of these have been re-introduced,
#   but dpkg being confused about their state causes dpkg to think the
#   admin has removed them and to not create the new content.
#   These have a 'restore' instead of a md5 in the list.
list_fixup_conffiles () {
	cat <<- EOF
		/etc/bash_completion.d/apache2					6a5f85e62655f6b5c8fa0f95c7c35c9c	removed
		/etc/apache2/sites-available/000-default.conf			2cc450cf300a880abbc3767fc002477d
		/etc/apache2/sites-available/default-ssl.conf			196d150beeaeaf845ece50d7e84e12de
		/etc/apache2/conf-available/charset.conf			e6fbb8adf631932851d6cc522c1e48d7
		/etc/apache2/conf-available/localized-error-pages.conf		844ba27ddb794fc6967bfb56b950e6a8
		/etc/apache2/conf-available/other-vhosts-access-log.conf	2cad303fc4221d6b0068a8b37597b9fb
		/etc/apache2/conf-available/security.conf			0f644d9d04ad556f44f1e65674bc07dc
		/etc/apache2/mods-available/cern_meta.load			restore
		/etc/apache2/mods-available/ident.load				restore
		/etc/apache2/mods-available/imagemap.load			restore
		EOF
}

create_fixup_conffiles_tgz () {
	FIXUP_TEMPFILE=$(mktemp)
	base64 -d > $FIXUP_TEMPFILE << EOF
XXX_FIXUP_CONFFILES_BASE64_XXX
EOF
}

extract_fixup_conffile () {
	local FILE=$1
	local BASENAME=${FILE##*/}
	tar -xz -O -f $FIXUP_TEMPFILE $BASENAME > $FILE
}

replace_broken_conffiles () {
	local FILE
	local MD5
	create_fixup_conffiles_tgz
	while read FILE MD5 REMOVED ; do
		if [ -f "$FILE" ] && md5sum "$FILE" | grep -q "^$MD5 " ; then
			echo "Replacing broken conffile ${FILE}."
			mv "$FILE" "${FILE}.dpkg-remove-fixup"
			if [ -z "$REMOVED" ] ; then
				extract_fixup_conffile "$FILE"
			fi
		elif [ ! -e "$FILE" ] && [ "$MD5" = "restore" ] ; then
			echo "Restoring lost conffile ${FILE}."
			extract_fixup_conffile "$FILE"
		fi
	done
	rm -f "$FIXUP_TEMPFILE"
}

revert_broken_conffiles () {
	local FILE
	local MD5
	local REMOVE
	while read FILE MD5 REMOVED; do
		if [ -f "$FILE.dpkg-remove-fixup" ]; then
			echo "Moving broken conffile $FILE back."
			mv "${FILE}.dpkg-remove-fixup" "$FILE"
		fi
	done
}

case "$1" in
    upgrade|install)

	if dpkg --compare-versions "$2" lt-nl "2.4.23-3~" ; then
		list_fixup_conffiles | replace_broken_conffiles
	fi

    ;;

    abort-upgrade)
		list_fixup_conffiles | revert_broken_conffiles
    ;;

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

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

#DEBHELPER#

exit 0
