#!/usr/bin/env bash
# scripts/publish.sh
# Build Mac + Linux N-API binaries and publish them to npm in one go.
set -euo pipefail

###############################################################################
#                                                                          #
#  Configuration (adjust if you need other targets / flags)                #
#                                                                          #
###############################################################################

export MACOSX_DEPLOYMENT_TARGET=${MACOSX_DEPLOYMENT_TARGET:-10.13}
export CARGO_INCREMENTAL=0

# Platform triplets to build – feel free to add/remove.
TARGETS=(
  "aarch64-apple-darwin"
  "x86_64-apple-darwin"
  "x86_64-unknown-linux-gnu"
  "x86_64-unknown-linux-musl"
)

# Extra Rust features to pass when you need them per-target
FEATURES_FP16="fp16kernels"   # example – delete if unused

###############################################################################
#                                                                          #
#  Sanity checks                                                            #
#                                                                          #
###############################################################################

command -v rustup >/dev/null      || { echo "rustup not found"; exit 1; }
command -v cargo >/dev/null       || { echo "cargo not found"; exit 1; }
command -v npm >/dev/null         || { echo "npm not found"; exit 1; }
command -v zig >/dev/null         || { echo "zig not found - brew install zig"; exit 1; }

if [[ -z "${NODE_AUTH_TOKEN:-}" && "${DRY_RUN:-false}" != "true" ]]; then
  echo "ERROR: NODE_AUTH_TOKEN must be set for a real publish" >&2
  exit 1
fi

###############################################################################
#                                                                          #
#  Prep                                                                     #
#                                                                          #
###############################################################################

ROOT_DIR="$(cd "$(dirname "$0")/../.."; pwd)"
echo "==> Root directory: $ROOT_DIR"
cd "$ROOT_DIR/nodejs"

echo "==> Installing JS deps"
npm ci

echo "==> Cleaning old artifacts"
rm -rf dist .cargo-cache || true
mkdir -p dist

###############################################################################
#                                                                          #
#  Build loop                                                               #
#                                                                          #
###############################################################################

for TARGET in "${TARGETS[@]}"; do
  echo ""
  echo "=== Building for $TARGET ==="
  rustup target add "$TARGET" >/dev/null 2>&1 || true

  case "$TARGET" in
    *apple-darwin)
      npx napi build \
        --release \
        --no-const-enum \
        --target "$TARGET" \
        --strip \
        dist/
      ;;

    *unknown-linux-gnu)
      CC="zig cc -target x86_64-linux-gnu" \
      npx napi build \
        --release \
        --no-const-enum \
        --features "$FEATURES_FP16" \
        --target "$TARGET" \
        --strip \
        dist/
      ;;

    *unknown-linux-musl)
      CC="zig cc -target x86_64-linux-musl" \
      npx napi build \
        --release \
        --no-const-enum \
        --features "$FEATURES_FP16" \
        --target "$TARGET" \
        --strip \
        dist/
      ;;

    *)
      echo "Target $TARGET not handled"
      exit 1
      ;;
  esac
done

###############################################################################
#                                                                          #
#  Generic JS / TypeScript files                                            #
#                                                                          #
###############################################################################

echo ""
echo "==> Building"
npm run build

###############################################################################
#                                                                          #
#  Package into per-platform tarballs                                       #
#                                                                          #
###############################################################################

echo ""
echo "==> Packing artifacts"
rm -rf npm
npx napi artifacts -d dist

###############################################################################
#                                                                          #
#  Publish                                                                  #
#                                                                          #
###############################################################################

PACKAGE_VERSION=$(jq -r .version package.json)
NPM_ARGS="--access public"

if [[ "${DRY_RUN:-false}" == "true" ]]; then
  NPM_ARGS+=" --dry-run"
fi
if [[ "$PACKAGE_VERSION" == *"beta"* ]]; then
  NPM_ARGS+=" --tag preview"
fi

echo ""
echo "==> npm publish $NPM_ARGS"
npm publish $NPM_ARGS

echo ""
echo "🎉  Done - version $PACKAGE_VERSION has been processed."
