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

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 /etc/conf.modules
if egrep -q "path\[([A-Za-z]+)\]=.*\1" /etc/conf.modules; then
	echo "You have an old-style /etc/conf.modules file. The format has"
	echo "changed a little and modutils no longer works with the old"
	echo "format."
	echo ""
	echo -n "Enter \"yes\" if I should convert /etc/conf.modules: "
	read ANSWER
	if [ "$ANSWER" != "yes" ]; then
		echo "Okay, NOT converting. Please note that depmod and modprobe may"
		echo "fail to work."
	else
		cp /etc/conf.modules /etc/conf.modules.old
		sed -e "s#path\[\([A-Za-z0-9]\+\)\]=\(.*\)/\1#path[\1]=\2#" \
			/etc/conf.modules.old > /etc/conf.modules
		chmod 644 /etc/conf.modules
		
		echo "Finished converting /etc/conf.modules. A copy of the old version"
		echo "has been saved as /etc/conf.modules.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

/etc/init.d/kerneld start

if test -x /usr/bin/update-menus; then update-menus; 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


# Create the symlink from /etc/conf.modules to the proper file in
# /etc/modutils if it does not exist
if [ ! -L /etc/conf.modules ]; then
	echo "You have no /etc/conf.modules symlink, creating it"
	MODEL="`dpkg --print-installation-architecture`"
	CONF="/etc/modutils/conf.${MODEL}"
	if [ $MODEL = "m68k" ]; then
		if [ -f /proc/hardware ]; then
			case `awk '$1 == "Model:" { print $2 }' /proc/hardware` in
				Atari)
					CONF="${CONF}.atari"
					;;
				Amiga)
					CONF="${CONF}.amiga"
					;;
				Macintosh)
					CONF="${CONF}.mac"
					;;
				Motorola)
					CONF="${CONF}.MVME"
					;;
			esac
			if [ ! -f $CONF ]; then
				echo "Machine-specific conf.modules not found, using m68k generic config"
				CONF="/etc/modutils/conf.m68k"
			fi
		else
			echo "/proc not mounted, using generic m68k conf.modules"
		fi
	fi

	if [ ! -f $CONF ]; then
		echo "Architecture-specific modutils configuration not found, using defaults"
		CONF="/etc/modutils/conf.generic"
	fi

	ln -s $CONF /etc/conf.modules
else
	echo "You already have a /etc/conf.modules symlink.. leaving it"
fi
