#! /bin/sh
#
# This is the update-modules script for Debian GNU/Linux.
# Written by Wichert Akkerman <wakkerma@debian.org>
# Copyright (C) 1998, 1999 Software in the Public Interest

# Modifications by Daniel Robbins <drobbins@gentoo.org>, Gentoo Technologies, Inc.
# 02 Sep 2001 -- Removed "arch" stuff since I see no reason to have support for varying
# CPU architectures on a single system.

CFGFILE=/etc/modules.conf
TMPFILE="${CFGFILE}.$!"
MODDIR=/etc/modules.d
ARCHDIR=${MODDIR}/arch
HEADER="### This file is automatically generated by update-modules"

set -e

# Reset the sorting order since we depend on it
LC_COLLATE=C
export LC_COLLATE

depdir()
{
	dep=`grep '[[:space:]]*depfile' ${CFGFILE} | tail -n 1 | sed -e 's/depfile=//' -e 's,/[^/]*$,,'`
	if [ -z "$dep" ]
	then
	    dep=/lib/modules/`uname -r`
	fi

	echo $dep
}

if [ -f $CFGFILE ]; then
	if ! sed -ne 1p $CFGFILE | grep -q "^$HEADER" ; then
		echo "Error: the current $CFGFILE is not automatically generated."
		if [ "$1" != "force" ]; then
			echo "Use \"update-modules force\" to force (re)generation."
			exit 1
		else
			echo "force specified, (re)generating file anyway."
		fi
	fi
fi

if [ 0 -ne "`id -u`" ]; then
	echo "You have to be root to do this."
	exit 2
fi

if [ -e $CFGFILE ]; then
  cp -f $CFGFILE ${CFGFILE}.old
fi

echo $HEADER > $TMPFILE
cat <<EOF >> $TMPFILE
#
# Please do not edit this file directly. If you want to change or add
# anything please take a look at the files in $MODDIR and read
# the manpage for update-modules.
#
EOF

for cfg in ${MODDIR}/* $CONF ; do
	[ ! -r $cfg ] && continue
	echo "### update-modules: start processing $cfg" >> $TMPFILE
	if [ -x $cfg ]; then
		# $cfg can be executable; nice touch, Wichert! :)
		$cfg >> $TMPFILE
	else
		cat $cfg >> $TMPFILE
	fi
	echo >> $TMPFILE
	echo "### update-modules: end processing $cfg" >> $TMPFILE
	echo >> $TMPFILE
done

mv $TMPFILE $CFGFILE

# We also call depmod here to stop insmod from complaining that modules.conf
# is more recent then modules.dep
#
if [ -d `depdir` -a -f /proc/modules ]
then
	depmod -a
fi

