#!/bin/sh
#
# This is the modules-update 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 modules-update"

set -e

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

depdir() {
	dep="`egrep '[[: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} | egrep -q "^${HEADER}"
	then
		echo "Error: the current ${CFGFILE} is not automatically generated."
		
		if [ "$1" != "force" ]
		then
			echo "Use \"modules-update 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 modules-update.
#
EOF

for cfg in ${MODDIR}/* ${CONF}
do
	[ -d ${cfg} ] && continue

	[ ! -r ${cfg} ] && continue

	[ -n "`echo ${cfg} | awk '!/~$|\.bak$/ { print $0 }'`" ] || continue
	
	echo "### modules-update: 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 "### modules-update: end processing ${cfg}" >> ${TMPFILE}
	echo >> ${TMPFILE}
done

mv -f ${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


# vim:ts=4
