#!/usr/bin/env bash
# Post-flash gates for a Vanta build (any device profile), over adb. Read-only: it
# never toggles Wi-Fi or anything else that could cut the adb link it runs on.
# Usage: tools/gate.sh [-s serial]     ADB=... overrides the adb binary.
# Exit 0 only if every automatic gate passes. Writes evidence JSON to out/.
set -uo pipefail
ADB=${ADB:-adb}
SER=(); [ "${1:-}" = "-s" ] && SER=(-s "$2")
P=$(cd "$(dirname "$0")/.." && pwd)
sh_() { "$ADB" "${SER[@]}" shell "$@" 2>/dev/null | tr -d '\r'; }
res=(); fails=0
g() { # id, description, pass(0/1), detail
  if [ "$3" = 1 ]; then echo "PASS  $1 $2"; else echo "FAIL  $1 $2 -- $4"; fails=$((fails+1)); fi
  res+=("{\"id\":\"$1\",\"pass\":$([ "$3" = 1 ] && echo true || echo false),\"detail\":\"${4//\"/\'}\"}")
}
"$ADB" "${SER[@]}" root >/dev/null 2>&1; "$ADB" "${SER[@]}" wait-for-device 2>/dev/null

v=$(sh_ getprop sys.boot_completed); g G1.boot "system booted" "$([ "$v" = 1 ] && echo 1 || echo 0)" "sys.boot_completed=$v"
v=$(sh_ getprop ro.vanta.version); g G1.vanta "Vanta layer in the image" "$([ -n "$v" ] && echo 1 || echo 0)" "ro.vanta.version=$v"
v=$(sh_ getprop ro.build.version.release); g G1.a17 "Android 17" "$([ "$v" = 17 ] && echo 1 || echo 0)" "release=$v"
dev=$(sh_ getprop ro.vanta.device)
echo "      device: ${dev:-unknown}"

if [ "$dev" = beyond2lte ]; then
  v=$(sh_ cmd overlay lookup android android:integer/config_num_physical_slots)
  g G2.slots "framework sees two physical SIM slots" "$([ "$v" = 2 ] && echo 1 || echo 0)" "config_num_physical_slots=$v"
fi
v=$(sh_ dumpsys connectivity | grep -c -E 'Transports: CELLULAR.*VALIDATED|CELLULAR.*Capabilities:.*VALIDATED')
g G2.data "a VALIDATED cellular network exists" "$([ "${v:-0}" -ge 1 ] && echo 1 || echo 0)" "validated cellular networks=$v (insert SIM, mobile data on)"

v=$(sh_ settings get secure vanta_identity_version); g G3.applied "identity applied once" "$([ "${v:-0}" -ge 1 ] 2>/dev/null && echo 1 || echo 0)" "vanta_identity_version=$v"
v=$(sh_ settings get secure theme_customization_overlay_packages)
g G3.accent "Monet accent 35E0FF" "$(case "$v" in *35E0FF*) echo 1;; *) echo 0;; esac)" "theme json=${v:0:120}"
g G3.font "IBM Plex named in the theme JSON" "$(case "$v" in *font.ibmplex*) echo 1;; *) echo 0;; esac)" "theme json=${v:0:120}"
v=$(sh_ cmd overlay list android | grep -c '\[x\] com.failure.vanta.overlay.framework')
g G3.overlay "framework overlay enabled" "$([ "${v:-0}" = 1 ] && echo 1 || echo 0)" "enabled=$v"
v=$(sh_ settings get global window_animation_scale)
g G3.motion "animation scale 0.75 on a fresh install" "$([ "$v" = 0.75 ] && echo 1 || echo 0)" "window_animation_scale=$v"

v=$(sh_ cmd overlay lookup android android:bool/config_isDesktopModeSupported)
g G4.desktop "desktop windowing supported (DP-alt glasses)" "$([ "$v" = true ] && echo 1 || echo 0)" "config_isDesktopModeSupported=$v"
v=$(sh_ cmd package resolve-activity --brief -a android.intent.action.MAIN -c android.intent.category.HOME | tail -1)
g G5.home "Vanta Home is the default home, with no chooser" "$(case "$v" in com.failure.vanta.home/*) echo 1;; *) echo 0;; esac)" "HOME resolves to $v"
if [ "$dev" = b5q ]; then
  v=$(sh_ getprop ro.vanta.foldable)
  g G6.cover "build knows it has a cover panel (Hub shows the Cover page)" "$([ "$v" = true ] && echo 1 || echo 0)" "ro.vanta.foldable=$v"
fi
if [ "$dev" = beyond2lte ]; then
  v=$(sh_ 'dmesg 2>/dev/null | grep -c -i kernelsu')
  g G7.root "KernelSU-Next in the kernel" "$([ "${v:-0}" -ge 1 ] && echo 1 || echo 0)" "kernelsu dmesg lines=$v"
fi

O="${GATE_OUT:-$P/out}"; mkdir -p "$O"; f="$O/gate-$(date -u +%Y%m%dT%H%M%SZ).json"
( IFS=,; echo "{\"fails\":$fails,\"gates\":[${res[*]}]}" ) > "$f"
cat <<'EOT'
MANUAL (eyes, not adb): boot animation plays; panel + touch; both speakers + mic
  in a call; fingerprint enrols; glasses on DP-alt open a desktop.
  Flip5: close the phone, wake the cover, unlock: the Vanta cover face shows
  (clock, swipe for notifications / media / controls / apps).
EOT
echo "evidence: $f"
[ "$fails" = 0 ] && echo "gate: PASS" || echo "gate: FAIL ($fails)"
[ "$fails" = 0 ]
