#!/bin/sh
#=================================================================
# Begin cpufre
#
# Description : Procedure to handle CPU working frequencies
#
#=================================================================

### BEGIN INIT INFO
# Provides:            $cpufreq
# Should-Start:        
# Required-Stop:       
# Should-Stop:         
# Default-Start:       3 4 5
# Default-Stop:        0 1 2 6
# Short-Description:   Starts cpufreq.
# Description:         Set starting CPU working frequency
### END INIT INFO

. /lib/lsb/init-functions
#loading local CPUSPEED Value
. /etc/sysconfig/cpufreq.conf

govern()

{
GOVERN=/sys/devices/system/cpu
NPROC=`/usr/bin/getconf _NPROCESSORS_ONLN`

STATUS=0
case "${1}" in
  "powersave"	|			\
  "userspace"	|			\
  "ondemand"	|			\
  "performance"	)
    num=0;
    log_info_msg "CPU speed set to \"${1}\"..."
    while [ $num -lt $NPROC ] ;
      do
      if [ ! -f $GOVERN/cpu$num/cpufreq/scaling_governor ] ; then
	STATUS=1
	break;
	fi
      echo ${1} > $GOVERN/cpu$num/cpufreq/scaling_governor
      ((num++))
      done
    ;;
  *		)
    echo "Cpu speed ${1} not available"
    STATUS=1
    ;;
  esac
return $STATUS
}

#loading speed control module
for mod in cpufreq_ondemand cpufreq_powersave cpufreq_conservative
  do
  modprobe $mod
  done

if [ -z "$CPUSPEED" ] ; then
  CPUSPEED=userspace
  fi

case "$1" in
   "start"	)
      govern $CPUSPEED
      evaluate_retval
      ;;

   "stop"	)
      govern powersave
      evaluate_retval
      ;;

   "restart"	)
      $0 stop
      sleep 1
      $0 start
      ;;

   *)
      echo "Usage: $0 {start|stop|restart}"
      exit 1
      ;;
esac

# End /etc/init.d/cpufreq
