#!/system/bin/sh
# Runs the Rust UDFPS sensor (sensors.oplus.so) and the multihal without the dead ALS
# correction in place of the installed ones until the next reboot. SensorService reconnects to
# the sensors HAL by itself. Run as root from this folder's copy. Nothing is written to /vendor.
# nsenter starts in /, so re-exec by absolute path.
case $0 in /*) SELF=$0 ;; *) SELF=$PWD/$0 ;; esac
[ -n "$SN_NS" ] || exec nsenter -t 1 -m -- env SN_NS=1 sh "$SELF" "$@"
D=$(cd "${0%/*}" && pwd)
SVC=vendor.sensors-hal-multihal
NAME=android.hardware.sensors-service.oplus-multihal
BIN=/vendor/bin/hw/$NAME
LIB=/vendor/lib64/sensors.oplus.so
T=/mnt/penguin_sensors
REPORT=$D/report-$(date +%H%M%S).txt
# dodge does not ship sensors.oplus.so (no optical UDFPS): swap only the multihal there.
HAS_LIB=0; [ -e $LIB ] && HAS_LIB=1

stats() {
    pid=$(pidof $NAME)
    echo "$1: pid $pid exe $(ls -l /proc/$pid/exe 2>/dev/null | sed 's/.*-> //') 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' ' ')"
}
sensors() { dumpsys sensorservice | sed -n '/Sensor List:/,/^$/p' | sed -E 's/last [0-9]+ events.*//' | grep -vE '^[[:space:]]+[0-9]+ \(ts=' ; }

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

cd "$D" || exit 1
grep -E " ($NAME|sensors.oplus.so)$" SHA256SUMS | sha256sum -c - || exit 1
[ $HAS_LIB = 1 ] || echo "$LIB is not installed: UDFPS sub-HAL not applicable, swapping only the multihal"
if grep -q " $BIN " /proc/mounts; then
    echo "Already swapped; run revert-sensors-rust.sh first"
    exit 1
fi

{ stats before; echo "== sensors before"; sensors; } > "$REPORT"

mkdir -p $T
grep -q " $T " /proc/mounts || mount -t tmpfs -o mode=0755 penguin_sensors $T || exit 1
cp $NAME $T/ || exit 1
chmod 755 $T/$NAME
chcon "$(ls -Z $BIN | cut -d' ' -f1)" $T/$NAME || exit 1
if [ $HAS_LIB = 1 ]; then
    cp sensors.oplus.so $T/ && chmod 644 $T/sensors.oplus.so || exit 1
    chcon "$(ls -Z $LIB | cut -d' ' -f1)" $T/sensors.oplus.so || exit 1
fi

# Bind first: servicemanager relaunches the HAL the moment it stops.
if [ $HAS_LIB = 1 ]; then mount --bind $T/sensors.oplus.so $LIB || exit 1; fi
mount --bind $T/$NAME $BIN || exit 1
stop $SVC
start $SVC
sleep 6

{
    echo "service: $(getprop init.svc.$SVC)"
    stats after
    echo "running exe sha $(sha256sum /proc/$(pidof $NAME)/exe 2>/dev/null | cut -c1-16), staged $(sha256sum $T/$NAME | cut -c1-16)"
    logcat -d -s sensors.oplus | tail -5
    echo "== sensors after"; sensors
} >> "$REPORT"

sed -n '/== sensors before/,/== sensors after/p' "$REPORT" | grep -v "^==" | sort > $D/sensors-before.txt
sed -n '/== sensors after/,$p' "$REPORT" | grep -v "^==" | sort > $D/sensors-after.txt
grep -E "^(before|after|service|running)" "$REPORT"
if diff $D/sensors-before.txt $D/sensors-after.txt > $D/sensors-diff.txt; then
    echo "PASS: same sensor list ($(grep -c . $D/sensors-after.txt) lines)"
else
    echo "MISMATCH: sensor list differs, see $D/sensors-diff.txt"; head -30 $D/sensors-diff.txt
fi
if [ $HAS_LIB = 1 ]; then
    grep -q "UDFPS Sensor" $D/sensors-after.txt && echo "UDFPS sensor listed" || echo "UDFPS sensor MISSING"
fi
echo "Full report: $REPORT"
