#!/system/bin/sh
SAVE_DIR="/data/local/tmp/defaults/thermal"
mkdir -p "$SAVE_DIR"

echo "[INFO] Saving thermal trip-points..."
for zone in /sys/class/thermal/thermal_zone*; do
    name=$(basename "$zone")

    for tp in "$zone"/trip_point_*_temp; do
        [ -f "$tp" ] || continue
        tp_name=$(basename "$tp")
        cat "$tp" > "$SAVE_DIR/${name}.${tp_name}"
    done
done

echo "[INFO] Saving thermal-related properties..."
props=(
    "debug.thermal.throttle.support"
    "ro.transsion.thermal_status_watcher.support"
    "ro.vendor.tran_thermal_switch.support"
    "ro.esports.thermal_config.support"
    "ro.esports.thermal_config.version"
    "ro.tran_thermal_cc_support"
    "ro.dar.thermal_core.support"
    "dalvik.vm.dexopt.thermal-cutoff"
    "ro.vendor.tran.hbm.thermal.temp.clr"
    "ro.vendor.tran.hbm.thermal.temp.trig"
    "ro.vendor.powerhal.thermal_ux_support"
    "persist.vendor.powerhal.thermal_ux_temp_max"
    "persist.vendor.powerhal.thermal_ux_temp_min"
    "ro.mtk_thermal_monitor.enabled"
    "ro.config.thermal_limit"
    "ro.config.thermal_dcs"
    "persist.thermal_config.mitigation"
    "ro.vendor.mtk_thermal_2_0"
    "persist.vendor.thermal.disabled"
    "persist.vendor.powerhal.thermal_ux_temp_max 999999"
    "persist.vendor.powerhal.thermal_ux_temp_min 35000"
)

for key in "${props[@]}"; do
    val="$(getprop "$key")"
    [ -n "$val" ] && echo "$val" > "$SAVE_DIR/prop.$key"
done

echo "[INFO] Saving thermal services status..."
services=(
    "thermald"
    "thermal_core"
    "vendor.thermal-hal-2-0.mtk"
    "vendor.thermal-engine"
    "vendor.power-hal-1-0"
    "vendor.thermal-mediatek"
    "vendor.mtk_thermal_2_0"
)
for svc in "${services[@]}"; do
    state="$(getprop init.svc.$svc)"
    echo "$state" > "$SAVE_DIR/svc.$svc"
done

touch "$SAVE_DIR/.saved"
echo "[INFO] Thermal defaults saved to $SAVE_DIR"