#!/usr/bin/env bash
set -euo pipefail

usage() {
  cat << USAGE
Usage: tools/run_llvm_harness.sh <input.hako> [-- <args...>]

Builds LLVM-harness prerequisites and runs the program via:
  NYASH_LLVM_USE_HARNESS=1 ./target/release/hakorune --backend llvm <input.hako>
USAGE
}

if [[ $# -lt 1 ]]; then
  usage
  exit 1
fi

INPUT="$1"
shift || true

if [[ "$INPUT" == "-h" || "$INPUT" == "--help" ]]; then
  usage
  exit 0
fi

if [[ ! -f "$INPUT" ]]; then
  echo "error: input file not found: $INPUT" >&2
  exit 1
fi

CARGO_TARGET_DIR_EFFECTIVE="${CARGO_TARGET_DIR:-$PWD/target}"
BIN_DEFAULT="$CARGO_TARGET_DIR_EFFECTIVE/release/hakorune"
BIN="${NYASH_BIN:-$BIN_DEFAULT}"

echo "[1/4] Building hakorune (llvm feature)..."
cargo build --release -p nyash-rust --features llvm --bin hakorune -j 24

echo "[2/4] Building ny-llvmc..."
cargo build --release -p nyash-llvm-compiler -j 24

echo "[3/4] Building nyash_kernel..."
cargo build --release -p nyash_kernel -j 24

if [[ ! -x "$BIN" ]]; then
  if [[ -x "$CARGO_TARGET_DIR_EFFECTIVE/release/nyash" ]]; then
    BIN="$CARGO_TARGET_DIR_EFFECTIVE/release/nyash"
  else
    echo "error: compiler binary not found/executable after build: $BIN" >&2
    echo "hint: ensure NYASH_BIN points to an existing binary or set CARGO_TARGET_DIR correctly" >&2
    exit 1
  fi
fi

echo "[4/4] Running LLVM harness..."
NYASH_LLVM_USE_HARNESS=1 "$BIN" --backend llvm "$INPUT" "$@"
