#!/sbin/runscript
# Copyright 1999-2003 Gentoo Technologies, Inc.
# Distributed under the terms of the GNU General Public License v2
# $Header: /home/cvsroot/gentoo-src/rc-scripts/init.d/modules,v 1.22 2003/03/11 02:30:22 azarah Exp $


depend() {
	need checkroot hostname
	use isapnp
}

load_modules() {
	[ -z "$1" ] && return 0
	[ ! -r "$1" ] && return 0

	# Loop over every line in $1
	(egrep -v "^#|^$" "$1"; echo) | # make sure there is a LF at the end
	while read module args
	do
		[ -z "${module}" ] && continue
		ebegin "  Loading module ${module}"
		modprobe -q ${module} ${args} &>/dev/null
		eend $? "  Failed to load ${module}"
	done
	echo
	
	return 0
}

start() {
	# Should not fail if kernel do not have module
	# support compiled in ...
	[ -f /proc/modules ] || return 0
	
	# Here we should fail, as a modular kernel do need
	# depmod command ...
	if [ ! -x /sbin/depmod ]
	then
		eerror "ERROR:  system is missing /sbin/depmod !"
		return 1
	fi
	
	ebegin "Calculating module dependencies"
	/sbin/modules-update &>/dev/null
	eend $? "Failed to calculate dependencies"

	if [ -f /etc/modules.autoload -a ! -L /etc/modules.autoload ]
	then
		einfo "Using /etc/modules.autoload:"
		# Loop over every line in /etc/modules.autoload.
		load_modules /etc/modules.autoload
	else
		local KV="`uname -r 2> /dev/null`"
		local KV_MAJOR="`KV_major "${KV}"`"
		local KV_MINOR="`KV_minor "${KV}"`"

		# New support for /etc/modules.autoload/kernel-$KV
		if [ "$(get_KV)" -ge "$(KV_to_int '2.5.0')" ] && \
		   [ -f /etc/modules.autoload.d/kernel-"${KV_MAJOR}.${KV_MINOR}" ]
		then
			einfo "Using /etc/modules.autoload.d/kernel-${KV_MAJOR}.${KV_MINOR}:"
			load_modules /etc/modules.autoload.d/kernel-"${KV_MAJOR}.${KV_MINOR}"
		else
			einfo "Using /etc/modules.autoload.d/kernel-2.4:"
			load_modules /etc/modules.autoload.d/kernel-2.4
		fi
	fi

	#
	# Just in case a sysadmin prefers generic symbolic links in
	# /lib/modules/boot for boot time modules we will load these modules
	#
	if [ -n "$(modprobe -l -t boot)" ]
	then
		modprobe -a -t boot \*  &>/dev/null
	fi
}


# vim:ts=4
