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

ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
EMSDK=${EMSDK:-/opt/emsdk}

if [[ ! -x "$EMSDK/upstream/emscripten/emcc" ]]; then
  echo "Emscripten not found at $EMSDK" >&2
  exit 1
fi

export EMSDK_QUIET=1
# shellcheck source=/dev/null
source "$EMSDK/emsdk_env.sh"

cmake -E rm -rf "$ROOT/build/d3wasm-reference"
emcmake cmake \
  -S "$ROOT/vendor/d3wasm/neo" \
  -B "$ROOT/build/d3wasm-reference" \
  -G Ninja \
  -DCMAKE_BUILD_TYPE=MinSizeRel
cmake --build "$ROOT/build/d3wasm-reference" --parallel "${JOBS:-4}"

