#!/usr/bin/env bash
# is_lab_host must read the registry JSON. A second column-0 REGISTRY= assignment
# anywhere in buildbox re-binds it and the predicate silently answers "no" for
# every host, so the extraction below deliberately takes every such assignment.
set -uo pipefail

REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)"
MOD="$REPO/modules/buildbox"
export BUILDBOX_REGISTRY="$REPO/modules/workstation/claude/buildbox-hosts.json"
[ -r "$BUILDBOX_REGISTRY" ] || { echo "FAIL: no registry at $BUILDBOX_REGISTRY" >&2; exit 2; }

eval "$(sed -n '/^REGISTRY=/p;/^is_lab_host() {/,/^}$/p' "$MOD/bin/buildbox")"

is_lab_host debian1; d1=$?
is_lab_host debian2; d2=$?

if [ "$d1" = 0 ] && [ "$d2" = 1 ]; then
  printf 'is-lab-host: ok (debian1 rc=%s lab, debian2 rc=%s not lab)\n' "$d1" "$d2"
  exit 0
fi
printf 'FAIL is-lab-host: debian1 rc=%s (want 0), debian2 rc=%s (want 1)\n' "$d1" "$d2" >&2
exit 1
