#!/bin/sh
# cycle unplug/replug; only count cycles where rebind succeeded (armed)
armed=0; i=0
while [ $i -lt ${1:-60} ]; do
  i=$((i+1))
  python3 /tmp/opencode/monrun.py "device_del k1" 0.8 >/dev/null 2>&1
  sleep 0.6
  python3 /tmp/opencode/monrun.py "device_add usb-kbd,id=k1" 0.8 >/dev/null 2>&1
  sleep 2.2
  r=$(ssh -F dfbsd-qemu/config -o ConnectTimeout=6 -o BatchMode=yes dfbsd-maxx "sh /tmp/rebind.sh" 2>/dev/null)
  case "$r" in rebound*) armed=$((armed+1));; esac
  if ! dfbsd-qemu/vm.sh status >/dev/null 2>&1; then echo "GUEST DOWN at cycle $i (armed $armed)"; exit 1; fi
done
echo "done: $i cycles, $armed armed"
