#!/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}.$$"
CFGFILE2="/etc/modprobe.conf"
TMPFILE2="${CFGFILE2}.$$"
CFGFILE3="/etc/modules.devfs"
TMPFILE3="${CFGFILE3}.$$"
CFGFILE4="/etc/modprobe.devfs"
TMPFILE4="${CFGFILE4}.$$"
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}"
}

for x in "${CFGFILE}" "${CFGFILE2}" "${CFGFILE4}"
do
	if [ -f "${x}" ]
	then
		if ! sed -ne 1p "${x}" | egrep -q "^${HEADER}"
		then
			echo "Error: the current ${x} 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
done

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
if [ -e "${CFGFILE2}" ]
then
	cp -f "${CFGFILE2}" "${CFGFILE2}".old
fi
if [ -e "${CFGFILE4}" ]
then
	cp -f "${CFGFILE4}" "${CFGFILE4}".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
if [ -x /sbin/generate-modprobe.conf ]
then
	sed -e "s:the files in ${MODDIR}:${CFGFILE}:" \
		"${TMPFILE}" > "${TMPFILE2}"

	if [ -f "${CFGFILE3}" ]
	then
		sed -e "s:the files in ${MODDIR}:${CFGFILE3}:" \
			"${TMPFILE}" > "${TMPFILE4}"
	fi
fi

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}"

if [ -x /sbin/generate-modprobe.conf ]
then
	if /sbin/generate-modprobe.conf >> "${TMPFILE2}" 2> /dev/null
	then
		mv -f "${TMPFILE2}" "${CFGFILE2}"
	else
		echo "Warning: could not generate ${CFGFILE2}!"
		rm -f "${TMPFILE2}"
	fi

	if [ -f "${CFGFILE3}" ]
	then
		gawk '$0 !~ /^[[:space:]]*include/ { print $0 }' "${CFGFILE3}" > "${TMPFILE3}"
		
		export TESTING_MODPROBE_CONF="${TMPFILE3}"
		if /sbin/generate-modprobe.conf >> "${TMPFILE4}" 2> /dev/null
		then
			mv -f "${TMPFILE4}" "${CFGFILE4}"

			echo >> "${CFGFILE4}"
			echo "include /etc/modprobe.conf" >> "${CFGFILE4}"
		else
			echo "Warning: could not generate ${CFGFILE4}!"
			rm -f "${TMPFILE4}"
		fi
		rm -f "${TMPFILE3}"
	fi
fi

# 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
