#! /bin/sh -e
#
# moblin-kernel-modules: Moblin Settings required kernel modules
#
# chkconfig: - 10 21
# description:  This script loads all the kernel modules that the \
#               moblin settings daemon needs to operate.
#
### BEGIN INIT INFO
# Provides: moblin-kernel-modules
# Required-Start: 
# Required-Stop: 
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Load and unload the kernel modules
# Description: Load and unload the kernel modules
### END INIT INFO

# Check for ACPI support on kernel side
[ -d /proc/acpi ] || exit 0

# Get lsb functions
. /lib/lsb/init-functions
. /etc/default/rcS

if [ "x$VERBOSE" = "xno" ]; then
        MODPROBE_OPTIONS="$MODPROBE_OPTIONS -Q"
        export MODPROBE_OPTIONS
fi

load_modules() {
        PRINTK=`cat /proc/sys/kernel/printk`
        [ "$VERBOSE" = no ] && echo "0 0 0 0" > /proc/sys/kernel/printk
        
        LIST=`/sbin/lsmod|awk '!/Module/ {print $1}'`

	# Get list of available modules
        LOC="/lib/modules/`uname -r`/kernel/drivers/acpi"
        if [ -d $LOC ]; then
	  MODAVAIL=`( find $LOC -type f -name "*.o" -printf "basename %f .o\n"; \
		find $LOC -type f -name "*.ko" -printf "basename %f .ko\n" ) | /bin/sh`
	else
	  MODAVAIL=""
	fi
	MODULES="$MODAVAIL"

	if [ -n "$MODULES" ]; then
		log_begin_msg "Loading ACPI modules..."
		STATUS=0
	        for mod in $MODULES; do
			echo $MODAVAIL | grep -q -w "$mod" || continue
		        if echo $LIST | grep -q -w "$mod"; then
				[ "$VERBOSE" != no ] && log_success_msg "Module already loaded: $mod"
			else
				if modprobe -b $mod 2>/dev/null; then
					[ "$VERBOSE" != no ] && log_success_msg "Loaded module: $mod"
				else
					if [ "$VERBOSE" != no ]; then
						log_warning_msg "Unable to load module: $mod"
					fi
				fi
			fi		
	        done
		log_end_msg $STATUS
	fi
        echo "$PRINTK" > /proc/sys/kernel/printk
}

unload_modules() {
        PRINTK=`cat /proc/sys/kernel/printk`
        [ "$VERBOSE" = no ] && echo "0 0 0 0" > /proc/sys/kernel/printk
        
        LIST=`/sbin/lsmod|awk '!/Module/ {print $1}'`

	# Get list of available modules
        LOC="/lib/modules/`uname -r`/kernel/drivers/acpi"
        if [ -d $LOC ]; then
	  MODAVAIL=`( find $LOC -type f -name "*.o" -printf "basename %f .o\n"; \
		find $LOC -type f -name "*.ko" -printf "basename %f .ko\n" ) | /bin/sh`
	else
	  MODAVAIL=""
	fi
	MODULES="$MODAVAIL"

	if [ -n "$MODULES" ]; then
		log_begin_msg "Unloading ACPI modules..."
		STATUS=0
	        for mod in $MODULES; do
			echo $MODAVAIL | grep -q -w "$mod" || continue
		        if echo $LIST | grep -q -w "$mod"; then
				if modprobe -r $mod 2>/dev/null; then
					[ "$VERBOSE" != no ] && log_success_msg "Unloaded module: $mod"
				else
					if [ "$VERBOSE" != no ]; then
						log_warning_msg "Unable to unload module: $mod"
					fi
				fi
			else
				[ "$VERBOSE" != no ] && log_success_msg "Module already unloaded: $mod"
			fi		
	        done
		log_end_msg $STATUS
	fi
        echo "$PRINTK" > /proc/sys/kernel/printk
}

case "$1" in
  start)
    [ -f /proc/modules ] && load_modules
    ;;
  stop)
#    [ -f /proc/modules ] && unload_modules
    ;;
  restart)
    $0 stop
    sleep 1
    $0 start
    ;;
  reload|force-reload) 
    ;;
  *)
    log_success_msg "Usage: /etc/init.d/moblin-applets {start|stop|restart|reload|force-reload}"
    exit 1
esac

exit 0
