#! /bin/sh -e

GOV_PATH=/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
AC_STATE_PATH=/proc/acpi/ac_adapter/AC/state
LAPTOP_MODE_PATH=/proc/sys/vm/laptop_mode

test -f "$GOV_PATH" || exit 0
test -f "$AC_STATE_PATH" || exit 0
test -f "$LAPTOP_MODE_PATH" || exit 0

case "$1" in
  start)
    echo -n 'Initiating laptop power settings:'
    echo ondemand > "$GOV_PATH"
    echo -n ' cpufreq'
    if grep  'off-line' "$AC_STATE_PATH" > /dev/null; then
      echo 1 > "$LAPTOP_MODE_PATH"
      echo -n ' laptop_mode'
    fi
    echo '.'
    ;;
  stop)
    echo -n 'Disabling laptop power settings:'
    echo 0 > "$LAPTOP_MODE_PATH"
    echo -n ' laptop_mode'
    echo performance > "$GOV_PATH"
    echo ' cpufreq.'
    ;;
  reload|force-reload|restart)
    $0 stop
    sleep 1
    $0 start
    ;;
  *)
    echo "Usage: /etc/init.d/laptop_powersave {start|stop|restart|reload|force-reload}"
    exit 1
esac

exit 0

