#!/bin/bash
# Strip CRLF and re-exec if needed (fix Windows line endings)
if grep -q $'\r' "$0" 2>/dev/null; then
    exec bash -s "$@" < <(sed 's/\r$//' "$0")
fi
# Linux sensor/thermal info collector
# Works on generic Linux (Ubuntu, Debian, etc.) and Chromebox with Linux
# Run: chmod +x linux_collect_sensors.sh && ./linux_collect_sensors.sh

OUTDIR="/tmp"
[ -w . ] 2>/dev/null && OUTDIR="."
TS=$(date +%Y%m%d_%H%M%S)
OUT="$OUTDIR/sensor_report_$TS.txt"

exec 1> >(tee -a "$OUT") 2>&1

echo "=========================================="
echo "Linux Sensor/Thermal Report"
echo "Date: $(date)"
echo "Host: $(hostname)  $(uname -r)"
echo "=========================================="
echo ""

echo "--- 1. Thermal zones (/sys/class/thermal) ---"
if [ -d /sys/class/thermal ]; then
    for tz in /sys/class/thermal/thermal_zone*; do
        [ -d "$tz" ] || continue
        n=$(basename "$tz")
        type=$(cat "$tz/type" 2>/dev/null)
        temp=$(cat "$tz/temp" 2>/dev/null)
        trip_0=$(cat "$tz/trip_point_0_temp" 2>/dev/null) || trip_0=""
        if [ -n "$temp" ] && [ "$temp" != "0" ]; then
            c=$((temp / 1000))
            echo -n "$n: $type = ${c} C"
            [ -n "$trip_0" ] && [ "$trip_0" != "0" ] && echo -n " (trip_point_0: $((trip_0 / 1000)) C)"
            echo
        else
            echo "$n: $type = N/A"
        fi
    done
else
    echo "/sys/class/thermal not found"
fi
echo ""

echo "--- 2. Hwmon devices ---"
if [ -d /sys/class/hwmon ]; then
    for h in /sys/class/hwmon/hwmon*; do
        [ -d "$h" ] || continue
        name=$(cat "$h/name" 2>/dev/null)
        echo "--- $h ($name) ---"
        for t in "$h"/temp*_input; do
            [ -f "$t" ] || continue
            bn=$(basename "$t" _input)
            label=$(cat "$h/${bn}_label" 2>/dev/null || echo "${bn}")
            val=$(cat "$t" 2>/dev/null)
            [ -n "$val" ] && echo "  $label: $((val / 1000)) C"
        done
        [ -z "$(ls "$h"/temp*_input 2>/dev/null)" ] && echo "  (no temp inputs)"
    done
else
    echo "/sys/class/hwmon not found"
fi
echo ""

echo "--- 3. lm-sensors (sensors) ---"
if command -v sensors >/dev/null 2>&1; then
    sensors 2>/dev/null || echo "sensors command failed"
else
    echo "sensors not installed (apt install lm-sensors)"
fi
echo ""

echo "--- 4. ectool (EC sensors, Chromebox/Chrome EC) ---"
if command -v ectool >/dev/null 2>&1; then
    out=$(ectool temps 2>&1) && echo "$out" || echo "ectool temps failed"
    out=$(ectool tempsinfo 2>&1) && echo "$out"
    out=$(ectool pwmgetfanrpm all 2>&1) && echo "$out"
    out=$(ectool pwmgetduty 0 2>&1) && echo "$out"
else
    echo "ectool not found (skip on non-Chromebox)"
fi
echo ""

echo "--- 5. dmesg (thermal, hwmon, cros_ec, sensor, peci) ---"
dmesg 2>/dev/null | grep -iE 'thermal|hwmon|sensor|cros_ec|peci|intel.*temp' | tail -100 || echo "dmesg not available (need root?)"
echo ""

echo "--- 6. CPU info ---"
grep -E 'model name|Hardware' /proc/cpuinfo 2>/dev/null | head -2
echo ""

echo "=========================================="
echo "Report saved to: $OUT"
echo "=========================================="
