#!/usr/bin/env bash
# =============================================================================
# Hermes Portable Rescue — Agent Entry Point
# =============================================================================
# This is the main entry point that usb-image.sh and autorun.sh expect at
# HERMES_DIR/agent/hermes. It finds and runs the Hermes CLI from the portable
# venv, or prompts to build if the venv doesn't exist yet.
# =============================================================================
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HERMES_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

# Try the venv binary first (post-build)
if [[ -x "$HERMES_ROOT/venv/bin/hermes" ]]; then
    exec "$HERMES_ROOT/venv/bin/hermes" "$@"
fi

# Try installed system hermes (pre-build / development)
if command -v hermes &>/dev/null; then
    exec hermes "$@"
fi

# Fallback: no hermes found
echo "╔══════════════════════════════════════════════════════╗" >&2
echo "║  Hermes Agent not found.                            ║" >&2
echo "║  Run the build script to create the portable venv:   ║" >&2
echo "║                                                      ║" >&2
echo "║    ./src/build-hermes-runtime.sh                     ║" >&2
echo "║                                                      ║" >&2
echo "║  Or install hermes-agent via pip:                    ║" >&2
echo "║    pip install hermes-agent                          ║" >&2
echo "╚══════════════════════════════════════════════════════╝" >&2
exit 1
