#! /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
H4sIAAAAAAAAA+07/XPbNrL91forMPbN2L7q05adPl8md4pFJ5qTLT9JTq+dm1EhEpJYkwQPIGWr
r+9/f7sLkCJlxc50LslrT5g2skjsYrFf2F2sms1mzRMzngZJ3ZXR7JvPMJowzttt+oSx+XnSbp5/
03zVPD09abfO8Xnr5LTd+oY1PwcxmyPVCVe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WrbbIfRCj52Qz+JbQZ8/OaFb6sPHkWGYmYvH487Fw6thHGGochFKvr2KK5
phXS9CCzz+w3elPprWzHBTX5CNN0ge0fwBufzyOQm++yOFWxBAs8rhcX2baTbN3KGNv4Tfc07eOg
9MBu4kqqqe9l4YXpkSLdsb07uch9YW4HbDETC+CbU/VKJyKksiYVFjMBMvTP5hdwWbkMW+CmQMiK
YZ2ksLyJXAqLmv066+uIysHQ1tvNFT8VCf5Zn/vJR97oZZS5rUL93/5wkFxorLCIm7DrUc8xhRoy
A3hM821JWJMjBGAsGjIRUMMHhGaecAOuTN94/stHrJthv5st8ZKrM0uSF7LNCpqMxT7H7U+zHn2P
9m08IlW2/1G7NHjJx9WynjG2DwoSgbfe/8Q9UrDHtJ9kP2IQ4VSYCr1xAbZjykfPjtNw4zMFbkPb
Bh9wzeSE+Zxjmzz2Brn3P8MhQoprjpPftsdshyN7eNVuJeBesX1av4ZXEtgND0cCNvUf/mX/jx2T
uuCJJ3hvDtE49z7PGi/lf61tv/8+2cV/X2L0Qeq2SJ6rwsSEMus2pKzIbUMc/JysFUfLP7aJ/KEH
/RT/M9o+jpfsv/lq0/5bZ63znf1/iVGwf1KFT7J9ozQ7u/8DDMgP5yLk8ed0AS+f/+3N8791frqz
/y8xivZvVeHTXECmNzsvsBu7sRu7sRu78bsb/wcVEdxEAFAAAA==

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.

# Automatically added by dh_installinit/13.32
if [ "$1" = "install" ] && [ -n "$2" ] && [ -e "/etc/init.d/apache2" ] ; then
	chmod +x "/etc/init.d/apache2" >/dev/null || true
fi
# End automatically added section
# Automatically added by dh_installinit/13.32
if [ "$1" = "install" ] && [ -n "$2" ] && [ -e "/etc/init.d/apache-htcacheclean" ] ; then
	chmod +x "/etc/init.d/apache-htcacheclean" >/dev/null || true
fi
# End automatically added section


exit 0
