#!/bin/sh
### BEGIN INIT INFO
# Provides: coldplug
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: coldplug
# Description: Script for adding new modules to /etc/modules, \
#       for don't depend to hotplug/udevd correctly works
### END INIT INFO

#=============================================================================#
#                    Add modules for loading to modules.conf                  #
#=============================================================================#
# (c) Denis Smirnov <mithraen@freesource.info>                                #
#=============================================================================#
# + must be run _before_ sound
# + must be run _before_ hotplug
# + must be run _after_ mounting /usr (for comm and uniq utilites)
set +e

WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

#PIDFILE=/var/run/template.pid
#LOCKFILE=/var/lock/subsys/template
RETVAL=0

start()
{
T_NEW1=`mktemp`
T_NEW=`mktemp`
T_DIFF=`mktemp`

/sbin/modprobe msr

# for I2C
echo i2c-dev >> $T_NEW1

# Get list for needed modules
> $T_NEW1
T_NEW_TMP=`mktemp`
# This strange code _not_ write output of this utilites, if they return non zero
/usr/bin/acpiscan -r	 > $T_NEW_TMP && cat $T_NEW_TMP >> $T_NEW1
/usr/bin/cpuscan -r	 > $T_NEW_TMP && cat $T_NEW_TMP >> $T_NEW1
/usr/bin/mousescan -r	 > $T_NEW_TMP && cat $T_NEW_TMP >> $T_NEW1
/usr/bin/pciscan -r	 > $T_NEW_TMP && cat $T_NEW_TMP >> $T_NEW1
rm -f $T_NEW_TMP

/bin/cat $T_NEW1 \
	| tr ' ' '\n' \
	| /bin/sort \
	| /usr/bin/uniq \
	> $T_NEW
rm -f $T_NEW1

T_OLD=`mktemp`
cat /etc/modules \
	| /bin/grep -v '^#' \
	| /bin/grep . \
	| /bin/sort \
	| /usr/bin/uniq \
	> $T_OLD

/usr/bin/comm -13 $T_OLD $T_NEW  > $T_DIFF
/bin/rm -f $T_OLD $T_NEW

if [ -s $T_DIFF ]; then
	# Loading modules

	# sync HDD -- loading new modules can hang computer
	/bin/sync
	/bin/sync
	sleep 1
	/bin/cat $T_DIFF \
		| /bin/xargs -n1 /sbin/modprobe
	sleep 3

	# if don't hang now, then we can load this modules every time when boot
	echo "# Added by coldplug @ `date -I`" >> /etc/modules
	/bin/cat $T_DIFF >> /etc/modules
fi

	RETVAL=$?
	return $RETVAL
}

stop()
{
#	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user root -- template
	RETVAL=$?
	return $RETVAL
}

restart()
{
#	stop
	start
}

reload()
{
#	msg_reloading template
#	stop_daemon --pidfile "$PIDFILE" --expect-user root -HUP -- template
	RETVAL=$?
	return $RETVAL
} 

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload)
		reload
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		start
#		if [ -e "$LOCKFILE" ]; then
#			restart
#		fi
		;;
	condreload)
		if [ -e "$LOCKFILE" ]; then
			reload
		fi
		;;
	status)
		status --pidfile "$PIDFILE" --expect-user root -- template
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

