#!/bin/sh

find_pwm_paths() {
    for hwmon in /sys/class/hwmon/hwmon*; do
        if [ -d "$hwmon" ]; then
            for pwm in "$hwmon"/pwm*; do
                if ! echo "$pwm" | grep -q "_enable"; then
                    if [ -f "$pwm" ]; then
                        echo "$pwm"
                    fi
                fi
            done
        fi
    done
}

find_pwm_enable_paths() {
    for hwmon in /sys/class/hwmon/hwmon*; do
        if [ -d "$hwmon" ]; then
            for pwm_enable in "$hwmon"/pwm*_enable; do
                if [ -f "$pwm_enable" ]; then
                    echo "$pwm_enable"
                fi
            done
        fi
    done
}

find_thermal_zones() {
    for zone in /sys/class/thermal/thermal_zone*; do
        if [ -d "$zone" ]; then
            if [ -f "$zone/temp" ]; then
                type=$(cat "$zone/type" 2>/dev/null || echo "unknown")
                echo "$zone/temp $type"
            fi
        fi
    done
}

validate_static_config() {
    local current_pwm_path=$(uci -q get luci-fan.@luci-fan[0].pwm_path)
    local current_thermal_zone=$(uci -q get luci-fan.@luci-fan[0].thermal_zone_cpu)
    local current_modem_method=$(uci -q get luci-fan.@luci-fan[0].modem_temp_method)
    
    local old_modem_at_port=$(uci -q get luci-fan.@luci-fan[0].modem_at_port)
    if [ -n "$old_modem_at_port" ]; then
        uci -q delete luci-fan.@luci-fan[0].modem_at_port
    fi
    
    if [ -z "$current_modem_method" ] || [ "$current_modem_method" = "AT" ]; then
        uci set luci-fan.@luci-fan[0].modem_temp_method='AUTO'
    fi
    
    if [ ! -f "$current_pwm_path" ] || [ ! -f "$current_thermal_zone" ]; then
        local new_pwm_path=$(find_pwm_paths | head -n 1)
        local new_thermal_zone=""
        
        local cpu_thermal=$(find_thermal_zones | grep -i "cpu" | head -n 1 | cut -d ' ' -f 1)
        if [ -n "$cpu_thermal" ]; then
            new_thermal_zone="$cpu_thermal"
        else
            new_thermal_zone=$(find_thermal_zones | head -n 1 | cut -d ' ' -f 1)
        fi
        
        if [ ! -f "$current_pwm_path" ]; then
            if [ -n "$new_pwm_path" ]; then
                uci set luci-fan.@luci-fan[0].pwm_path="$new_pwm_path"
            else
                uci set luci-fan.@luci-fan[0].enabled='0'
            fi
        fi
        
        if [ ! -f "$current_thermal_zone" ]; then
            if [ -n "$new_thermal_zone" ]; then
                uci set luci-fan.@luci-fan[0].thermal_zone_cpu="$new_thermal_zone"
            else
                uci set luci-fan.@luci-fan[0].enabled='0'
            fi
        fi
        
        local current_pwm_enable_path=$(uci -q get luci-fan.@luci-fan[0].pwm_enable_path)
        if [ ! -f "$current_pwm_enable_path" ]; then
            local new_pwm_enable_path=$(find_pwm_enable_paths | head -n 1)
            if [ -n "$new_pwm_enable_path" ]; then
                uci set luci-fan.@luci-fan[0].pwm_enable_path="$new_pwm_enable_path"
            else
                uci -q delete luci-fan.@luci-fan[0].pwm_enable_path
            fi
        fi
        
        return 0
    fi
    
    return 1
}

setup_modem_scripts() {
    mkdir -p /usr/share/fan-control/modem/usb
    
    for script in /usr/share/fan-control/modem/usb/*; do
        if [ -f "$script" ]; then
            chmod +x "$script"
        fi
    done
    
    if [ -f "/usr/share/fan-control/common.sh" ]; then
        chmod +r "/usr/share/fan-control/common.sh"
    fi
}

[ ! -x /etc/init.d/luci-fan ] && chmod +x /etc/init.d/luci-fan
[ ! -x /usr/libexec/fan-control ] && chmod +x /usr/libexec/fan-control

setup_modem_scripts

if validate_static_config; then
    uci commit luci-fan
fi

if [ -x /usr/libexec/fan-control ]; then
    /usr/libexec/fan-control init
fi

uci -q batch <<-EOF >/dev/null
	delete ucitrack.@luci-fan[-1]
	add ucitrack luci-fan
	set ucitrack.@luci-fan[-1].init=luci-fan
	commit ucitrack
EOF

if [ -x /etc/init.d/luci-fan ]; then
    /etc/init.d/rpcd restart
    /etc/init.d/luci-fan enable
    
    if [ "$(uci -q get luci-fan.@luci-fan[0].enabled)" = "1" ]; then
        /etc/init.d/luci-fan start
    else
        /etc/init.d/luci-fan stop
    fi
fi

exit 0