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

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
FREECAD_SOURCE_DIR="${FREECAD_SOURCE_DIR:-${ROOT_DIR}/.cache/freecad/FreeCAD}"
QT6_WASM_DIR="${QT6_WASM_DIR:-${ROOT_DIR}/.cache/toolchains/qt6/install-wasm}"
OFFLINE_INCLUDE_DIR="${OFFLINE_INCLUDE_DIR:-${ROOT_DIR}/.cache/offline-sysroot/include}"
DIST_DIR="${ROOT_DIR}/native/freecad-naming-probe/dist"
OBJECT_DIR="${DIST_DIR}/objects"
SOURCE_ARCHIVE="${DIST_DIR}/libFreeCADPrivateNamingProbe.a"
SHIM_DIR="${ROOT_DIR}/native/freecad-naming-probe/shims"
FREECAD_COMMIT="0108fd4b4850cc46e625b60e53cea7a7bbe69f8d"
INDEXED_NAME_SHA256="73e0e60a9d6ee06851252e2071ebdd58698f8f99733903232529b3a194164435"
MAPPED_NAME_SHA256="90173aba5f331ac9453589833f7e5fc63c5c80a35f1600dbc71211606b1c9dbc"
STRING_HASHER_SHA256="fd32f35c9c2a0c21a60fa634ec37ec594d6f331924c5bba7fdbbcb351c3b612c"
HANDLE_SHA256="8f02d27a8b672e9a56764f85e394265b4fcf023aba097f040b03dcb38bc393d2"
MAPPED_ELEMENT_SHA256="f5448d9de693e319460d5e3ef52c63a54e0948963e4b92786aac38c77bdcd7a5"
ELEMENT_NAMING_UTILS_SHA256="fc3223857ca6d2990d2b48f437f75841cdf23e1e79fa18ef18296f40acd3cdc3"
ELEMENT_MAP_SHA256="e1ceaedb624688ddffa6a834c1b32539e8ba5bbdc0ec31401ea087e6a9c3eb52"

if [[ ! -d "${FREECAD_SOURCE_DIR}/.git" ]] || [[ "$(git -C "${FREECAD_SOURCE_DIR}" rev-parse HEAD)" != "${FREECAD_COMMIT}" ]]; then
  echo "FreeCAD private naming probe requires locked commit ${FREECAD_COMMIT}." >&2
  exit 1
fi
if ! git -C "${FREECAD_SOURCE_DIR}" diff --quiet HEAD -- \
  src/App/IndexedName.cpp src/App/IndexedName.h \
  src/App/MappedName.cpp src/App/MappedName.h \
  src/App/MappedElement.cpp src/App/MappedElement.h \
  src/App/ElementNamingUtils.cpp src/App/ElementNamingUtils.h \
  src/App/ElementMap.cpp src/App/ElementMap.h \
  src/App/StringHasher.cpp src/App/StringHasher.h \
  src/Base/Handle.cpp src/Base/Handle.h; then
  echo "FreeCAD private naming probe requires unmodified locked source inputs." >&2
  exit 1
fi
printf '%s  %s\n' \
  "${INDEXED_NAME_SHA256}" "${FREECAD_SOURCE_DIR}/src/App/IndexedName.cpp" \
  "${MAPPED_NAME_SHA256}" "${FREECAD_SOURCE_DIR}/src/App/MappedName.cpp" \
  "${STRING_HASHER_SHA256}" "${FREECAD_SOURCE_DIR}/src/App/StringHasher.cpp" \
  "${HANDLE_SHA256}" "${FREECAD_SOURCE_DIR}/src/Base/Handle.cpp" \
  "${MAPPED_ELEMENT_SHA256}" "${FREECAD_SOURCE_DIR}/src/App/MappedElement.cpp" \
  "${ELEMENT_NAMING_UTILS_SHA256}" "${FREECAD_SOURCE_DIR}/src/App/ElementNamingUtils.cpp" \
  "${ELEMENT_MAP_SHA256}" "${FREECAD_SOURCE_DIR}/src/App/ElementMap.cpp" | sha256sum -c -
if [[ "$(emcc --version | sed -n '1s/.* \([0-9][0-9.]*\) .*/\1/p')" != "3.1.69" ]]; then
  echo "FreeCAD private naming probe requires Emscripten 3.1.69." >&2
  exit 1
fi
for path in \
  "${QT6_WASM_DIR}/include/QtCore/QByteArray" \
  "${QT6_WASM_DIR}/lib/libQt6Core.a" \
  "${QT6_WASM_DIR}/lib/libQt6BundledPcre2.a" \
  "${QT6_WASM_DIR}/lib/libQt6BundledZLIB.a"; do
  if [[ ! -s "${path}" ]]; then
    echo "Missing Qt6 wasm Core prerequisite: ${path}" >&2
    exit 1
  fi
done
if [[ ! -s "${OFFLINE_INCLUDE_DIR}/boost/signals2/signal.hpp" ]]; then
  echo "Missing offline Boost headers: ${OFFLINE_INCLUDE_DIR}" >&2
  exit 1
fi

mkdir -p "${DIST_DIR}" "${OBJECT_DIR}"
COMPILE_FLAGS=(
  -I"${SHIM_DIR}" \
  -include "${SHIM_DIR}/ProbeAppHost.h" \
  -I"${FREECAD_SOURCE_DIR}/src" \
  -I"${QT6_WASM_DIR}/include" \
  -I"${QT6_WASM_DIR}/include/QtCore" \
  -I"${OFFLINE_INCLUDE_DIR}" \
  -std=c++17 -Oz -fexceptions
)
SOURCE_OBJECTS=()
for source in \
  "App/IndexedName.cpp" \
  "App/MappedName.cpp" \
  "App/StringHasher.cpp" \
  "App/MappedElement.cpp" \
  "App/ElementNamingUtils.cpp" \
  "App/ElementMap.cpp" \
  "Base/Handle.cpp"; do
  object="${OBJECT_DIR}/$(basename "${source}" .cpp).o"
  em++ "${COMPILE_FLAGS[@]}" -c "${FREECAD_SOURCE_DIR}/src/${source}" -o "${object}"
  SOURCE_OBJECTS+=("${object}")
done
ARCHIVE_TMP="${SOURCE_ARCHIVE}.$$.tmp"
emar rcs "${ARCHIVE_TMP}" "${SOURCE_OBJECTS[@]}"
mv "${ARCHIVE_TMP}" "${SOURCE_ARCHIVE}"

em++ \
  "${ROOT_DIR}/native/freecad-naming-probe/probe.cpp" \
  "${COMPILE_FLAGS[@]}" --bind \
  -Wl,--start-group \
  "${SOURCE_ARCHIVE}" \
  "${QT6_WASM_DIR}/lib/libQt6Core.a" \
  "${QT6_WASM_DIR}/lib/libQt6BundledPcre2.a" \
  "${QT6_WASM_DIR}/lib/libQt6BundledZLIB.a" \
  -Wl,--end-group \
  -sMODULARIZE=1 -sEXPORT_ES6=1 -sENVIRONMENT=web,worker,node \
  -sALLOW_MEMORY_GROWTH=1 -sNO_EXIT_RUNTIME=1 \
  -o "${DIST_DIR}/freecad-private-naming-source-probe.js"

sha256sum "${SOURCE_ARCHIVE}" "${DIST_DIR}/freecad-private-naming-source-probe.js" "${DIST_DIR}/freecad-private-naming-source-probe.wasm"
