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

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
SDK_ROOT="${ROOT_DIR}/.cache/toolchains/freecad-naming-sdk"
SOURCE_ROOT="${ROOT_DIR}/.cache/toolchains/freecad-naming-sdk/source-occt8"
BUILD_ROOT="${ROOT_DIR}/.cache/toolchains/freecad-naming-sdk/build-wasm"
OCCT_BUILD="${ROOT_DIR}/.cache/bitbybit/occt-history-build-offline"
EM_CACHE_DIR="${ROOT_DIR}/.cache/toolchains/emscripten-freecad-cache"
DIST_DIR="${ROOT_DIR}/native/freecad-naming-bridge/dist"
BRIDGE_SOURCE="${ROOT_DIR}/native/freecad-naming-bridge/freecad_naming_bridge.cpp"
QT="${ROOT_DIR}/.cache/toolchains/qt6/install-wasm-freecad"
PYTHON="${ROOT_DIR}/.cache/toolchains/python/install-wasm"
ICU="${ROOT_DIR}/.cache/toolchains/icu/install-wasm"
XERCES="${ROOT_DIR}/.cache/toolchains/xerces-c/install-wasm"
BOOST="${ROOT_DIR}/.cache/toolchains/boost/install-wasm"
YAML="${ROOT_DIR}/.cache/toolchains/yaml-cpp/install-wasm"

cd "${ROOT_DIR}"
node scripts/check-freecad-naming-sdk-readiness.mjs >/dev/null
for path in \
  "${SDK_ROOT}/lib/libFreeCADBase.a" \
  "${SDK_ROOT}/lib/libFreeCADApp.a" \
  "${SDK_ROOT}/lib/libPart.a" \
  "${PYTHON}/lib/python-deps/libmpdec.a" \
  "${PYTHON}/lib/python-deps/libexpat.a" \
  "${PYTHON}/lib/python-deps/libHacl_Hash_SHA2.a" \
  "${PYTHON}/lib/python-deps/libz.a" \
  "${PYTHON}/lib/python-deps/libbz2.a" \
  "${PYTHON}/lib/python-deps/libsqlite3-mt.a" \
  "${PYTHON}/share/python-wasm-stdlib/lib/python313.zip" \
  "${BRIDGE_SOURCE}"; do
  [[ -s "${path}" ]] || { echo "Missing isolated bridge input: ${path}" >&2; exit 1; }
done
mkdir -p "${DIST_DIR}"

mapfile -t OCCT_LIBRARIES < <(find "${OCCT_BUILD}/lin32/clang/lib" -maxdepth 1 -type f -name '*.a' | sort)
EM_FROZEN_CACHE=0 EM_CACHE="${EM_CACHE_DIR}" em++ "${BRIDGE_SOURCE}" \
  -I"${SOURCE_ROOT}/src" \
  -I"${BUILD_ROOT}/src" \
  -I"${SOURCE_ROOT}/src/3rdParty/PyCXX" \
  -I"${OCCT_BUILD}/include/opencascade" \
  -I"${ROOT_DIR}/.cache/occt/occt/src/Deprecated/NCollectionAliases" \
  -I"${QT}/include" -I"${QT}/include/QtCore" \
  -I"${PYTHON}/include/python3.13" \
  -I"${XERCES}/include" -I"${ICU}/include" -I"${YAML}/include" \
  -I"${ROOT_DIR}/.cache/offline-sysroot/include" \
  -include "${ROOT_DIR}/config/freecad-wasm-sdk-compat.h" \
  -D__linux__=1 -DQT_NO_KEYWORDS -DHAVE_CONFIG_H -DPYCXX_6_2_COMPATIBILITY \
  -std=c++20 -O1 -fexceptions -pthread --bind \
  -Wl,--start-group \
  "${SDK_ROOT}/lib/libPart.a" \
  "${SDK_ROOT}/lib/libFreeCADApp.a" \
  "${SDK_ROOT}/lib/libFreeCADBase.a" \
  "${OCCT_LIBRARIES[@]}" \
  "${PYTHON}/lib/libpython3.13.a" \
  "${PYTHON}/lib/python-deps/libmpdec.a" \
  "${PYTHON}/lib/python-deps/libexpat.a" \
  "${PYTHON}/lib/python-deps/libHacl_Hash_SHA2.a" \
  "${PYTHON}/lib/python-deps/libz.a" \
  "${PYTHON}/lib/python-deps/libbz2.a" \
  "${PYTHON}/lib/python-deps/libsqlite3-mt.a" \
  "${XERCES}/lib/libxerces-c.a" \
  "${YAML}/lib/libyaml-cpp.a" \
  "${BOOST}/lib/libboost_program_options.a" \
  "${BOOST}/lib/libboost_regex.a" \
  "${BOOST}/lib/libboost_thread.a" \
  "${BOOST}/lib/libboost_date_time.a" \
  "${BOOST}/lib/libboost_atomic.a" \
  "${ICU}/lib/libicui18n.a" "${ICU}/lib/libicuuc.a" "${ICU}/lib/libicudata.a" \
  "${QT}/lib/libQt6Concurrent.a" "${QT}/lib/libQt6Network.a" "${QT}/lib/libQt6Xml.a" \
  "${QT}/lib/libQt6Core.a" "${QT}/lib/libQt6BundledPcre2.a" "${QT}/lib/libQt6BundledZLIB.a" \
  -Wl,--end-group \
  -sMODULARIZE=1 -sEXPORT_ES6=1 -sENVIRONMENT=node \
  -sALLOW_MEMORY_GROWTH=1 -sINITIAL_MEMORY=268435456 -sMAXIMUM_MEMORY=2147483648 \
  -sNO_EXIT_RUNTIME=1 -sPTHREAD_POOL_SIZE=2 \
  --preload-file "${PYTHON}/share/python-wasm-stdlib@/usr/local" \
  --pre-js "${ROOT_DIR}/native/freecad-naming-bridge/pre.js" \
  -o "${DIST_DIR}/freecad-private-naming-bridge.js"

FREECAD_NAMING_BRIDGE_REPORT="${SDK_ROOT}/isolated-bridge-report.json" \
  npx tsx native/freecad-naming-bridge/smoke-test.ts
sha256sum \
  "${DIST_DIR}/freecad-private-naming-bridge.js" \
  "${DIST_DIR}/freecad-private-naming-bridge.wasm" \
  "${DIST_DIR}/freecad-private-naming-bridge.data"
[[ ! -e "${ROOT_DIR}/public/native/freecad-naming-bridge" ]] || { echo "Isolated bridge must not be published under public/." >&2; exit 1; }
echo "Isolated bridge evidence: ${SDK_ROOT}/isolated-bridge-report.json"
