#!/bin/bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
BUILD_DIR="$SCRIPT_DIR/build"
EMSDK_DIR="$(cd "$SCRIPT_DIR/../../emsdk" && pwd)"

# Setup paths
export PATH="/c/Program Files/CMake/bin:/c/Build/ninja:$EMSDK_DIR:$EMSDK_DIR/upstream/emscripten:$EMSDK_DIR/node/22.16.0_64bit/bin:$PATH"

mkdir -p "$BUILD_DIR"
cd "$BUILD_DIR"

python "$EMSDK_DIR/upstream/emscripten/emcmake.py" cmake "$SCRIPT_DIR" \
  -DCMAKE_BUILD_TYPE=Release \
  -G Ninja

python "$EMSDK_DIR/upstream/emscripten/emmake.py" ninja

# Copy output to public/ for serving
cp meshlab-core.js meshlab-core.wasm "$SCRIPT_DIR/../public/"

echo "Wasm build complete"
ls -lh meshlab-core.wasm
