#!/usr/bin/env bash
# FR-53: optional local HTTP API entry point.
set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

if [ -n "${MOONVSR_API_BIN:-}" ] && [ -x "${MOONVSR_API_BIN}" ]; then
  exec "${MOONVSR_API_BIN}" "$@"
fi

RELEASE="$ROOT/_build/native/release/build/cmd/api/api.exe"
DEBUG="$ROOT/_build/native/debug/build/cmd/api/api.exe"
if [ -x "$RELEASE" ] && [ -x "$DEBUG" ]; then
  if [ "$DEBUG" -nt "$RELEASE" ]; then
    exec "$DEBUG" "$@"
  fi
  exec "$RELEASE" "$@"
fi
if [ -x "$RELEASE" ]; then
  exec "$RELEASE" "$@"
fi
if [ -x "$DEBUG" ]; then
  exec "$DEBUG" "$@"
fi

echo "moonbit-vsr-api: native binary not found; run: moon build --target native" >&2
exit 127