#!/bin/sh -e
# This script can be called in the following ways:
#
# Before the package is installed:
#	<new-preinst> install
#
# Before removed package is upgraded:
#	<new-preinst> install <old-version>
#
# Before the package is upgraded:
#	<new-preinst> upgrade <old-version>
#
#
# If postrm fails during upgrade or fails on failed upgrade:
#	<old-preinst> abort-upgrade <new-version>


# Prepare to remove a no-longer used conffile
prep_rm_conffile()
{
    CONFFILE="$1"

    if [ -e "$CONFFILE" ]; then
        md5sum="`md5sum \"$CONFFILE\" | sed -e \"s/ .*//\"`"
        old_md5sum="`sed -n -e \"/^Conffiles:/,/^[^ ]/{\\\\' $CONFFILE '{s/ obsolete$//;s/.* //;p}}\" /var/lib/dpkg/status`"
        if [ "$md5sum" != "$old_md5sum" ]; then
            echo "Obsolete conffile $CONFFILE has been modified by you, renaming to .dpkg-bak"
            mv -f "$CONFFILE" "$CONFFILE".dpkg-bak
	else
	    mv -f "$CONFFILE" "$CONFFILE".dpkg-obsolete
	fi
    fi
}

# Prepare to move a conffile without triggering a dpkg question
prep_mv_conffile() {
    CONFFILE="$1"

    if [ -e "$CONFFILE" ]; then
        md5sum="`md5sum \"$CONFFILE\" | sed -e \"s/ .*//\"`"
        old_md5sum="`sed -n -e \"/^Conffiles:/,/^[^ ]/{\\\\' $CONFFILE '{s/ obsolete$//;s/.* //;p}}\" /var/lib/dpkg/status`"
        if [ "$md5sum" = "$old_md5sum" ]; then
            mv -f "$CONFFILE" "$CONFFILE".dpkg-bak
	else
            mv -f "$CONFFILE" "$CONFFILE".dpkg-moving
        fi
    fi
}


# Prepare to migrate config to 3.7-style
prep_migrate_config_37()
{
    prep_rm_conffile /etc/modprobe.d/aliases
    prep_rm_conffile /etc/modprobe.d/isapnp
    prep_rm_conffile /etc/modprobe.d/options

    prep_rm_conffile /etc/modprobe.d/arch/alpha
    prep_rm_conffile /etc/modprobe.d/arch/i386
    prep_rm_conffile /etc/modprobe.d/arch/ia64
    prep_rm_conffile /etc/modprobe.d/arch/m68k.amiga
    prep_rm_conffile /etc/modprobe.d/arch/m68k.atari
    prep_rm_conffile /etc/modprobe.d/arch/m68k.generic
    prep_rm_conffile /etc/modprobe.d/arch/mips
    prep_rm_conffile /etc/modprobe.d/arch/parisc
    prep_rm_conffile /etc/modprobe.d/arch/powerpc.apus
    prep_rm_conffile /etc/modprobe.d/arch/powerpc.generic
    prep_rm_conffile /etc/modprobe.d/arch/powerpc.pmac
    prep_rm_conffile /etc/modprobe.d/arch/s390
    prep_rm_conffile /etc/modprobe.d/arch/sparc
    prep_rm_conffile /etc/modprobe.d/arch/x86_64

    if [ -L /etc/modprobe.d/arch-aliases ]; then
	rm -f /etc/modprobe.d/arch-aliases
    fi

    prep_rm_conffile /etc/modprobe.d/blacklist-amd76-edac
    prep_mv_conffile /etc/modprobe.d/blacklist \
		     /etc/modprobe.d/blacklist.conf
    prep_mv_conffile /etc/modprobe.d/blacklist-firewire \
		     /etc/modprobe.d/blacklist-firewire.conf
    prep_mv_conffile /etc/modprobe.d/blacklist-framebuffer \
		     /etc/modprobe.d/blacklist-framebuffer.conf
    prep_mv_conffile /etc/modprobe.d/blacklist-watchdog \
		     /etc/modprobe.d/blacklist-watchdog.conf
    prep_mv_conffile /etc/modprobe.d/intel-5300-iwlagn-disable11n \
		     /etc/modprobe.d/intel-5300-iwlagn-disable11n.conf
}


case "$1" in
    install|upgrade)
	# Upgrade from intrepid
	if dpkg --compare-versions "$2" lt "3.7~pre7-1"; then
	    prep_migrate_config_37
	fi
	# Unequivocally drop this conf file. Its got a bogus module parameter.
	if dpkg --compare-versions "$2" le-nl "3.12-1ubuntu6"; then
	    rm -f /etc/modprobe.d/intel-3945-iwlagn-disable11n.conf
	fi
	;;

    abort-upgrade)
	;;

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

# iwlwifi split into 2 drivers as of 2.6.38; iwl3945 and iwlagn
dpkg-maintscript-helper rm_conffile /etc/modprobe.d/intel-5300-iwlagn-disable11n.conf 3.12-1ubuntu4 -- "$@"

# Automatically added by dh_installinit
if [ "$1" = install ] || [ "$1" = upgrade ]; then
	if [ -e "/etc/init.d/module-init-tools" ] && [ ! -L "/etc/init.d/module-init-tools" ]; then
		if [ "`md5sum \"/etc/init.d/module-init-tools\" | sed -e \"s/ .*//\"`" != \
		     "`dpkg-query -W -f='${Conffiles}' module-init-tools | sed -n -e \"\\\\' /etc/init.d/module-init-tools '{s/ obsolete$//;s/.* //p}\"`" ]
		then
			echo "Obsolete conffile /etc/init.d/module-init-tools has been modified by you, renaming to .dpkg-bak"
			mv -f "/etc/init.d/module-init-tools" "/etc/init.d/module-init-tools.dpkg-bak"
		else
			rm -f "/etc/init.d/module-init-tools"
		fi
	fi
fi
# End automatically added section

exit 0
