#!/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"

set -e

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

# Remove obsolete /etc/init.d/modclean file
if [ -e /etc/init.d/modclean ] ; then
	rm -f /etc/init.d/modclean
	update-rc.d modclean remove
	echo
# Unfortunately modutils 2.4.2-1 forgot to call update-rc.d so we need
# to check for that as well.
elif [ -e /etc/rc2.d/S20modclean ] ; then
	update-rc.d modclean remove
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
	rm -f /lib/modules/current
	echo
fi

# Create /var/log/ksymoops if this is an intial install. This allows
# a user to remove it to disable logging later on
if [ -z "$2" -a ! -d /var/log/ksymoops ] ; then
	mkdir /var/log/ksymoops
	chmod 755 /var/log/ksymoops
fi

# Create the /etc/modules file if it does not exist
if [ ! -e /etc/modules ] ; then
	cat <<EOF > /etc/modules
# /etc/modules: kernel modules to load at boot time.
#
# This file should contain the names of kernel modules that are
# to be loaded at boot time, one per line.  Comments begin with
# a "#", and everything on the line after them are ignored.

EOF
	chmod 644 /etc/modules
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

exit 0

