# Nomad: per-connection Wi-Fi MAC randomization (mac_mode=per-connect).
#
# Fires when a wireless interface comes up. Self-gates on the UCI flag so it
# is inert for off / per-boot. Debounced via a tmpfs timestamp so the `wifi
# reload` that *applies* a new MAC (which re-adds the iface) cannot start an
# infinite randomize loop — a genuine AP-up more than ~25 s later randomizes
# again. Sourced by hotplug, so no shebang.

[ "$ACTION" = "add" ] || return 0

case "$INTERFACE" in
    wlan*|wl*|phy*-ap*|ap[0-9]*) ;;
    *) return 0 ;;
esac

[ "$(uci -q get nomad.wifi.mac_mode)" = "per-connect" ] || return 0

LOCK="/tmp/.nomad-wifi-mac.ts"
now="$(date +%s 2>/dev/null)" || return 0
if [ -f "$LOCK" ]; then
    last="$(cat "$LOCK" 2>/dev/null || echo 0)"
    [ $((now - last)) -lt 25 ] && return 0
fi
echo "$now" > "$LOCK"

( PYTHONPATH=/etc/nomad python3 -m nomad.cli wifi randomize >/dev/null 2>&1 ) &
