#!/bin/bash

# PowerManagement Control Script
# This script will be called on certain events from a
# powermanagement daemon with two arguments. The first
# argument contains the command and the second the
# currently active power source.
#
# arg $1: powersave     minimum power consumption
#         performance   maximum performance
#         sleep         prepare for sleep
#         wakeup        recovering from sleep
#
# arg $2: ac            running on ac power
#         battery       running on battery
#
# Options for this scripts will be kept in the central
# configuration file ../config. Variables for each script
# will be introduced by a special unique prefix.

# name        : cpufreq
# prefix      : CPUFREQ_
# author      : Matthias Grimm <joker@cymes.de>
# description : Set processor frequency
# requirements: 
# limitations : only first CPU supported 
#
# --- end of public part -- don't change below this line ---

PATH=/bin:/sbin:/usr/bin:/usr/sbin

# source configuration
. config

KVER=`uname -r`

case "$1" in
  powersave)
    case "$KVER" in
      2.6.*)
        if [ -d /sys ]; then
          echo -n "userspace" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
          cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq > /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
          cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
          cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq > /sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed
        elif [ -f /proc/cpufreq ]; then
          echo -n "0:0:0:powersave" > /proc/cpufreq
	elif [ -d /proc/sys/cpu ]; then
          cat /proc/sys/cpu/0/speed-min > /proc/sys/cpu/0/speed
	fi
        ;;
      2.4.*)
        if [ -d /proc/sys/cpu ]; then
          cat /proc/sys/cpu/0/speed-min > /proc/sys/cpu/0/speed
	fi
        ;;
    esac
   ;;
  performance)
     case "$KVER" in
      2.6.*)
        if [ -d /sys ]; then
          echo -n "userspace" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
          cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq > /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
          cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
          cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq > /sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed
        elif [ -f /proc/cpufreq ]; then
          echo -n "1%100%100%performance" > /proc/cpufreq
	elif [ -d /proc/sys/cpu ]; then
           cat /proc/sys/cpu/0/speed-max > /proc/sys/cpu/0/speed
	fi
        ;;
      2.4.*)
        if [ -d /proc/sys/cpu ]; then
          cat /proc/sys/cpu/0/speed-max > /proc/sys/cpu/0/speed
	fi
        ;;
    esac
   ;;
  sleep)
    ;;
  wakeup)
    ;;
esac

exit 0

