#!/usr/bin/env bash
# Builds the universal (arm64 + x86_64) nathan-popup binary into ../vendor.
# Requires Xcode / the Swift toolchain. Run on macOS.
set -euo pipefail
cd "$(dirname "$0")"

SRC="nathan-popup.swift"
OUT="../vendor/nathan-popup"
mkdir -p ../vendor

build() { # <arch>
  swiftc -O -target "$1-apple-macos11" "$SRC" -o "nathan-popup-$1"
}

echo "building arm64…"
build arm64
SLICES=(nathan-popup-arm64)

if build x86_64 2>/dev/null; then
  echo "building x86_64… ok"
  SLICES+=(nathan-popup-x86_64)
else
  echo "x86_64 slice failed — shipping arm64-only"
fi

lipo -create -output "$OUT" "${SLICES[@]}"
rm -f nathan-popup-arm64 nathan-popup-x86_64
chmod +x "$OUT"
codesign -s - -f "$OUT"   # ad-hoc sign so Gatekeeper is happy
echo "--- built $OUT ---"
lipo -info "$OUT"
codesign -dv "$OUT" 2>&1 | head -3
