#!/bin/bash
# hx-artex-scan: run an ARTEX scan on HOGSMEADE's artexlab guest WITH the capable LLM rendezvous ensured up
# first. This is the package-managed entry point the agent/UI-less path should use instead of calling
# `ssh hogsmeade-agent sudo -n hx-artex scan` directly, so a run never silently drops onto the weak 9B.
#
#   hx-artex-scan "<goal>" <host...>
#
# It brings the capable backend up (hx-artex-llm-link up: beauxbatons while its gate is open, else the
# opt-in AWS GPU, else nothing), then starts the scan on the guest. If no capable backend came up it still
# runs, but pins the task to the 9B (profile 1) as a last-ditch and says so loudly.
set -u
CONF=${HXLL_CONF:-/etc/ctg-hx-artex-llm/link.env}; [ -r "$CONF" ] && . "$CONF"
: "${HOG_HOST:=hogsmeade-agent}"
SSH="ssh -o BatchMode=yes -o ConnectTimeout=10"

[ $# -ge 1 ] || { echo 'usage: hx-artex-scan "<goal>" <host...>' >&2; exit 2; }

echo "[hx-artex-scan] ensuring the capable LLM endpoint is up..."
if hx-artex-llm-link up; then
  echo "[hx-artex-scan] capable endpoint ready -> running on the default profile (122b)."
  exec $SSH "$HOG_HOST" "sudo -n hx-artex scan $(printf '%q ' "$@")"
else
  echo "[hx-artex-scan] WARNING: no capable backend (Mac gate closed and AWS fallback off)."
  echo "[hx-artex-scan] running on the 9B last-ditch fallback (profile 1). Results will be shallow."
  exec $SSH "$HOG_HOST" "sudo -n hx-artex scan --profile 1 $(printf '%q ' "$@")"
fi
