#!/system/bin/sh

## Exit if module not loaded
[ -d /proc/overclock ] || exit

## Autoload Milestone Overclock settings

/system/xbin/mount -o ro /dev/block/mmcblk0p1 /sdcard
[ $? -eq 0 ] || exit

## Autoload Milestone Overclock settings
# Milestone Overclock saves it's autoload values inside:
pathprefs=/data/data/pt.com.darksun.milestoneoverclock/shared_prefs/MilestoneOverclockPrefs.xml
# Milestone Overclocks saves the autoload preference by creating or
# destroying this file on the sdcard:
pathautoload=/sdcard/Android/data/pt.com.darksun.milestoneoverclock/files/autoload

getMilestoneOverclockPref () {
    tr -dc '[[:alnum:]>"]' < $pathprefs \
| tr \> \\n \
| egrep "^longname\"$1\"value\"[0-9]+\"$" \
| cut -f 4 -d \"
}

## Make sure module is loaded and that autoload is requested
# Beware! It's possible the user had autoload set/saved on the sdcard
# from a previous ROM but has no preferences! So check for both.
if [ -f $pathautoload -a -f $pathprefs ] ; then
    # Extract max_rate, max_vsel values from Milestone Overclock
    # preferences
    autoloadRate=$(getMilestoneOverclockPref autoloadRate)
    autoloadVsel=$(getMilestoneOverclockPref autoloadVsel)

    # Ensure both values were successfully read
    if [ ! -z "$autoloadRate" -a ! -z "$autoloadVsel" ] ; then
echo $autoloadRate > /proc/overclock/max_rate
echo $autoloadVsel > /proc/overclock/max_vsel
log -p w set milestone overclock max_rate=$autoloadRate
log -p w set milestone overclock max_vsel=$autoloadVsel
    fi
fi

umount /sdcard
