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

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
QT_VERSION="6.8.2"
QT_ARCHIVE="qtbase-everywhere-src-${QT_VERSION}.tar.xz"
QT_ARCHIVE_SHA256="012043ce6d411e6e8a91fdc4e05e6bedcfa10fcb1347d3c33908f7fdd10dfe05"
QT_CACHE_DIR="${QT6_WASM_CACHE_DIR:-${ROOT_DIR}/.cache/toolchains/qt6}"
QT_DOWNLOAD_DIR="${QT_CACHE_DIR}/downloads"
QT_SOURCE_DIR="${QT_CACHE_DIR}/src"
QT_BUILD_DIR="${QT_CACHE_DIR}/build-wasm"
QT_INSTALL_DIR="${QT_CACHE_DIR}/install-wasm"
EMSDK_SHIM_DIR="${QT_CACHE_DIR}/emsdk-shim"
JOBS="${JOBS:-$(nproc)}"

if ! command -v emcc >/dev/null || ! command -v em++ >/dev/null; then
  echo "Emscripten emcc/em++ are required." >&2
  exit 1
fi
if [[ "$(emcc --version | sed -n '1s/.* \([0-9][0-9.]*\) .*/\1/p')" != "3.1.69" ]]; then
  echo "Qt wasm Core must use the project-locked Emscripten 3.1.69 toolchain." >&2
  exit 1
fi

mkdir -p "${QT_DOWNLOAD_DIR}" "${QT_CACHE_DIR}" "${EMSDK_SHIM_DIR}/upstream"
if [[ ! -f "${QT_DOWNLOAD_DIR}/${QT_ARCHIVE}" ]]; then
  if [[ "${WEB_FREECAD_OFFLINE:-0}" == "1" ]]; then
    echo "Missing offline Qt source archive: ${QT_DOWNLOAD_DIR}/${QT_ARCHIVE}" >&2
    exit 1
  fi
  QT_ARCHIVE_PART="${QT_DOWNLOAD_DIR}/${QT_ARCHIVE}.part"
  curl -fL --retry 3 --retry-delay 2 \
    "https://mirrors.ustc.edu.cn/qtproject/archive/qt/6.8/${QT_VERSION}/submodules/${QT_ARCHIVE}" \
    -o "${QT_ARCHIVE_PART}"
  printf '%s  %s\n' "${QT_ARCHIVE_SHA256}" "${QT_ARCHIVE_PART}" | sha256sum -c -
  mv "${QT_ARCHIVE_PART}" "${QT_DOWNLOAD_DIR}/${QT_ARCHIVE}"
fi
printf '%s  %s\n' "${QT_ARCHIVE_SHA256}" "${QT_DOWNLOAD_DIR}/${QT_ARCHIVE}" | sha256sum -c -

if [[ ! -x "${QT_SOURCE_DIR}/configure" ]]; then
  mkdir -p "${QT_SOURCE_DIR}"
  tar -xJf "${QT_DOWNLOAD_DIR}/${QT_ARCHIVE}" -C "${QT_SOURCE_DIR}" --strip-components=1
fi

printf '%s\n' "EMSCRIPTEN_ROOT = emsdk_path + '/upstream/emscripten'" > "${EMSDK_SHIM_DIR}/.emscripten"
ln -sfn /usr/share/emscripten "${EMSDK_SHIM_DIR}/upstream/emscripten"
ln -sfn "$(command -v emcc)" "${EMSDK_SHIM_DIR}/emcc"
ln -sfn "$(command -v em++)" "${EMSDK_SHIM_DIR}/em++"

mkdir -p "${QT_BUILD_DIR}" "${QT_INSTALL_DIR}"
if [[ ! -f "${QT_BUILD_DIR}/build.ninja" ]]; then
  (
    cd "${QT_BUILD_DIR}"
    EMSDK="${EMSDK_SHIM_DIR}" "${QT_SOURCE_DIR}/configure" \
      -platform wasm-emscripten \
      -qt-host-path /usr \
      -prefix /qt \
      -extprefix "${QT_INSTALL_DIR}" \
      -static -release -optimize-size \
      -no-gui -no-widgets -no-dbus -no-feature-network -no-feature-thread \
      -nomake examples -nomake tests -nomake benchmarks -nomake manual-tests \
      -no-pch -no-sbom \
      -- -G Ninja \
      -DCMAKE_TOOLCHAIN_FILE=/usr/share/emscripten/cmake/Modules/Platform/Emscripten.cmake \
      -DCMAKE_BUILD_TYPE=Release \
      -DQT_BUILD_TOOLS_WHEN_CROSSCOMPILING=OFF \
      -DQT_FEATURE_wasm_simd128=OFF \
      -DQT_FEATURE_wasm_exceptions=OFF \
      -DQt6HostInfo_DIR=/usr/lib/x86_64-linux-gnu/cmake/Qt6HostInfo
  )
fi

cmake --build "${QT_BUILD_DIR}" --target Core --parallel "${JOBS}"
cmake --install "${QT_BUILD_DIR}" --component Devel
cmake --install "${QT_BUILD_DIR}/src/corelib"

for path in \
  "${QT_BUILD_DIR}/lib/libQt6Core.a" \
  "${QT_BUILD_DIR}/lib/libQt6BundledPcre2.a" \
  "${QT_BUILD_DIR}/lib/libQt6BundledZLIB.a"; do
  if [[ ! -s "${path}" ]]; then
    echo "Qt wasm Core output is missing: ${path}" >&2
    exit 1
  fi
  cp -f "${path}" "${QT_INSTALL_DIR}/lib/$(basename "${path}")"
done
if [[ ! -s "${QT_INSTALL_DIR}/include/QtCore/QByteArray" ]]; then
  echo "Qt wasm Core headers were not installed into ${QT_INSTALL_DIR}." >&2
  exit 1
fi

sha256sum \
  "${QT_DOWNLOAD_DIR}/${QT_ARCHIVE}" \
  "${QT_INSTALL_DIR}/lib/libQt6Core.a" \
  "${QT_INSTALL_DIR}/lib/libQt6BundledPcre2.a" \
  "${QT_INSTALL_DIR}/lib/libQt6BundledZLIB.a"
