#!/system/bin/sh
# Runs the Rust thermal HAL in place of the installed one until the next reboot. The framework
# reconnects to the HAL on its own, so no framework restart is needed. Run as root from this
# folder's copy on the phone. Nothing is written to /vendor.
[ -n "$TH_NS" ] || exec nsenter -t 1 -m -- env TH_NS=1 sh "$0" "$@"
D=$(cd "${0%/*}" && pwd)
SVC=vendor.thermal-hal
NAME=android.hardware.thermal-service.qti
BIN=/vendor/bin/hw/$NAME
T=/mnt/penguin_thermal
REPORT=$D/report-$(date +%H%M%S).txt

stats() {
    pid=$(pidof $NAME)
    echo "$1: pid $pid threads $(ls /proc/$pid/task 2>/dev/null | wc -l)" \
        "$(grep -E '^(Pss|Rss):' /proc/$pid/smaps_rollup 2>/dev/null | tr -s ' ' | tr '\n' ' ')"
}

# The userspace trip points the HAL programs (skin and socd).
trips() {
    for z in /sys/class/thermal/thermal_zone*; do
        case "$(cat $z/type)" in
        sys-therm-2|socd)
            echo "$(cat $z/type) temp $(cat $z/temp) trip1 $(cat $z/trip_point_1_temp 2>/dev/null)" \
                "hyst1 $(cat $z/trip_point_1_hyst 2>/dev/null)" ;;
        esac
    done
}

restore() {
    if [ "$(getprop init.svc.$SVC)" != running ]; then
        echo "FAILED: restoring the installed HAL"
        umount -l $BIN 2>/dev/null
        stop $SVC
        sleep 1
        start $SVC
    fi
}
trap restore EXIT

cd "$D" || exit 1
grep -E " $NAME$" SHA256SUMS | sha256sum -c - || exit 1
if grep -q " $BIN " /proc/mounts; then
    echo "Already swapped; run revert-thermal-rust.sh first"
    exit 1
fi

{
    stats before
    trips
    echo "== dumpsys thermalservice (installed HAL)"
    dumpsys thermalservice
} > "$REPORT"

mkdir -p $T
grep -q " $T " /proc/mounts || mount -t tmpfs -o mode=0755 penguin_thermal $T || exit 1
cp $NAME $T/ || exit 1
chmod 755 $T/$NAME
chcon "$(ls -Z $BIN | cut -d' ' -f1)" $T/$NAME || exit 1

# Bind first, then restart: servicemanager restarts vendor.thermal-hal as soon as it stops
# (IThermal is a declared interface), so a stop before the bind relaunches the old binary.
mount --bind $T/$NAME $BIN || exit 1
stop $SVC
sleep 1
start $SVC
sleep 5
if [ "$(stat -L -c %s /proc/$(pidof $NAME)/exe)" != "$(stat -c %s $T/$NAME)" ]; then
    echo "FAILED: the running HAL is not the Rust build"
fi

{
    echo "service: $(getprop init.svc.$SVC)"
    stats after
    trips
    logcat -d -s thermal_hal | tail -12
    echo "== dumpsys thermalservice (Rust HAL)"
    dumpsys thermalservice
} >> "$REPORT"
grep -E "^(before|after|service)|sys-therm-2|socd|thermal_hal" "$REPORT"

# Pass criterion: the framework sees the same sensors and thresholds as with the installed HAL.
# A threshold or zone mismatch here can mean a thermal power-off, so it must be empty.
section() { sed -n "/== dumpsys thermalservice ($1)/,/^== /p" "$REPORT" | grep -E "TemperatureThreshold|mName=" |
    sed -E 's/mValue=[-0-9.]+//; s/mStatus=[0-9]+//' | sort; }
section "installed HAL" > $D/sensors-before.txt
section "Rust HAL" > $D/sensors-after.txt
if diff $D/sensors-before.txt $D/sensors-after.txt > $D/sensors-diff.txt; then
    echo "PASS: same sensors and thresholds ($(wc -l < $D/sensors-after.txt) lines)"
else
    echo "MISMATCH: sensors or thresholds differ, see $D/sensors-diff.txt"
    cat $D/sensors-diff.txt | head -40
fi
echo "Full report: $REPORT"
