#! /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

# XXX remove
# echo apache2.preinst $@ running

# XXX: Whichever file you add in one of the sections below, make sure you copy
# the same file arrays to apache2.postinst and apache2.postrm, too!

OBSOLETE_CONFFILES="/etc/apache2/mods-available/authz_default.load
/etc/apache2/mods-available/authn_default.load
/etc/apache2/mods-available/mem_cache.load
/etc/apache2/mods-available/mem_cache.conf
/etc/apache2/mods-available/authn_alias.load
/etc/apache2/mods-available/cern_meta.load
/etc/apache2/mods-available/disk_cache.load
/etc/apache2/mods-available/disk_cache.conf
/etc/apache2/mods-available/imagemap.load
/etc/apache2/mods-available/version.load"

# conffiles which moved from one random location to another, separate source and
# destination by a colon
MOVED_CONFFILES="/etc/bash_completion.d/apache2.2-common:/etc/bash_completion.d/apache2
/etc/apache2/sites-available/default:/etc/apache2/sites-available/000-default.conf
/etc/apache2/sites-available/default-ssl:/etc/apache2/sites-available/default-ssl.conf
/etc/apache2/conf.d/charset:/etc/apache2/conf-available/charset.conf
/etc/apache2/conf.d/localized-error-pages:/etc/apache2/conf-available/localized-error-pages.conf
/etc/apache2/conf.d/other-vhosts-access-log:/etc/apache2/conf-available/other-vhosts-access-log.conf
/etc/apache2/conf.d/security:/etc/apache2/conf-available/security.conf"


obsolete_conffile_exists()
{
	for CONFFILE in $OBSOLETE_CONFFILES ; do
		if [ -e "$CONFFILE" ] ; then
			return 0
		fi
	done

	for CONFFILE in $MOVED_CONFFILES ; do
		CONFFILE=$( echo "$CONFFILE" | cut -d: -f1 )
		if [ -e "$CONFFILE" ] ; then
			return 0
		fi
	done

	return 1
}

# The two functions below are licensed GPL-2+ and was written by dpkg maintainers
# See the dpkg-maintscript-helper script for details
prepare_rm_conffile()
{
	for CONFFILE in $OBSOLETE_CONFFILES ; do
		[ -e "$CONFFILE" ] || continue

		local md5sum="$(md5sum $CONFFILE | sed -e 's/ .*//')"
		local old_md5sum="$(dpkg-query -W -f='${Conffiles}' apache2.2-common apache2 | \
			sed -n -e "\' $CONFFILE ' { s/ obsolete$//; s/.* //; p }")"
		if [ "$md5sum" != "$old_md5sum" ]; then
			echo "Obsolete conffile $CONFFILE has been modified by you."
			echo "Saving as $CONFFILE.dpkg-bak ..."
			mv -f "$CONFFILE" "$CONFFILE.dpkg-backup"
		else
			echo "Moving obsolete conffile $CONFFILE out of the way..."
			mv -f "$CONFFILE" "$CONFFILE.dpkg-remove"
		fi
	done
}

prepare_mv_conffile()
{
	for CONFFILE in $MOVED_CONFFILES ; do

		CONFFILE=$( echo "$CONFFILE" | cut -d: -f1 )

		[ -e "$CONFFILE" ] || continue

		local md5sum="$(md5sum $CONFFILE | sed -e 's/ .*//')"
		local old_md5sum="$(dpkg-query -W -f='${Conffiles}' apache2.2-common apache2 | \
			sed -n -e "\' $CONFFILE ' { s/ obsolete$//; s/.* //; p }")"

		if [ "$md5sum" = "$old_md5sum" ]; then
			mv -f "$CONFFILE" "$CONFFILE.dpkg-remove"
		fi
	done
}

case "$1" in
    install|upgrade)

	# black magic follows below. we're upgrading from Squeeze if

	# 1) an apache2-mpm package exists
	if [ -d "/etc/apache2/" ] ; then
	    	mpm=$(dpkg-query -f '${Package}\t${Status}\n'  -W 'apache2-mpm-*' 2>/dev/null | grep "install ok" | cut -f1)
		if [ -n "$mpm" ] ; then
			if [ ! -f /etc/apache2/.apache2_mpm_selected ] ; then
				echo "# automatically created during upgrade" >> /etc/apache2/.apache2_mpm_selected
				echo "# it can be safely removed anytime" >> /etc/apache2/.apache2_mpm_selected
				echo "$mpm" >> /etc/apache2/.apache2_mpm_selected
			fi
		fi

		if [ -n "$2" ] && dpkg --compare-versions "$2" 'lt' '2.4.7-1~' && dpkg --compare-versions "$2" 'ge' '2.4.1-1' ; then
			CUR_MPM="$(ls /etc/apache2/mods-enabled/mpm_*.load)"
			CUR_MPM="${CUR_MPM##*/mpm_}"
			CUR_MPM="${CUR_MPM%.load}"
			if [ "$CUR_MPM" == "itk" ] ; then
				echo "apache2-mpm-itk" >> /etc/apache2/.apache2_mpm_selected
			fi
		fi
	fi
	# 2) an apache2.2-common conffiles exists or the 2.2 apache2 package is
	# installed
	if [ -n "$2" ] || obsolete_conffile_exists ; then
		prepare_rm_conffile
		prepare_mv_conffile
	fi

    ;;

    abort-upgrade)
    ;;

    *)
	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

# vim: syntax=sh ts=4 sw=4 sts=4 sr noet
