#!/system/bin/sh

echoit ()
{
echo "$1" | fold -s -w 45
}
who=`whoami`
if [ "$who" != "root" ]
 then
 echoit "batt-cfg must be ran as root."
 echoit "Press enter to attempt to run as root"
 echoit "Or press 1 then enter to continue without attempting to gain root access"
 echoit ""
 read opt
 if [ "$opt" -eq "1" ] 
  then
	echoit "Continuing..."
 else
 su -c "batt-cfg"
 echoit "Exited batt-cfg as root"
 exit
 fi
fi

show_available_schedulers()
{
echoit "Available schedulers"
cat /sys/block/mtdblock3/queue/scheduler
echo ""
}

show_available_governors()
{
echoit "Available Governors:"
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors
echoit ""
}

show_freq_table()
{
echoit "Available Frequencies:"
awk < /sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state '{ print $1 " " }' | awk -v RS="\n" -v ORS="" {print} | fold -s -w 40 
echoit ""
}

#Added USBpower definitions and options by Decad3nce

#this may seem redundant because they are included in load_defaults
#this is only here in case the person upgraded from a prior version
#sets sensible defaults in that case
audio_fix=0
LEDfix=1
CFStweaks=0
MOUNToptions=0
enabled=1
#end of sensible upgrade defaults
. /system/etc/batt.conf

#disabled replaced with enabled
#disabled=0
#CFStweaks=0

toggle()
{
if [ "$1" -gt "0" ]
   then
	 echo 0
	 else
 	 echo 1
fi
}

get_disp()
{
if [ "$1" -gt "0" ]
   then
	 echoit "Disable"
	 else
	 echoit "Enable"
fi
}

load_defaults()
{
audio_fix=0
LEDfix=1
MOUNToptions=1
CFStweaks=0
enabled=1
cpu_max_underclock_perc=33
cpu_limiting_method=1
max_freq_on_battery=691200
max_freq_on_USBpower=691200
polling_interval_on_battery=60
polling_interval_on_USBpower=15
polling_interval_on_power=15
min_freq_on_battery=245000
min_freq_on_power=245000
min_freq_on_USBpower=245000
}


restart()
{
echoit "Starting batt.sh"
#nohup /system/bin/batt.sh 2>/dev/nul &
start collin_ph
t=`pidof batt.sh`
if [ "$t" ]
   then
	 echoit "Started"
   else
	 nohup /system/bin/batt.sh 2>/dev/nul &
	 echoit "NOTE: when you exit ADB or terminal, you may have to hit CTRL-C"
fi

}
customize()
{
echoit ""
echoit ""
echoit ""

echoit "You will be asked a series of configuration questions. After Each option, enter your selection and press enter. Leave blank and press enter to keep the current setting."
echoit ""

echoit "The CPU limiting method is either underclock or powersave bias. Underclock reclocks the cpu to a lower speed to conserve battery. Powersave bias limits individual processes CPU usage to conserve battery."
echoit "";

echoit "[1] Underclock (highly recommended)"
echoit "[2] Powersave Bias"
echoit "Make a choice and hit enter, or leave blank to leave unchanged [$cpu_limiting_method]"
read new_cpu_limiting_method
echoit ""
case $new_cpu_limiting_method in
        "1") cpu_limiting_method=1;;
        "2") cpu_limiting_method=2;;
				*) echoit "setting unchanged";;
esac

echoit ""
echoit ""
echoit ""
echoit ""
echoit ""
echoit ""
echoit ""
echoit "The cpu scaling governor controls how/when your cpu scales on the frequency table"
echoit "";

show_available_governors
echoit "Make a choice and hit enter, or leave blank to leave unchanged [$scaling_governor]"
read new_scaling_governor
echoit ""
tscale=$scaling_governor
if [ "$new_scaling_governor" ] 
   then 
   tscale=$new_scaling_governor
fi
scaling_governor=$tscale

echoit ""
echoit ""
echoit ""
echoit ""
echoit ""
echoit ""
echoit ""
echoit "The cpu scheduler controls how the system tasks are scheduled"
echoit "";

show_available_schedulers
echoit "Make a choice and hit enter, or leave blank to leave unchanged [$cpu_scheduler]"
read new_cpu_scheduler
echoit ""
tsched=$cpu_scheduler
if [ "$new_cpu_scheduler" ] 
   then 
   tsched=$new_cpu_scheduler
fi
cpu_scheduler=$tsched

echoit ""
echoit ""
echoit ""
echoit ""
echoit ""
echoit ""
echoit ""
echoit "The Min and Max frequency settings determine how fast the CPU runs. The Min frequency must be less than or equal to the Max."
echoit "Some phones do not support settings higher than 6912000."
echoit ""
#no longer works. 
#cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies
#replaced with
show_freq_table

echoit ""
echoit "Choose a Min CPU setting while ON BATTERY or enter [$min_freq_on_battery]"
read new_min_freq_on_battery
echoit ""
echoit "Choose a Max CPU setting while ON BATTERY or enter [$max_freq_on_battery]"
read new_max_freq_on_battery
echoit ""
tminf=$min_freq_on_battery
if [ "$new_min_freq_on_battery" ] 
   then 
   tminf=$new_min_freq_on_battery
fi
tmaxf=$max_freq_on_battery
if [ "$new_max_freq_on_battery" ]
   then
	 tmaxf=$new_max_freq_on_battery
fi

if [ $tmaxf -ge $tminf ]
   then
	 min_freq_on_battery=$tminf
	 max_freq_on_battery=$tmaxf
	 echoit "Battery CPU settings accepted."
	 else
	 echoit "Invalid Battery CPU Settings. New Battery CPU settings ignored."
fi

echoit ""
echoit ""
echoit ""
echoit ""
echoit "The Min and Max frequency settings determine how fast the CPU runs. The Min frequency must be less than or equal to the 480000 on USB. Some phones do not support settings higher than 691200."
echoit ""
show_freq_table
echoit ""
echoit "Choose a Min CPU setting while ON USB POWER or enter [$min_freq_on_USBpower]"
read new_min_freq_on_USBpower
echoit ""
echoit "Choose a Max CPU setting while ON USB POWER or enter [$max_freq_on_USBpower]"
read new_max_freq_on_USBpower
echoit ""
tminf=$min_freq_on_USBpower
if [ "$new_min_freq_on_USBpower" ] 
   then 
   tminf=$new_min_freq_on_USBpower
fi
tmaxf=$max_freq_on_USBpower
if [ "$new_max_freq_on_USBpower" ]
   then
	 tmaxf=$new_max_freq_on_USBpower
fi

if [ "$tmaxf" -ge "$tminf" ]
   then
	 min_freq_on_USBpower=$tminf
	 max_freq_on_USBpower=$tmaxf
	 echoit "USB Power CPU settings accepted."
	 else
	 echoit "Invalid USB Power CPU Settings. New USB Power CPU settings ignored."
fi

echoit ""
echoit ""
echoit ""
echoit ""
echoit ""
echoit ""
echoit ""
echoit ""

echoit "Set CPU settings on AC POWER. Note: These settings differ from USB charging. You can freely allow the Min CPU setting to be higher than 480000"
echoit ""
show_freq_table
echoit "Choose a Min CPU setting while ON AC POWER or enter [$min_freq_on_power]"
read new_min_freq_on_power
echoit ""
echoit "Choose a Max CPU setting while ON AC POWER or enter [$max_freq_on_power]"
read new_max_freq_on_power
echoit ""

tminf=$min_freq_on_power
if [ "$new_min_freq_on_power" ] 
   then 
   tminf=$new_min_freq_on_power
fi
tmaxf=$max_freq_on_power
if [ "$new_max_freq_on_power" ]
   then
	 tmaxf=$new_max_freq_on_power
fi

if [ "$tmaxf" -ge "$tminf" ]
   then
	 min_freq_on_power=$tminf
	 max_freq_on_power=$tmaxf
	 echoit "Power CPU settings accepted."
	 else
	 echoit "Invalid AC Power CPU Settings. New Power CPU settings ignored."
fi

echoit ""
echoit ""
echoit ""
echoit ""
echoit ""
echoit ""
echoit ""
echoit ""

echoit "The batt.sh service will poll the system perodically to detect the power state. The delay between polls (in seconds) is called the polling interval. To save battery, this interval should be longer when on battery. When on AC power, the interval is less. This helps quickly adjust the settings when the power is removed. The lower the intervals, the more CPU will be used."

echoit "Enter the polling interval (in seconds) when on battery: [$polling_interval_on_battery]"
read new_polling_interval_on_battery
echoit ""
if [ "$new_polling_interval_on_battery" -gt 1 ] 
   then
	 polling_interval_on_battery=$new_polling_interval_on_battery
else
   echoit "Interval not updated"
fi

echoit "Enter the polling interval (in seconds) when on USB power: [$polling_interval_on_USBpower]"
read new_polling_interval_on_USBpower
echoit ""
if [ "$new_polling_interval_on_battery" -gt 1 ] 
   then
	 polling_interval_on_USBpower=$new_polling_interval_on_USBpower
else
   echoit "Interval not updated"
fi

echoit "Enter the polling interval (in seconds) when on AC power: [$polling_interval_on_power]"
read new_polling_interval_on_power
echoit ""
if [ "$new_polling_interval_on_battery" -gt 1 ] 
   then
	 polling_interval_on_power=$new_polling_interval_on_power
else
   echoit "Interval not updated"
fi



echoit ""
echoit ""
echoit ""
echoit ""
echoit ""
echoit ""
echoit ""
echoit ""
echoit "The CPU or process speed will be limited as the battery drains.When fully charged or charging, the CPU will operate at full speed. When nearly dead, the cpu will be reduced by a certain percentage. This percentage must be a whole number and not contain a % sign."
echoit ""
echoit "Please enter the maximum percentage to reduce the CPU by [$cpu_max_underclock_perc]"
read new_cpu_max_underclock_perc
t=`expr "$new_cpu_max_underclock_perc" ">" 1`
if [ "$t" -gt 0 ] 
   then
	 cpu_max_underclock_perc=$new_cpu_max_underclock_perc  
else
   echoit "CPU Underclock percentage not updated."
fi
echoit ""

save_settings
}

load_768()
{
echoit "Loading 768mhz defaults"
load_defaults
max_freq_on_power=768000
max_freq_on_USBpower=768000
save_settings
}

load_691()
{
echoit "Loading 691mhz defaults"
load_defaults
max_freq_on_power=691200
max_freq_on_USBpower=691200
save_settings
}

save_settings()
{
tpid=`pidof batt.sh`
if [ "$tpid" -gt 0 ]
 then
 echoit "Killing batt.sh process"
 kill -9 $tpid
 fi

echoit "Saving Settings..."
mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system
echo "audio_fix=$audio_fix" > /system/etc/batt.conf
echo "CFStweaks=$CFStweaks" >> /system/etc/batt.conf
echo "MOUNToptions=$MOUNToptions" >>  /system/etc/batt.conf
echo "enabled=$enabled" >> /system/etc/batt.conf
echo "LEDfix=$LEDfix" >> /system/etc/batt.conf
echo "cpu_limiting_method=$cpu_limiting_method" >> /system/etc/batt.conf
echo "min_freq_on_battery=$min_freq_on_battery" >> /system/etc/batt.conf
echo "max_freq_on_battery=$max_freq_on_battery" >> /system/etc/batt.conf
echo "min_freq_on_USBpower=$min_freq_on_USBpower" >> /system/etc/batt.conf
echo "max_freq_on_USBpower=$max_freq_on_USBpower" >> /system/etc/batt.conf
echo "min_freq_on_power=$min_freq_on_power" >> /system/etc/batt.conf
echo "max_freq_on_power=$max_freq_on_power" >> /system/etc/batt.conf
echo "polling_interval_on_battery=$polling_interval_on_battery" >> /system/etc/batt.conf
echo "polling_interval_on_power=$polling_interval_on_power" >> /system/etc/batt.conf
echo "polling_interval_on_USBpower=$polling_interval_on_USBpower" >> /system/etc/batt.conf
echo "cpu_max_underclock_perc=$cpu_max_underclock_perc " >> /system/etc/batt.conf
echo "scaling_governor=$scaling_governor" >> /system/etc/batt.conf
echo "cpu_scheduler=$cpu_scheduler" >> /system/etc/batt.conf
mount -o remount,ro -t yaffs2 /dev/block/mtdblock3 /system

echoit ""
echoit "Settings Saved."
if [ "$enabled" ]
   then
   restart
fi
exit
}

while [ 1 ]
do

echoit ""
echoit ""
echoit ""
echoit ""
echoit ""
echoit ""
echoit ""
echoit ""
echoit ""
echoit ""

echoit "The batt-cfg utility configures the collin_ph battery tweak. When on USB/AC power, maximum performance settings will be used. When on battery, more conservative settings will be used. As the battery drains, the CPU clock will be lowered. The CPU Scales between min and max settings always. Additional timing and memory settings are adjusted."
echoit ""
echoit ""
echoit ""

echoit "Please choose an option and press enter."
echoit "[1] Revert to 768mhz defaults and exit"
echoit "[2] Revert to 691mhz defaults and exit"
echoit "[3] Customize settings"


disp=`get_disp $enabled`
echoit "[4] $disp Battery Tweak Entirely"
disp=`get_disp $CFStweaks`
echoit "[5] $disp CFS Tweaks "
disp=`get_disp $LEDfix`
echoit "[6] $disp LED Fix (If LED Stuck)"
disp=`get_disp $MOUNToptions`
echoit "[7] $disp Disk Boost"
disp=`get_disp $audio_fix`
disp2=`toggle $audio_fix`
disp2=`get_disp $disp2`
echoit "[8] $disp Audio Fix ($disp2 Stagefright)"
echoit "[9] Save Changes & Exit"
echoit "[Enter]: Exit Without Saving Changes"
read choice
echoit ""
echoit ""

  case $choice in
        "1") load_768;;
        "2") load_691;;
				"3") customize;;
				"4") enabled=`toggle $enabled`;;
				"5") CFStweaks=`toggle $CFStweaks`;;
				"6") LEDfix=`toggle $LEDfix`;;
        "7") MOUNToptions=`toggle $MOUNToptions`;;
				"8") audio_fix=`toggle $audio_fix`;;
				"9") save_settings;;
				*) echoit "Exiting. No Changes Saved"
				   exit;; 
     esac
done

