#! /bin/bash

#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

# 2009-11-27  Marcos Daniel Henning  <marcoshenning@gmail.com>

# Readme:
# Este script es algo parecido a un governador de CPU, lo que hace es
# descender la velocidad del nucleo cuando la temperatura alcanza cierto
# nivel de "calentura".ja!

# Configuraciones

# Valores de frecuencia seguros, donde la temperatura no se va al
# joraca.
cpu_freq_gov="conservative"
cpu_freq_khz=1600000

# Valor de Temperatura máximo desde el cual se va a utilizar la 
# configuración de frecuencia anterior.
cpu_temp_threshold=85  

# Bucle principal del proceso.
sem=0
while [ 1 ]
do
	temp=(`cat /proc/acpi/thermal_zone/THRM/temperature`)
	if [ ${temp[1]} -ge $cpu_temp_threshold ]; then
		echo 'Ups, temperatura sobre el limite ('$cpu_temp_threshold') '${temp[1]}' C'
		echo 'Seteando frecuencia a' $cpu_freq_khz
		cpufreq-set -f $cpu_freq_khz
		sleep 15 #espero para que haya un descenso en la temperatura.
		cpufreq-set -g $cpu_freq_gov
		sem=1
	fi
	
	# Si esta todo frio le pongo ondemand
	if [ $sem -eq 1 ]; then
		if [ ${temp[1]} -le 60 ]; then
			echo 'Todo tranquilo '${temp[1]}' C, set OnDemand'
			cpufreq-set -g ondemand
			sem=0
		fi
	fi
sleep 1
done

