#!/bin/sh
# Builds argoni-embed with the vector index (USearch, vendor/usearch/) compiled in, and puts
# the meaning model beside it, after checking the model against its SHA256SUMS.
#   engine/embed/build            into engine/embed/out/ (development; gitignored)
#   engine/embed/build DIR        into DIR (the app's product/embed/, scripts/pack-product.sh)
# Fails, and builds nothing, when the model is missing or differs. Apple's tools only
# (xcrun clang++, swiftc); nothing is downloaded.
set -eu
here=$(cd "$(dirname "$0")" && pwd)
out="${1:-$here/out}"
"$here/check-model"
(cd "$here" && shasum -a 256 -c router.sha256) >/dev/null || {
    echo "build: router head is missing or differs from router.sha256" >&2; exit 1;
}
mkdir -p "$out"
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
U="$here/vendor/usearch"
xcrun clang++ -std=c++17 -O3 -target arm64-apple-macos15.0 -I "$U/include" \
    -DUSEARCH_USE_NUMKONG=0 -DUSEARCH_USE_FP16LIB=0 -DUSEARCH_USE_OPENMP=0 \
    -c "$U/c/lib.cpp" -o "$work/usearch.o"
xcrun swiftc -O -swift-version 5 -target arm64-apple-macos15.0 -import-objc-header "$here/bridge.h" \
    "$here/argoni-embed.swift" "$work/usearch.o" -o "$out/argoni-embed" \
    -framework CoreML -framework Accelerate -lsqlite3 -lc++
rsync -a --delete --exclude .DS_Store "$here/model/e5-small.mlmodelc/" "$out/e5-small.mlmodelc/"
cp "$here/model/vocab.tsv" "$out/vocab.tsv"
cp "$here/router.json" "$out/router.json"
echo "built $out/argoni-embed, the model beside it"
