#!/bin/sh -e
# This script can be called in the following ways:
#
# After the package was installed:
#	<postinst> configure <old-version>
#
#
# If prerm fails during upgrade or fails on failed upgrade:
#	<old-postinst> abort-upgrade <new-version>
#
# If prerm fails during deconfiguration of a package:
#	<postinst> abort-deconfigure in-favour <new-package> <version>
#	           removing <old-package> <version>
#
# If prerm fails during replacement due to conflict:
#	<postinst> abort-remove in-favour <new-package> <version>


# Remove a no-longer used conffile
rm_conffile()
{
    CONFFILE="$1"

    if [ -e "$CONFFILE".dpkg-obsolete ]; then
	echo "Removing obsolete conffile $CONFFILE"
	rm -f "$CONFFILE".dpkg-obsolete
    fi
}

# Remove a conffile directory if it's not empty
rm_confdir()
{
    CONFDIR="$1"

    if [ -d "$CONFDIR" ]; then
	rmdir "$CONFDIR" 2>/dev/null \
	    || echo "Unable to remove $CONFDIR, not empty"
    fi
}

# Move a conffile without triggering a dpkg question
mv_conffile() {
    OLDCONFFILE="$1"
    NEWCONFFILE="$2"

    if [ -e "$OLDCONFFILE".dpkg-moving ]; then
        echo "Preserving user changes to $NEWCONFFILE"
        mv -f "$NEWCONFFILE" "$NEWCONFFILE".dpkg-new
        mv -f "$OLDCONFFILE".dpkg-moving "$NEWCONFFILE"
    elif [ -e "$OLDCONFFILE".dpkg-bak ]; then
	rm -f "$OLDCONFFILE".dpkg-bak
    fi
}


# Migrate config to 3.7-style
migrate_config_37()
{
    rm_conffile /etc/modprobe.d/aliases
    rm_conffile /etc/modprobe.d/isapnp
    rm_conffile /etc/modprobe.d/options

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

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


# Create /etc/modules
create_etc_modules()
{
    if [ ! -e /etc/modules ]; then
	cat <<EOF >/etc/modules
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.

EOF
	chmod 644 /etc/modules
    fi
}

# Run depmod for installed kernels
run_depmod()
{
    for KVER in $(/bin/ls -v /lib/modules); do
	[ -f "/boot/System.map-$KVER" ] || continue

	echo "Running depmod for $KVER..."
	/sbin/depmod -a -F "/boot/System.map-$KVER" "$KVER"
    done
}


case "$1" in
    configure)
	# Upgrade from intrepid
	if dpkg --compare-versions "$2" lt "3.7~pre7-1"; then
	    migrate_config_37
	    run_depmod
	fi

	create_etc_modules
	;;

    abort-upgrade|abort-deconfigure|abort-remove)
	;;

    *)
	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
update-rc.d -f module-init-tools remove >/dev/null || exit $?
# End automatically added section

exit 0
