#!/bin/bash
# Force a clean BT PAN reconnect around sleep/wake.
#
# Problem: after resume, Android reuses the stale BNEP session and
# re-applies its unicast block (captive-portal network policy).
# Fix: disconnect all BT devices before sleep so the phone sees a
# clean drop, then restart bt-nap on wake so the NAP re-registers
# and the phone auto-reconnects with a fresh session.

case "$1" in
    pre)
        # Disconnect all connected BT devices to force a clean BNEP teardown
        for dev in $(bluetoothctl devices Connected 2>/dev/null | awk '{print $2}'); do
            bluetoothctl disconnect "$dev" 2>/dev/null || true
        done
        ;;
    post)
        sleep 2
        systemctl restart bt-nap.service || true
        sleep 2
        systemctl restart dnsmasq.service || true
        ;;
esac
