#!/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 3 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, see <http://www.gnu.org/licenses/>

# (c) Antonio Sánchez León <introlinux[fix@]gmail[fix.]com>
# (c) Miguel Anxo Bouzada <mbouzada[fix@]gmail[fix.]com>
# (c) GALPon MinNo Team <minino[fix@]galpon[fix.]org>
# july 2012

## -

case $LANG in
  ca*)
    Powersave="_Estalvi d'energia"
    Ondemand="_Sota demanda"
    Performance="_Millor rendiment"
  ;;
  gl*)
    Powersave="_Aforro de enerxía" 
    Ondemand="_Baixo demanda"
    Performance="_Mellor rendemento"
  ;;
  es*)
    Powersave="_Ahorro de energía" 
    Ondemand="_Bajo demanda"
    Performance="_Mejor rendimiento"
  ;;
  *)
    Powersave="Power_save" 
    Ondemand="_Ondemand"
    Performance="_Performance"
  ;;
esac

modulos=`lsmod |grep freq`

if [ "$modulos" = "" ];then
    sudo /etc/init.d/cpufrequtils start
    sudo /etc/init.d/loadcpufreq start
fi

ACTUAL_FREQ=0
FREQ=0
NUM_STEPS=0
TEMP_FILE="/tmp/freqfly"
rm $TEMP_FILE 2>&1

#modificando la última f me da la frecuencia 1, la 2, la 3...
#cpufreq-info|grep "available frequency steps"|head -n1|cut -d":" -f2|tr -d ' '|cut -d',' -f1

#esto nos daría cuántas frecuencias tenemos en total
NUM_STEPS=`cpufreq-info|grep "available frequency steps"|head -n1|cut -d":" -f2|tr -d ' '|grep -o ","|wc -l`
NUM_STEPS=$(($NUM_STEPS+1))

#consigue la freq actual:
ACTUAL_FREQ=`cpufreq-info |grep "current CPU frequency"|head -n1|tr -d ' '|cut -d 's' -f2|rev|cut -c2-|rev`



for ((i = 1; i <= $NUM_STEPS; i++)); do
	
	FREQ=`cpufreq-info|grep "available frequency steps"|head -n1|cut -d":" -f2|tr -d ' '|cut -d',' -f$i`
	echo "item=$FREQ">>$TEMP_FILE
	echo "cmd=sudo cpufreq-set -f $FREQ">>$TEMP_FILE
	if [ $ACTUAL_FREQ = $FREQ ];then

		echo "icon=/usr/local/share/pixmaps/selected.png">>$TEMP_FILE
	else
		echo "icon=">>$TEMP_FILE
	fi
done

echo "SEPARATOR">>$TEMP_FILE

echo "item=${Powersave}">>$TEMP_FILE
echo "cmd=sudo cpufreq-set -g powersave">>$TEMP_FILE
echo "icon=/usr/local/share/pixmaps/powersave.png">>$TEMP_FILE
echo "item=${Ondemand}">>$TEMP_FILE
echo "cmd=sudo cpufreq-set -g ondemand">>$TEMP_FILE
echo "icon=/usr/local/share/pixmaps/ondemand.png">>$TEMP_FILE
echo "item=${Performance}">>$TEMP_FILE
echo "cmd=sudo cpufreq-set -g performance">>$TEMP_FILE
echo "icon=/usr/local/share/pixmaps/performance.png">>$TEMP_FILE

mygtkmenu $TEMP_FILE
