#!/system/bin/sh

. ./_readconf

if [ "${cpufreq_max_freq}" != "" ]; then
  cpufreq_scaling_max_freq="${cpufreq_max_freq}"
fi
if [ "${cpufreq_min_freq}" != "" ]; then
  cpufreq_scaling_min_freq="${cpufreq_min_freq}"
fi

COUNT=0

while [ ${COUNT} -le 600 ]; do
  if [ -f /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor ]; then
    GOV=`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor`
    if [ "${GOV}" != "performance" ]; then
      break
    fi
  fi
  sleep 5
  COUNT=`expr ${COUNT} + 5`
done

if [ "${cpufreq_scaling_governor}" != "" -a -f /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor ]; then
  echo "${cpufreq_scaling_governor}" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
fi
if [ "${cpufreq_scaling_max_freq}" != "" -a -f /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq ]; then
  echo "${cpufreq_scaling_max_freq}" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
fi
if [ "${cpufreq_scaling_min_freq}" != "" -a -f /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq ]; then
  echo "${cpufreq_scaling_min_freq}" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
fi
if [ "${cpufreq_scaling_setspeed}" != "" -a -f /sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed ]; then
  echo "${cpufreq_scaling_setspeed}" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed
fi

if [ "${cpufreq_scaling_governor}" == "smartassV2" ]; then
  governor_suffix="smartass"
elif [ "${cpufreq_scaling_governor}" == "interactiveX" ]; then
  governor_suffix="${cpufreq_scaling_governor}"
else
  governor_suffix="`echo ${cpufreq_scaling_governor} | tr [A-Z] [a-z]`"
fi

if [ -d /sys/devices/system/cpu/cpufreq/${governor_suffix} ]; then
  governor_dir=/sys/devices/system/cpu/cpufreq/${governor_suffix}
elif [ -d /sys/devices/system/cpu/cpu0/cpufreq/${governor_suffix} ]; then
  governor_dir=/sys/devices/system/cpu/cpu0/cpufreq/${governor_suffix}
fi

for v in `ls ${governor_dir}`; do
  eval s="\$cpufreq_${governor_suffix}_${v}"
  if [ "${s}" != "" ]; then
    echo ${s} > ${governor_dir}/${v}
  fi
done

if [ "${cpufreq_boost_cpufreq}" != "" -a -f /sys/devices/system/cpu/cpu0/cpufreq/boost_cpufreq ]; then
  echo "${cpufreq_boost_cpufreq}" > /sys/devices/system/cpu/cpu0/cpufreq/boost_cpufreq
fi
