#!/bin/sh -e
# This script can be called in the following ways:
#
# After the package was removed:
#	<postrm> remove
#
# After the package was purged:
#	<postrm> purge
#
# After the package was upgraded:
#	<old-postrm> upgrade <new-version>
# if that fails:
#	<new-postrm> failed-upgrade <old-version>
#
#
# After all of the packages files have been replaced:
#	<postrm> disappear <overwriting-package> <version>
#
#
# If preinst fails during install:
#	<new-postrm> abort-install
#
# If preinst fails during upgrade of removed package:
#	<new-postrm> abort-install <old-version>
#
# If preinst fails during upgrade:
#	<new-postrm> abort-upgrade <old-version>


# Undo removal of a no-longer used conffile
undo_rm_conffile()
{
    CONFFILE="$1"

    if [ ! -e "$CONFFILE" ]; then
	if [ -e "$CONFFILE".dpkg-bak ]; then
	    echo "Restoring modified conffile $CONFFILE"
	    mv -f "$CONFFILE".dpkg-bak "$CONFFILE"
	elif [ -e "$CONFFILE".dpkg-obsolete ]; then
	    mv -f "$CONFFILE".dpkg-obsolete "$CONFFILE"
	fi
    fi
}

# Finish removal of a no-longer used conffile
finish_rm_conffile()
{
    CONFFILE="$1"

    if [ -e "$CONFFILE".dpkg-bak ]; then
	rm -f "$CONFFILE".dpkg-bak
    fi
}

# Undo move of a conffile
undo_mv_conffile()
{
    CONFFILE="$1"

    if [ ! -e "$CONFFILE" ]; then
	if [ -e "$CONFFILE".dpkg-bak ]; then
	    mv -f "$CONFFILE".dpkg-bak "$CONFFILE"
	elif [ -e "$CONFFILE".dpkg-moving ]; then
	    mv -f "$CONFFILE".dpkg-moving "$CONFFILE"
	fi
    fi
}

# Finish move of a conffile
finish_mv_conffile()
{
    CONFFILE="$1"

    if [ -e "$CONFFILE".dpkg-bak ]; then
	rm -f "$CONFFILE".dpkg-bak
    fi
}


# Finish migrating config to 3.7-style
finish_migrate_config_37()
{
    finish_rm_conffile /etc/modprobe.d/aliases
    finish_rm_conffile /etc/modprobe.d/isapnp
    finish_rm_conffile /etc/modprobe.d/options

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

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

# Undo migrating config to 3.7-style
undo_migrate_config_37()
{
    undo_rm_conffile /etc/modprobe.d/aliases
    undo_rm_conffile /etc/modprobe.d/isapnp
    undo_rm_conffile /etc/modprobe.d/options

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

    undo_rm_conffile /etc/modprobe.d/blacklist-amd76-edac
    undo_mv_conffile /etc/modprobe.d/blacklist \
		     /etc/modprobe.d/blacklist.conf
    undo_mv_conffile /etc/modprobe.d/blacklist-firewire \
		     /etc/modprobe.d/blacklist-firewire.conf
    undo_mv_conffile /etc/modprobe.d/blacklist-framebuffer \
		     /etc/modprobe.d/blacklist-framebuffer.conf
    undo_mv_conffile /etc/modprobe.d/blacklist-watchdog \
		     /etc/modprobe.d/blacklist-watchdog.conf
}



# Remove configuration
purge_files()
{
    if [ -f /etc/modules ]; then
	rm -f /etc/modules
    fi
}


case "$1" in
    remove)
	;;

    purge)
	finish_migrate_config_37
	purge_files
	;;

    upgrade|failed-upgrade|disappear)
	;;

    abort-install|abort-upgrade)
	# Abort upgrade from intrepid
	if dpkg --compare-versions "$2" lt "3.7~pre7-1"; then
	    undo_migrate_config_37
	fi
	;;

    *)
	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 -- "$@"


exit 0
