#!/usr/bin/env bash
# Rebuild occt-import-js 0.0.23 (with Open CASCADE 7.6.1) as WebAssembly on Linux.
# Linux equivalent of the author's tools/setup_emscripten_win.bat + tools/build_wasm_win.bat (Release).
# Needs: git, cmake >= 3.6, make, python3, about 10 GB disk; takes 30-90 min.
set -euo pipefail
WORK="${1:-$PWD/occt-import-js-build}"
mkdir -p "$WORK" && cd "$WORK"

# 1. sources: occt-import-js tag 0.0.23 = c2148e54b456..., submodule occt = d2abb6d84423... (OCCT V7_6_1)
git clone https://github.com/kovacsv/occt-import-js.git
cd occt-import-js
git checkout c2148e54b456b571238d35cac037d304053d64b2
# the submodule URL is git.dev.opencascade.org; use the official GitHub mirror if that host is unreachable:
git config submodule.occt.url https://github.com/Open-Cascade-SAS/OCCT.git
git submodule update --init --depth 1 occt || git submodule update --init occt
test "$(git -C occt rev-parse HEAD)" = "d2abb6d844231cb8f29be6894440874a4700e4a5"

# 2. Emscripten 3.1.69 (as in tools/setup_emscripten_win.bat)
mkdir -p build/wasm && cd build/wasm
git clone https://github.com/emscripten-core/emsdk.git
./emsdk/emsdk install 3.1.69
./emsdk/emsdk activate 3.1.69
source ./emsdk/emsdk_env.sh
cd ../..

# 3. configure and build (as in tools/build_wasm_win.bat Release)
emcmake cmake -B build/wasm -G "Unix Makefiles" -DEMSCRIPTEN=1 -DCMAKE_BUILD_TYPE=Release .
emmake make -C build/wasm -j"$(nproc)"

# 4. result
ls -la build/wasm/Release/occt-import-js.js build/wasm/Release/occt-import-js.wasm
sha256sum build/wasm/Release/occt-import-js.js build/wasm/Release/occt-import-js.wasm
echo "Shipped release for comparison: js 3fb44ce1... (CRLF) / 8b090be3... (LF), wasm 33391fc9..."
