#!/system/bin/sh
# B14 check: FusionLight must start the lazy ISensorFeature service itself after a multihal
# restart. Binds the fixed sensors.fusionlight.so until reboot; nothing is written to /vendor.
# nsenter starts in /, so re-exec by absolute path.
case $0 in /*) SELF=$0 ;; *) SELF=$PWD/$0 ;; esac
[ -n "$FL_NS" ] || exec nsenter -t 1 -m -- env FL_NS=1 sh "$SELF" "$@"
D=$(cd "${0%/*}" && pwd)
SVC=vendor.sensors-hal-multihal
LIB=/vendor/lib64/sensors.fusionlight.so
T=/mnt/penguin_fusionlight
cd "$D" || exit 1
grep " sensors.fusionlight.so$" SHA256SUMS | sha256sum -c - || exit 1
grep -q " $LIB " /proc/mounts && { echo "Already bound; run revert-fusionlight-b14.sh first"; exit 1; }
mkdir -p $T
grep -q " $T " /proc/mounts || mount -t tmpfs -o mode=0755 penguin_fusionlight $T || exit 1
cp sensors.fusionlight.so $T/ && chmod 644 $T/sensors.fusionlight.so || exit 1
chcon "$(ls -Z $LIB | cut -d' ' -f1)" $T/sensors.fusionlight.so || exit 1
# Make sure the lazy service is not running, so the old checkService() lookup would miss it.
stop vendor.oplusSensor-aidl-1
sleep 1
echo "oplusSensor before: $(getprop init.svc.vendor.oplusSensor-aidl-1)"
mount --bind $T/sensors.fusionlight.so $LIB || exit 1
logcat -c
stop $SVC
start $SVC
sleep 10
echo "multihal: $(getprop init.svc.$SVC), maps: $(grep -c "$LIB" /proc/$(pidof android.hardware.sensors-service.oplus-multihal)/maps) mappings"
echo "running lib inode $(stat -L -c %i $LIB), staged $(stat -c %i $T/sensors.fusionlight.so)"
logcat -d | grep -E "ISensorFeature|IDisplayPanelFeature|FusionLight calibration|Loaded FusionLight" | tail -10
if logcat -d | grep -q "ISensorFeature is unavailable"; then
    echo "FAIL: ISensorFeature still unavailable"
elif logcat -d | grep -q "ISensorFeature::getSensorCalibrationData returned"; then
    echo "PASS: ISensorFeature reached after the lazy service was stopped"
else
    echo "UNKNOWN: no ISensorFeature lines (profile may not use screen captures); check logcat"
fi
echo "Light sensor check: dumpsys sensorservice | grep -i -A2 'fusion\|light'"
