# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=freecad
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.0.2
pkgrel=2
_OS_commit=09d6175a
pkgdesc="Free and Opensource multiplatform 3D parametric modeler (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://freecadweb.org/'
msys2_repository_url="https://github.com/FreeCAD/FreeCAD"
msys2_references=(
  'archlinux: freecad'
)
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-boost-libs"
         "${MINGW_PACKAGE_PREFIX}-coin"
         "${MINGW_PACKAGE_PREFIX}-fmt"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-med"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-opencascade"
         "${MINGW_PACKAGE_PREFIX}-openscad"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-numpy"
         "${MINGW_PACKAGE_PREFIX}-python-packaging"
         "${MINGW_PACKAGE_PREFIX}-python-pivy"
         "${MINGW_PACKAGE_PREFIX}-pyside6"
         "${MINGW_PACKAGE_PREFIX}-qt6-base"
         "${MINGW_PACKAGE_PREFIX}-qt6-svg"
         "${MINGW_PACKAGE_PREFIX}-xerces-c"
         "${MINGW_PACKAGE_PREFIX}-vtk"
         "${MINGW_PACKAGE_PREFIX}-yaml-cpp"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-nlohmann-json"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "${MINGW_PACKAGE_PREFIX}-python-matplotlib"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools"
             "${MINGW_PACKAGE_PREFIX}-swig"
             "${MINGW_PACKAGE_PREFIX}-fast_float"
             "${MINGW_PACKAGE_PREFIX}-microsoft-gsl"
             "${MINGW_PACKAGE_PREFIX}-utf8cpp")
source=("https://github.com/FreeCAD/FreeCAD/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "https://github.com/Ondsel-Development/OndselSolver/archive/${_OS_commit}/OndselSolver-${_OS_commit}.tar.gz"
        "001-clang19-char-traits.patch"
        "002-fix-build-with-qt-6.10.patch"
        "https://github.com/FreeCAD/FreeCAD/commit/fe951f63.patch"
        "https://github.com/FreeCAD/FreeCAD/commit/8547e798.patch")
sha256sums=('228ee52f00627c7d8fa61998179deb01865ece69390829feb1300228d24f7e9e'
            '890fb6ddeb1b42face95854689ff7b96c926c368b7d616d9e31e4b35f79ddde8'
            '0dcc9a5ce20f080913f2bff71f5e2eb7d166e789f1012cea2009f94c9f30e5f2'
            '03dd01d48920d902b02a3d62e2810bdc93b6bd7b68e5f901c0af93b0bc83eca0'
            '9dc082a9c4be6c5794b16642bd7acd1975b8b4815af63d18be9ba9830b8508ff'
            '1f9c1c044bd314747c11a77ede2f97764e7cc27a1b31d6535fccee3dcfd654cb')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  mv OndselSolver-*/* FreeCAD-${pkgver}/src/3rdParty/OndselSolver/

  cd FreeCAD-${pkgver}

  apply_patch_with_msg \
    001-clang19-char-traits.patch \
    002-fix-build-with-qt-6.10.patch
  # Fix Find Eigen3
  apply_patch_with_msg \
    fe951f63.patch \
    8547e798.patch

  # opencascade defines HAVE_TBB which makes FreeCAD uses TBB without linking to it.
  sed -i "s|HAVE_TBB|HAVE_NOT_TBB|g" src/Mod/Import/App/ImportOCAF.cpp
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${CC} == clang ]]; then
    CXXFLAGS+=" -Wno-macro-redefined"
  else
    CXXFLAGS+=" -Wno-template-id-cdtor"
  fi

  CXXFLAGS+=" -Wno-deprecated-declarations -Wno-unknown-pragmas" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;" \
    cmake -Wno-dev \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DFREECAD_QT_VERSION="6" \
      -DBUILD_TEST=OFF \
      -DBUILD_WEB=OFF \
      -DBUILD_ADDONMGR=OFF \
      -DENABLE_DEVELOPER_TESTS=OFF \
      -DINSTALL_TO_SITEPACKAGES=OFF \
      -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      "${_extra_config[@]}" \
      -S "FreeCAD-${pkgver}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}"

  install -Dm644 FreeCAD-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

  MSYS2_ARG_CONV_EXCL="-d" \
    python -m compileall -o 0 -o 1 -q -d"${MINGW_PREFIX}" "${pkgdir}${MINGW_PREFIX}"
}
