#!/usr/bin/env bash
# Build a per-platform omnirush-cli package from this repo (local use).
# Usage: scripts/package.sh [platform]   (linux-x64|darwin-aarch64|darwin-x64|windows-x64)
set -euo pipefail
PLATFORM="${1:-linux-x64}"
STAGE="omnirush-cli-$PLATFORM"
rm -rf "$STAGE" && mkdir -p "$STAGE"
cp -r src assets package.json "$STAGE/"
mkdir -p "$STAGE/.runtime/bin"
if [ "$PLATFORM" = "linux-x64" ]; then
  cp .runtime/bin/bun "$STAGE/.runtime/bin/bun"
elif [ "$PLATFORM" = "windows-x64" ]; then
  cp ".runtime/bun-windows-x64/bin/bun.exe" "$STAGE/.runtime/bin/bun.exe"
else
  cp ".runtime/bun-$PLATFORM/bin/bun" "$STAGE/.runtime/bin/bun"
fi
# Search tools (fd, rg): bundled so the agent never attempts its own
# GitHub download at startup.
TOOL_EXT=""
[ "$PLATFORM" = "windows-x64" ] && TOOL_EXT=".exe"
for tool in fd rg; do
  if [ -f ".runtime/bin/$tool$TOOL_EXT" ]; then
    cp ".runtime/bin/$tool$TOOL_EXT" "$STAGE/.runtime/bin/$tool$TOOL_EXT"
  else
    echo "warning: .runtime/bin/$tool$TOOL_EXT missing — run scripts/postinstall.js first" >&2
  fi
done
if [[ "$PLATFORM" == windows* ]]; then
  zip -q -r "$STAGE.zip" "$STAGE"
else
  tar -czf "$STAGE.tar.gz" "$STAGE"
fi
echo "built $STAGE archive"
