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

repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
dest_dir="$repo_root/tools/sentrux/bin"
dest_linux="$dest_dir/sentrux"
dest_windows="$dest_dir/sentrux.exe"

vector_sentrux_binary="/home/dev/src/VectorShell/sentrux/target/release/sentrux"
vector_sentrux_manifest="/home/dev/src/VectorShell/sentrux/Cargo.toml"
scriptorium_sentrux_windows="/home/dev/src/ScriptoriumAI/sentrux.exe"

usage() {
  cat <<'EOF'
Usage:
  scripts/bootstrap-hypertwist-sentrux.sh [--if-missing]

Materializes a repo-local sentrux binary under tools/sentrux/bin/ while keeping
ordinary HyperTwist runs repo-owned by default.

Resolution order:
  1. HYPERTWIST_SENTRUX_BINARY if explicitly provided
  2. optional sibling-repo seeds, but only when
     HYPERTWIST_ALLOW_SIBLING_SENTRUX_BOOTSTRAP=1
     - existing VectorShell Linux build artifact
     - local VectorShell source build via cargo
     - local ScriptoriumAI Windows binary

The resulting analyzer then lives under HyperTwist-owned tools/sentrux/bin/
so later runs do not need to execute directly from sibling repos.
EOF
}

copy_binary() {
  local source_path="$1"
  local destination_path="$2"

  mkdir -p "$dest_dir"
  cp "$source_path" "$destination_path"
  chmod +x "$destination_path" 2>/dev/null || true
  printf 'Materialized sentrux at %s from %s\n' "$destination_path" "$source_path"
}

if [[ "${1:-}" == "--help" ]]; then
  usage
  exit 0
fi

if [[ "${1:-}" == "--if-missing" ]]; then
  if [[ -x "$dest_linux" || -x "$dest_windows" ]]; then
    printf 'Repo-local sentrux already present under %s\n' "$dest_dir"
    exit 0
  fi
elif [[ $# -gt 0 ]]; then
  usage >&2
  exit 1
fi

if [[ -n "${HYPERTWIST_SENTRUX_BINARY:-}" && -x "${HYPERTWIST_SENTRUX_BINARY}" ]]; then
  case "${HYPERTWIST_SENTRUX_BINARY}" in
    *.exe)
      copy_binary "${HYPERTWIST_SENTRUX_BINARY}" "$dest_windows"
      ;;
    *)
      copy_binary "${HYPERTWIST_SENTRUX_BINARY}" "$dest_linux"
      ;;
  esac
  exit 0
fi

allow_sibling_bootstrap="${HYPERTWIST_ALLOW_SIBLING_SENTRUX_BOOTSTRAP:-0}"

if [[ "$allow_sibling_bootstrap" == "1" ]]; then
  if [[ -x "$vector_sentrux_binary" ]]; then
    copy_binary "$vector_sentrux_binary" "$dest_linux"
    exit 0
  fi

  if command -v cargo >/dev/null 2>&1 && [[ -f "$vector_sentrux_manifest" ]]; then
    cargo build --quiet --release --manifest-path "$vector_sentrux_manifest" --bin sentrux
    if [[ -x "$vector_sentrux_binary" ]]; then
      copy_binary "$vector_sentrux_binary" "$dest_linux"
      exit 0
    fi
  fi

  if [[ -f "$scriptorium_sentrux_windows" ]]; then
    copy_binary "$scriptorium_sentrux_windows" "$dest_windows"
    exit 0
  fi
fi

cat >&2 <<EOF
Unable to materialize a repo-local sentrux binary.

Checked:
  - HYPERTWIST_SENTRUX_BINARY
  - sibling seeds are $(if [[ "$allow_sibling_bootstrap" == "1" ]]; then printf 'enabled'; else printf 'disabled by default'; fi)
  - $vector_sentrux_binary
  - $vector_sentrux_manifest
  - $scriptorium_sentrux_windows

Provide HYPERTWIST_SENTRUX_BINARY directly or place a compatible sentrux binary
under tools/sentrux/bin/. If you intentionally want to seed from sibling repos
on this machine, rerun with HYPERTWIST_ALLOW_SIBLING_SENTRUX_BOOTSTRAP=1.
EOF
exit 1
