#!/bin/sh /etc/rc.common
START=60

NAME=amlogic

uci_get_by_type() {
    local ret=$(uci get $NAME.@$1[0].$2 2>/dev/null)
    echo ${ret:=$3}
}

uci_set_by_type() {
    uci set $NAME.@$1[0].$2=$3 2>/dev/null
    uci commit $NAME
}

start() {
    [ -x "/usr/sbin/fixcpufreq.pl" ] && /usr/sbin/fixcpufreq.pl && sync
    local cpu_policys=$(ls /sys/devices/system/cpu/cpufreq 2>/dev/null | grep -E 'policy[0-9]{1,3}' | xargs)
    if [ "${cpu_policys}" = "" ]; then
        cpu_policys="policy0"
    fi

    config_load $NAME
    for policy_name in ${cpu_policys}; do
        local policy_id="${policy_name//policy/}"

        # Get an optional value list for the current device
        local governor_list="$(cat /sys/devices/system/cpu/cpufreq/${policy_name}/scaling_available_frequencies 2>/dev/null | xargs)"
        local second_place_order="$(echo ${governor_list} | awk '{print $1}')"
        local second_place_reverse="$(echo ${governor_list} | awk '{print $NF}')"

        # Get the default value in the Config file
        local governor=$(uci_get_by_type settings governor${policy_id} schedutil)
        local minfreq=$(uci_get_by_type settings minfreq${policy_id} ${second_place_order})
        local maxfreq=$(uci_get_by_type settings maxfreq${policy_id} ${second_place_reverse})

        # Update result to the corresponding file
        echo $governor >/sys/devices/system/cpu/cpufreq/${policy_name}/scaling_governor
        echo $minfreq >/sys/devices/system/cpu/cpufreq/${policy_name}/scaling_min_freq
        echo $maxfreq >/sys/devices/system/cpu/cpufreq/${policy_name}/scaling_max_freq

        # If the governor is ondemand, configure its specific parameters.
        if [ "$governor" = "ondemand" ]; then
            local ondemand_dir="/sys/devices/system/cpu/cpufreq/${policy_name}/ondemand"
            # Check if the per-policy ondemand directory exists.
            if [ -d "$ondemand_dir" ]; then
                # Read ondemand parameters from UCI, or use default values (e.g., 80 and 20) if not set.
                local up_threshold=$(uci_get_by_type settings up_threshold${policy_id} 80)
                local sampling_down_factor=$(uci_get_by_type settings sampling_down_factor${policy_id} 20)

                # Write the values to the system files.
                echo $up_threshold > "${ondemand_dir}/up_threshold"
                echo $sampling_down_factor > "${ondemand_dir}/sampling_down_factor"
            fi
        fi
    done
}

reload() {
    start
    return 0
}
