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

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

if [ ! "$1" = "configure" ]; then
	exit 0
fi

# Do the FHS-documentation-symlink-trick
if [ -d /usr/doc -a ! -e /usr/doc/$pkg -a -d /usr/share/doc/$pkg ] ; then
	ln -s ../share/doc/$pkg /usr/doc/$pkg
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 $CFGFILE exists and is not generated print a big fat
# warning and inform people about the new system
if [ -f $CFGFILE ] && ! 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
else
	update-modules
fi

if [ -f /etc/cron.d/modutils ]; then
	cat <<EOF

You still have a /etc/cron.d/modutils file. This was used to
remove automatically loaded modules every 20 minutes. However
this is no longer present to prevent problems with kernel versions
2.0 and older and prevent machines from awakening. Removing this
file is strongly encouraged.

EOF
	echo -n "Enter \"no\" if you do not want me to remove this file: "
	read ANSWER
	echo ""
	if [ "$ANSWER" != "no" ]; then
		echo "Removing file."
		rm -f /etc/cron.d/modutils
	else
		echo "Okay, I will not remove this file. Be warned you might"
		echo "experience some problems."
		echo ""
	fi
fi

