#!/usr/bin/env bash
# FR-50: stable CLI entry point. `moonbit-vsr --help` works from the repo; the
# wrapper forwards to the native build, preferring the newer of release/debug
# so a stale release artifact cannot shadow an updated debug build.
set -euo pipefail

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

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

RELEASE="$ROOT/_build/native/release/build/cmd/main/main.exe"
DEBUG="$ROOT/_build/native/debug/build/cmd/main/main.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: native binary not found; run: moon build --target native" >&2
exit 127