#! /bin/sh
### BEGIN INIT INFO
# Provides:          kldutils module-init-tools
# Required-Start:
# Required-Stop:
# Should-Start:      $local_fs
# Should-stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Process /etc/modules.
# Description:       Load the modules listed in /etc/modules.
### END INIT INFO
#
# skeleton	example file to build /etc/init.d/ scripts.
#		This file should be used to construct scripts for /etc/init.d.
#
#		Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#		Modified for Debian 
#		by Ian Murdock <imurdock@gnu.ai.mit.edu>.
#
# Version:	@(#)skeleton  1.9  26-Feb-2001  miquels@cistron.nl
#

set -e

PATH=/sbin:/bin

do_start() {
	for i in sysctl kldload kldstat kldunload ; do
		which ${i} >/dev/null || exit 1
	done

	# This syctl is (supposedly) correct in kernel, but kfreebsd-loader enjoys messing with it
	sysctl "kern.module_path=/lib/modules/`uname -r`;/boot/kernel"

	# Load modules
	for file in /etc/modules /etc/modules.d/* ; do
		if ! test -f ${file} ; then
			continue
		fi
		modules="`sed -e 's/#.*//g' -e '/^\( \|\t\)*$/d' ${file}`"
		for module in ${modules} ; do
			if ! kldstat -q -m ${module} 2>/dev/null ; then
				echo "Loading ${module} ..."
				kldload ${module} || true
				echo "... done."
			else
				echo "Not loading ${module} (already loaded)"
			fi
		done
	done
}

case "$1" in
  start|"")
	do_start
	;;
  restart|reload|force-reload)
	echo "Error: argument '$1' not supported" >&2
	exit 3
	;;
  stop)
	# No-op
	;;
  *)
	echo "Usage: $0 [start|stop]" >&2
	exit 3
	;;
esac

exit 0
