#!/bin/bash
#
# 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 Foundation
# 02 Sep 2001 -- Removed "arch" stuff since I see no reason to have
# support for varying CPU architectures on a single system.
#
# Updated by Aron Griffis <agriffis@gentoo.org>
# 05 May 2004 -- handle --assume-kernel argument for livecd building

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"

source /sbin/functions.sh

# Parse command-line
FORCE=false
ASSUME_KV=
while [ -n "$1" ]; do
	case "$1" in
		force)
			FORCE=true ;;
		--assume-kernel=*)
			ASSUME_KV=${1#*=} ;;
		*)
			echo "Error: I don't understand $1" >&2
			exit 1 ;;
	esac
	shift
done

# Set kernel version, either from --assume-kernel or uname -r
KV=${ASSUME_KV:-$(uname -r)}
if [[ $(KV_to_int ${KV}) -ge $(KV_to_int 2.5.48) ]]; then
	KERNEL_2_5=true
else
	KERNEL_2_5=false
fi

set -e

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

depdir() {
	dep="`sed -n -e '/[ \t]*depfile=/h;${x;s/[ \t]*depfile=//g;s,/[^/]*$,,p}' ${CFGFILE}`"
	if [ -z "${dep}" ]
	then
	    dep="/lib/modules/${KV}"
	fi

	echo "${dep}"
}

CFGFILES="${CFGFILE}"

if ${KERNEL_2_5}; then
	CFGFILES="${CFGFILES} ${CFGFILE2} ${CFGFILE4}"
fi

for x in ${CFGFILES}
do
	if [ -f "${x}" ]
	then
		if ! sed -ne 1p "${x}" | egrep -q "^${HEADER}"
		then
			echo "Error: the current ${x} is not automatically generated."
		
			if $FORCE; then
				echo "force specified, (re)generating file anyway."
			else
				echo "Use \"modules-update force\" to force (re)generation."
				exit 1
			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 ${KERNEL_2_5}; then
	if [ -e "${CFGFILE2}" ]
	then
		cp -f "${CFGFILE2}" "${CFGFILE2}".old
	fi
	if [ -e "${CFGFILE4}" ]
	then
		cp -f "${CFGFILE4}" "${CFGFILE4}".old
	fi
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 -a "${KERNEL_2_5}" = "true" ]
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

	# Skip backup and RCS files; fixes bug 20597 (07 May 2004 agriffis)
	[[ ${cfg} == *~ || ${cfg} == *.bak || ${cfg} == *,v ]] && 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 -a "${KERNEL_2_5}" = "true" ]
then
	# Make sure that generate-modprobe.conf can handle --assume-kernel
	# if we were called with it.
	if [[ -n ${ASSUME_KV} ]] && ! grep -qe --assume-kernel \
		/sbin/generate-modprobe.conf
	then
		eerror "Error: modules-update called with --assume-kernel flag, but"
		eerror "generate-modprobe.conf doesn't understand it.  You need to"
		eerror "install >=module-init-tools-3.0-r2"
		exit 3
	fi
	if /sbin/generate-modprobe.conf ${ASSUME_KV:+--assume-kernel=${KV}} \
		>> "${TMPFILE2}" 2> /dev/null
	then
		mv -f "${TMPFILE2}" "${CFGFILE2}"
	else
		ewarn "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 ${ASSUME_KV:+--assume-kernel=${KV}} \
			>> "${TMPFILE4}" 2> /dev/null
		then
			mv -f "${TMPFILE4}" "${CFGFILE4}"

			echo >> "${CFGFILE4}"
			echo "include /etc/modprobe.conf" >> "${CFGFILE4}"
		else
			ewarn "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
	if [ -f /usr/src/linux/System.map ]; then
		depmod -a -F /usr/src/linux/System.map ${KV}
	else
		ewarn "System.map not found - unable to check symbols"
	fi
fi


# vim:ts=4