#!/bin/sh -e
# post install script for the Debian GNU/Linux modutils package

CFGFILE="/etc/conf.modules"
HEADER="### This file is automatically generated by update-modules"

case "$1" in
	configure)
		# continue as normal
		;;
	abort-upgrade|abort-remove|abort-deconfigure)
		exit 0;
	;;
	*)
		echo "postinst called with unknown argument \`$1'" >&2;
		exit 0;
	;;
esac

# See if the user still has an old-style $CFGFILE
if egrep -q "path\[([A-Za-z]+)\]=.*\1" $CFGFILE ; then
	echo "You have an old-style $CFGFILE file. The format has"
	echo "changed a little and modutils no longer works with the old"
	echo "format."
	echo ""
	echo -n "Enter \"no\" if I should not convert $CFGFILE: "
	read ANSWER
	if [ "$ANSWER" = "no" -o $ANSWER = "NO" -o $answer = "n" -o $ANSWER = "N"]
	then
		echo "Okay, NOT converting. Please note that depmod and modprobe may"
		echo "fail to work."
	else
		cp $CFGFILE ${CFGFILE}.old
		sed -e "s#path\[\([A-Za-z0-9]\+\)\]=\(.*\)/\1#path[\1]=\2#" \
			${CFGFILE}.old > $CFGFILE
		
		echo "Finished converting $CFGFILE. A copy of the old version"
		echo "has been saved as $CFGFILE.old. You may safely remove"
		echo "this file."
	fi
fi

# Remove obsolete /lib/modules/current link
if [ -L /lib/modules/current ]
then
	echo -n "Removing obsolete /lib/modules/current softlink.."
	rm -f /lib/modules/current
	echo
fi

# Reset KDOPT setting in /etc/init.d/kerneld.
if [ -f /var/run/kerneld.OPT ]
then
	echo "Restoring original KDOPT setting to /etc/init.d/kerneld.."
	OPTLINE="`cat /var/run/kerneld.OPT`"
	cp /etc/init.d/kerneld /etc/init.d/kerneld.TMP
	sed -e "s/^KDOPT="".*/$OPTLINE/" < /etc/init.d/kerneld.TMP > /etc/init.d/kerneld
	if [ -s /etc/init.d/kerneld ]
	then
		rm -f /etc/init.d/kerneld.TMP
	fi
	rm -f /var/run/kerneld.OPT
fi

update-rc.d kerneld defaults 12 >/dev/null

if [ -f /etc/modules ]; then
	if ! grep -q '^[[:space:]]*noauto[[:space:]]*$' /etc/modules ; then
		/etc/init.d/kerneld start
	fi
fi

if [ ! -z "$2" ]; then
	if `dpkg --compare-versions 2.1.85-1 gt $2`; then
		cat <<EOF
IMPORTANT NOTES
===============
1) Support for request-route has been dropped from modutils. If you
used this feature please switch to diald.

2) Debian GNU/Linux modutils now supports persistant module storage.
Please read the readme /usr/doc/modutils/README.Debian.gz before using
it though!

EOF
		echo -n "Press enter to continue "
		read HITME
	fi
fi

# If $CFGFILE exists and is not generated print a big fat
# warning and inform people about the new system
if [ -f $CFGFILE ]; then
	if ! head -1 $CFGFILE | grep -q "^$HEADER" ; then
		cat <<EOF

WARNING: you already have an $CFGFILE file which has not
been generated by update-modules. Debian now uses a new system which
uses multiple files in the /etc/modutils directory. See the manpage
for update-modules for more information on this setup.

Please check all changes you made in $CFGFILE and either
apply them to the provided files in /etc/modutils or add your own
files there. Then run update-modules.

EOF
		echo -n "Press [ENTER] to continue"
		read HITME
	fi
else
	update-modules
fi

