#!/bin/sh
# Load eeePC-specific modules
#
# chkconfig: 2345 35 65
# description:  Load modules which are useful \
#               on EeePCs and similar hardware
#
# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

# exit if package not installed
test -f /usr/share/eeepc-acpi-scripts/functions.sh || exit 0

# exit if eeepc-laptop isn't loaded (should be loaded by udev)
test -d /sys/bus/platform/devices/eeepc || exit 0

PATH="/sbin:/bin"

. /etc/init.d/functions

LOCKFILE=/var/lock/subsys/eeepc-acpi-scripts
RETVAL=0

load_module ()
{
  action "  $1:" modprobe $@
}

case "$1" in
  start|restart|reload|force-reload)

    # First, get the kernel version.

    KERNEL="`uname -r`"
    case "$KERNEL" in
      2.6.*)
	KERNEL="`echo $KERNEL | sed -re 's/^([0-9]+\.){2}([0-9]+).*$/\2/'`"
	;;
      *)
	KERNEL=0
        ;;
    esac

    # Now load the modules. We ignore failure since it's possible that
    # they're built into the running kernel.

    echo 'Loading EeePC support modules'

    # Load pciehp if required.
    # There are three recognised cases:
    # - kernel 2.6.26 & older: two parameters required
    # - kernel 2.6.27 & .28  : one of those parameters has been removed
    # - kernel 2.6.29 & newer: hotplugging is handled in eeepc-laptop except on the
    #                          EeePC 900A model
    # - kernel 2.6.32 & newer: eeepc-laptop also works on the EeePC 900A
    if [ -d /sys/module/pciehp ]; then
      # Hmm, already present
      if [ "$KERNEL" -ge 29 -a "`cat /sys/class/dmi/id/product_name`" != "900A" ]; then
        # 2.6.29 and newer on all but the EeePC 900A - unload pciehp if loaded
        action 'Module "pciehp" is loaded; trying to unload' modprobe -r pciehp
      elif [ "$KERNEL" -ge 32 ]; then
        # 2.6.32 and newer on all EeePC models - unload pciehp if loaded
        action 'Module "pciehp" is loaded; trying to unload' modprobe -r pciehp
      fi
    else
      # Load it if needed
      if [ "$KERNEL" -lt 27 ]; then
        # 2.6.26 and older
        load_module pciehp pciehp_force=1 pciehp_slot_with_bus=1
      elif [ "$KERNEL" -lt 29 ]; then
        # 2.6.27 and 2.6.28
        load_module pciehp pciehp_force=1
      elif [ "$KERNEL" -lt 32 -a "`cat /sys/class/dmi/id/product_name`" = "900A" ]; then
        # 2.6.29 to 2.6.31 on the EeePC 900A
        load_module pciehp pciehp_force=1
      fi
    fi

    # Load rfkill-input if possible, i.e. kernel is 2.6.28, .29 or .30.
    # This results in faster WLAN hw toggling.
    if [ "$KERNEL" -ge 28 -a "$KERNEL" -le 30 ]; then
      if grep -q '^H.*\brfkill\b' /proc/bus/input/devices; then
	:
      else
        load_module rfkill-input
      fi
    fi

    # Done.

    if [ -f /etc/acpi/lib/shengine.sh ]; then
	echo -n  'Setting super hybrid engine according to configuration'
	if [ -f /etc/default/eeepc-acpi-scripts ]; then
	    . /etc/default/eeepc-acpi-scripts
	fi
	. /etc/acpi/lib/shengine.sh
        if shengine_supported; then
            if [ "${SHENGINE_SETTING:-auto}" != auto ]; then
                success 'set_shengine (manual)'
            elif [ "$(cat /sys/class/power_supply/AC0/online 2>/dev/null)" = 0 ]; then
                echo -n '(battery)'
                set_shengine "${PWR_CLOCK_BATTERY:-$(($SHENGINE_LIMIT - 1))}"  && echo_success 'set_shengine (battery)' || failure 'set_shengine (battery)' ; echo
            else
                echo -n '(AC)'
                set_shengine "${PWR_CLOCK_AC:-0}" && success 'set_shengine (AC)' || failure 'set_shengine (AC)' ; echo
            fi
        else
            echo -n '(model not supported)'
        fi
    fi
    ;;

  condrestart|condstop|stop)
    # Nothing to do.
    ;;

  *)
    msg_usage "${0##*/} {start|stop|restart|reload|force-reload}"
    RETVAL=1
    ;;
esac

exit $RETVAL
