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

repo_root=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)
source_root="$repo_root/CAMotics/src"
output_dir="$repo_root/public/vendor/camotics"

test -f "$source_root/camotics/sim/ConicSweep.cpp"
test -f "$source_root/camotics/sim/SpheroidSweep.cpp"
command -v em++ >/dev/null
mkdir -p "$output_dir"

em++ \
  "$repo_root/native/camotics-wasm/camotics_sweep.cpp" \
  "$source_root/camotics/sim/Sweep.cpp" \
  "$source_root/camotics/sim/ConicSweep.cpp" \
  "$source_root/camotics/sim/SpheroidSweep.cpp" \
  -I"$repo_root/native/camotics-wasm/include" \
  -I"$source_root" \
  -std=c++17 -O3 -flto -fno-exceptions \
  -sSTANDALONE_WASM=1 \
  -sERROR_ON_UNDEFINED_SYMBOLS=1 \
  -sEXPORTED_FUNCTIONS='["_camotics_sweep_abi_version","_camotics_conic_depth","_camotics_spheroid_depth","_camotics_conic_bbox_count"]' \
  -Wl,--no-entry \
  -o "$output_dir/camotics-sweep.wasm"
