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

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
OCCT_SOURCE_DIR="${OCCT_SOURCE_DIR:-/home/mes123456/working/WebCam/occt}"
OCCT_BUILD_DIR="${OCCT_BUILD_DIR:-${ROOT_DIR}/.cache/bitbybit/occt-history-build}"
DIST_DIR="${OCCT_HISTORY_DIST_DIR:-${ROOT_DIR}/native/occt-history/dist}"
PUBLIC_DIR="${OCCT_HISTORY_PUBLIC_DIR:-${ROOT_DIR}/public/native/occt-history}"
JOBS="${JOBS:-$(nproc)}"

if [[ ! -f "${OCCT_SOURCE_DIR}/CMakeLists.txt" ]]; then
  echo "OCCT source checkout not found: ${OCCT_SOURCE_DIR}" >&2
  exit 1
fi
if ! command -v emcmake >/dev/null || ! command -v em++ >/dev/null; then
  echo "Emscripten emcmake/em++ are required." >&2
  exit 1
fi

emcmake cmake -S "${OCCT_SOURCE_DIR}" -B "${OCCT_BUILD_DIR}" -G Ninja \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_MAKE_PROGRAM="$(command -v ninja)" \
  -DCMAKE_CXX_FLAGS="-fexceptions" \
  -DCMAKE_CXX_FLAGS_RELEASE="-O0 -DNDEBUG -fexceptions" \
  -DBUILD_RELEASE_DISABLE_EXCEPTIONS=OFF \
  -DBUILD_LIBRARY_TYPE=Static \
  -DBUILD_MODULE_FoundationClasses=ON \
  -DBUILD_MODULE_ModelingData=ON \
  -DBUILD_MODULE_ModelingAlgorithms=ON \
  -DBUILD_MODULE_Visualization=OFF \
  -DBUILD_MODULE_ApplicationFramework=OFF \
  -DBUILD_MODULE_DataExchange=ON \
  -DBUILD_MODULE_Draw=OFF \
  -DBUILD_ADDITIONAL_TOOLKITS="TKBO;TKFillet;TKOffset;TKDESTEP" \
  -DBUILD_DOC_Overview=OFF \
  -DBUILD_Inspector=OFF \
  -DBUILD_SAMPLES_QT=OFF \
  -DBUILD_USE_PCH=OFF \
  -DUSE_FREETYPE=OFF \
  -DUSE_TK=OFF \
  -DUSE_TBB=OFF \
  -DUSE_RAPIDJSON=OFF

cmake --build "${OCCT_BUILD_DIR}" --target \
  TKernel TKMath TKG2d TKG3d TKGeomBase TKGeomAlgo TKShHealing TKPrim TKTopAlgo TKBRep TKBool TKBO TKFillet TKOffset \
  TKDE TKXSBase TKDESTEP \
  --parallel "${JOBS}"
mkdir -p "${DIST_DIR}"

em++ "${ROOT_DIR}/native/occt-history/occt_history.cpp" \
  -I"${OCCT_BUILD_DIR}/include/opencascade" \
  -I"${OCCT_SOURCE_DIR}/src" \
  -std=c++17 -O2 -fexceptions --bind \
  -Wl,--start-group \
  "${OCCT_BUILD_DIR}/lin32/clang/lib/libTKBO.a" \
  "${OCCT_BUILD_DIR}/lin32/clang/lib/libTKFillet.a" \
  "${OCCT_BUILD_DIR}/lin32/clang/lib/libTKOffset.a" \
  "${OCCT_BUILD_DIR}/lin32/clang/lib/libTKBool.a" \
  "${OCCT_BUILD_DIR}/lin32/clang/lib/libTKDESTEP.a" \
  "${OCCT_BUILD_DIR}/lin32/clang/lib/libTKXSBase.a" \
  "${OCCT_BUILD_DIR}/lin32/clang/lib/libTKDE.a" \
  "${OCCT_BUILD_DIR}/lin32/clang/lib/libTKBRep.a" \
  "${OCCT_BUILD_DIR}/lin32/clang/lib/libTKTopAlgo.a" \
  "${OCCT_BUILD_DIR}/lin32/clang/lib/libTKPrim.a" \
  "${OCCT_BUILD_DIR}/lin32/clang/lib/libTKShHealing.a" \
  "${OCCT_BUILD_DIR}/lin32/clang/lib/libTKGeomAlgo.a" \
  "${OCCT_BUILD_DIR}/lin32/clang/lib/libTKGeomBase.a" \
  "${OCCT_BUILD_DIR}/lin32/clang/lib/libTKG3d.a" \
  "${OCCT_BUILD_DIR}/lin32/clang/lib/libTKG2d.a" \
  "${OCCT_BUILD_DIR}/lin32/clang/lib/libTKMath.a" \
  "${OCCT_BUILD_DIR}/lin32/clang/lib/libTKernel.a" \
  -Wl,--end-group \
  -sMODULARIZE=1 -sEXPORT_ES6=1 -sENVIRONMENT=web,worker,node \
  -sALLOW_MEMORY_GROWTH=1 -sINITIAL_MEMORY=134217728 -sMAXIMUM_MEMORY=2147483648 \
  -sNO_EXIT_RUNTIME=1 \
  -o "${DIST_DIR}/bitbybit-occt-history.js"

cp "${ROOT_DIR}/native/occt-history/package.json" "${DIST_DIR}/package.json"
mkdir -p "${PUBLIC_DIR}"
cp "${DIST_DIR}/bitbybit-occt-history.js" "${PUBLIC_DIR}/bitbybit-occt-history.js"
cp "${DIST_DIR}/bitbybit-occt-history.wasm" "${PUBLIC_DIR}/bitbybit-occt-history.wasm"
sha256sum "${DIST_DIR}/bitbybit-occt-history.js" "${DIST_DIR}/bitbybit-occt-history.wasm"
