#!/system/bin/sh
TMP_ICON="/data/local/tmp/rairin.webp"
safe_write() {
local file="$1"
local value="$2"
[ -f "$file" ] || return 1
chmod 644 "$file"
echo "$value" > "$file"
chmod 444 "$file"
}
log() {
    local msg="$1"
    echo "$(date '+%Y-%m-%d %H:%M:%S') | $msg" >> /data/adb/modules/RaiRin-AI/thermal.log
}

su -lp 2000 -c "cmd notification post -S bigtext -t 'RaiRin-AI' RaiRin 'Game Detected — Adjusting Thermal in 40s'"
sleep 1
su -lp 2000 -c "/system/bin/cmd notification post \
    -t 'RaiRin-AI' \
    -i file://$TMP_ICON \
    -I file://$TMP_ICON \
    'RaiRin' 'Game Detected, Adjusting Thermal in 40s'"
echo "disable" > /data/local/tmp/RaiRin-AI/thermal-stat.txt    
sleep 40
reset_thermal_props() {
log "Resetting thermal props..."
rpairs=(
"debug.thermal.throttle.support no"
"ro.transsion.thermal_status_watcher.support 0"
"ro.vendor.tran_thermal_switch.support 0"
"ro.esports.thermal_config.support 0"
"ro.esports.thermal_config.version 0"
"ro.tran_thermal_cc_support 0"
"ro.dar.thermal_core.support 0"
"dalvik.vm.dexopt.thermal-cutoff 0"
"ro.vendor.tran.hbm.thermal.temp.clr 99999"
"ro.vendor.tran.hbm.thermal.temp.trig 99999"
"ro.vendor.powerhal.thermal_ux_support 0"
"persist.vendor.powerhal.thermal_ux_temp_max 999999"
"persist.vendor.powerhal.thermal_ux_temp_min 35000"
"ro.mtk_thermal_monitor.enabled false"
"ro.config.thermal_limit 999999"
"ro.config.thermal_dcs 35"
"persist.thermal_config.mitigation 0"
#"ro.vendor.mtk_thermal_2_0 0"
"persist.vendor.thermal.disabled 1"
)

for pair in "${rpairs[@]}"; do  
    key=$(echo "$pair" | awk '{print $1}')  
    val=$(echo "$pair" | awk '{print $2}')  
    if [ -n "$(getprop "$key")" ]; then  
        resetprop "$key" "$val"  
        log "[apply] $key → $val"  
    else  
        log "[skip] $key (not found)"  
    fi  
done

}

stop_thermal_services() {
log "Checking and stopping thermal services if running..."
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)"  
    if [ "$state" = "running" ] || [ "$state" = "restarting" ]; then  
        setprop ctl.stop "$svc"  
        log "[stopped] $svc"  
    else  
        log "[skip] $svc (state: ${state:-not found})"  
    fi  
done

}

meruhi() {
# Stop semua thermal service dengan aman
stop_thermal_services

# Override semua trip-point thermal  
for zone in /sys/class/thermal/thermal_zone*; do  
    for tp in "$zone"/trip_point_*_temp; do  
        safe_write "$tp" 9999999  
    done  
done  

# Reset semua properti thermal  
reset_thermal_props

}
meruhi
setprop sys.rairin.thermal.state extreme