#! /bin/sh
# postrm script for netenv
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postrm> `remove'
#        * <postrm> `purge'
#        * <old-postrm> `upgrade' <new-version>
#        * <new-postrm> `failed-upgrade' <old-version>
#        * <new-postrm> `abort-install'
#        * <new-postrm> `abort-install' <old-version>
#        * <new-postrm> `abort-upgrade' <old-version>
#        * <disappearer's-postrm> `disappear' <r>overwrit>r> <new-version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

# function defining
restore_false_links(){
    if [ X`readlink /etc/resolv.conf` = X/etc/resolv.conf.netenv_default ]; then
	if [ ! -r `readlink /etc/resolv.conf` ]; then
			# is the old file still there?
	    if [ -f /etc/resolv.conf.netenv-default ]; then
		rm /etc/resolv.conf
		mv /etc/resolv.conf.netenv-default /etc/resolv.conf
		echo "Netenv: Restored old resolv.conf (Bug #225582)."
	    else
		echo "Problems cleaning up wrong links for resolv.conf (Bug #225582)."
		echo "Correct removal or purge of netenv might require manual action"
	    fi
	fi
    fi
    if [ X`readlink /etc/network/interfaces` = X/etc/network/interfaces.netenv_default ]; then
	if [ ! -r `readlink /etc/network/interfaces` ]; then
			# is the old file still there?
	    if [ -f /etc/network/interfaces.netenv-default ]; then
		rm /etc/network/interfaces
		mv /etc/network/interfaces.netenv-default /etc/network/interfaces
		echo "Netenv: Restored old interfaces (Bug #225582)."
	    else
		echo "Problems cleaning up wrong links for interfaces (Bug #225582)."
		echo "Correct removal or purge of netenv might require manual action"
	    fi
	fi
    fi
    BAD_SCRIPT=/etc/netenv/setup-default
    if grep netenv_default $BAD_SCRIPT >/dev/null 2>&1; then
	sed -e 's/netenv_default/netenv-default/g' < $BAD_SCRIPT >${BAD_SCRIPT}.tmp
	rm $BAD_SCRIPT
	mv ${BAD_SCRIPT}.tmp $BAD_SCRIPT
	echo "Netenv: Corrected buggy setup-default file (Bug #225582)."
    fi
}

remove_netenvlinks(){
    oldpwd=`pwd`
    cd /etc
    echo
    if [ -L resolv.conf ]; then
	target=`readlink resolv.conf`
	rm resolv.conf
	mv $target resolv.conf && rm -f resolv.conf.netenv-old resolv.conf.netenv-default
	echo "Restoring static network configuration:"
	echo "Using $target for /etc/resolv.conf"
	echo "Please check that you really want this configuration!"
    fi
    cd network
    if [ -L interfaces ]; then
	target=`readlink interfaces`
	rm interfaces
	mv $target interfaces && rm -f interfaces.netenv-old interfaces.netenv-default
	echo "Restoring static network configuration:"
	echo "Using $target for /etc/network/interfaces"
	echo "Please check that you really want this configuration!"
    fi
    echo
    cd $oldpwd
}

restore_configuration(){
    if [ -f /usr/share/debconf/confmodule ]; then
	. /usr/share/debconf/confmodule

	db_get netenv/auto_configure
	case "$RET" in 
	    "Use current settings")
		db_get netenv/auto_method
		case "$RET" in
		    pcmcia)
			echo "If you followed the instructions in the netenv documentation,"
			echo "remember to restore the changes you made to /etc/pcmcia/network.opts" ;;
		    interfaces)
			if [ "$1" = "purge" ]; then
			    remove_netenvlinks;
			fi ;;
		esac
		;;
	    *) 
		;;
	esac
    fi
}

# start the actual work:

case "$1" in
    remove|upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
 	  # clean up for bug #225582 in version 0.94.3-1 (and probably older)
	restore_false_links
  	# normal cleanup
	restore_configuration "$1"
	;;
    purge)
 	  # clean up for bug #225582 in version 0.94.3-1 (and probably older)
	restore_false_links
  	# normal cleanup
	restore_configuration "$1"
		# We purge the configuration files that we created. However, we deliberately
		# do _not_ reconstruct /etc/resolv.conf and /etc/network/interfaces from the
		# safed files (*.netenv-old). This is because the settings in there might no
		# longer be valid and changes made only in the files used by netenv. This 
		# cannot be sorted out without knowing correct network parameters - therefore
		# it is left to the user.
	NODE=`uname -n`
	CONFIGURATION_FILES="/etc/netenv/${NODE} /etc/netenv/setup-default"
	rm -f $CONFIGURATION_FILES
	CONFDIR=/etc/netenv/
		# remove confdir if there are no user files left. If the user nuked the files
		# before, the dir will have been removed by dpkg.
	if [ -d $CONFDIR ]; then
	    [ $(find "$CONFDIR" | wc -l) -eq 1 ] && { rmdir $CONFDIR; echo "Removed empty directory $CONFDIR"; };
	fi
	
    # now remove the defaultitem file
	rm -f /var/cache/netenv/defaultitem
	;;
    *)
	echo "postrm 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
