# Maintainer: Rafal Brzegowy <rafal.brzegowy@yahoo.com>

_realname=opencascade
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=7.8.1
_pkgver=${pkgver//./_}
pkgrel=2
pkgdesc='Open CASCADE Technology, 3D modeling & numerical simulation (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://dev.opencascade.org'
msys2_repository_url="https://github.com/Open-Cascade-SAS/OCCT"
license=('spdx:LGPL-2.1-or-later WITH OCCT-exception-1.0')
depends=("${MINGW_PACKAGE_PREFIX}-ffmpeg4.4"
         "${MINGW_PACKAGE_PREFIX}-freeimage"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-tbb"
         "${MINGW_PACKAGE_PREFIX}-openvr"
         "${MINGW_PACKAGE_PREFIX}-tcl"
         "${MINGW_PACKAGE_PREFIX}-tk"
         "${MINGW_PACKAGE_PREFIX}-vtk")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-qt5-base"
             "${MINGW_PACKAGE_PREFIX}-rapidjson"
             "${MINGW_PACKAGE_PREFIX}-nlohmann-json")
source=("https://github.com/Open-Cascade-SAS/OCCT/archive/refs/tags/V${_pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-fix-compile-openvr.patch"
        # The following patches are ported from the Debian Science Team
        # https://salsa.debian.org/science-team/opencascade/-/tree/master/debian/patches
        # Source: https://salsa.debian.org/science-team/opencascade/-/blob/master/debian/patches/0002-GeomPlate_BuildAveragePlane-BasePlan-Don-t-set-yvect.patch
        "0002-GeomPlate_BuildAveragePlane-BasePlan-Don-t-set-yvect.patch"
        # Source: https://salsa.debian.org/science-team/opencascade/-/blob/master/debian/patches/0003-BRepFill_Filling-WireFromList-We-can-t-assume-that-a.patch
        "0003-BRepFill_Filling-WireFromList-We-can-t-assume-that-a.patch"
        # Source: https://salsa.debian.org/science-team/opencascade/-/blob/master/debian/patches/0004-BRepFill_Filling-Curve-constraints-confused-by-impli.patch
        "0004-BRepFill_Filling-Curve-constraints-confused-by-impli.patch"
        # Source: https://salsa.debian.org/science-team/opencascade/-/blob/master/debian/patches/0005-BRepFill_Filling-Don-t-even-attempt-to-build-with-em.patch
        "0005-BRepFill_Filling-Don-t-even-attempt-to-build-with-em.patch"
        # Source: https://salsa.debian.org/science-team/opencascade/-/blob/master/debian/patches/0006-BRepOffset_Tool-TryProject-Check-return-of-BRepLib-B.patch
        "0006-BRepOffset_Tool-TryProject-Check-return-of-BRepLib-B.patch"
        "0007-static-build.patch"
        "0008-clang-dllexport-attributes.patch"
        "0009-SSE2-detection-gcc.patch"
        "0010-Fix-TBB-detection.patch"
        "0011-Fix-linking-TKDE.patch"
        "0012-Avoid-invalid-conversion-error.patch"
        "0013-ExpToCasExe-Fix-building-static.patch"
        "0014-aarch64-mm_alloc.patch")
sha256sums=('7321af48c34dc253bf8aae3f0430e8cb10976961d534d8509e72516978aa82f5'
            '0bbba5858e3e478759dac3d4802f821ec633304a63aeef542e0e4e0e0674e205'
            '9e72d389359899d959fa66b131c9871c15a136bb600c246fcf7e05910d277335'
            'f126465de547d606c4af2e95bd550ba7d5528d53a9b73693078952f9a40b3cae'
            '1f6062eece494ccec34b2b7fae0004d3970d477835f78a3fbacb0180a60f279f'
            'fa627467f355b541216a90ca9170fb87893652d6608c129bd913b19b7d1c9ee5'
            'd39ce849914e6349161675b9d412351684eb6af4bed1aa31afbdc54fa153a532'
            'c202fa3d21b3342595a15690be9e870488154a37cbfbea0a847fff62b7466f3a'
            'ffacd5229719b17ba89f1f8cd078294507d09f4dec6cdf29e2a6084f14c2c642'
            '84582a55b2421562f84a50e2c0ade4df0de63ec0a43ce8632aec0a68c7742fd7'
            '3d927cd0b2def78db8def263cd40bfc8520b8c8520bf5a7543f0775e02e5e86e'
            '24e2814c352bc4141f24b1652a5b5d3c49649c8cd852faea017e0584245a4331'
            '214c120c465029412e8c61c04aa6caf1845f9df6597983c39ca07415de252c7f'
            '6c1e3cfebecc398424d94de43d501d2718800bf101f90a156c086fad4ba042e0'
            'b23f2278da8c90fad69aaea2ffec3e439702a24278ea804062e9c0207c93e915')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}

prepare() {
  cd "${srcdir}"/OCCT-${_pkgver}
  apply_patch_with_msg \
    0001-fix-compile-openvr.patch \
    0002-GeomPlate_BuildAveragePlane-BasePlan-Don-t-set-yvect.patch \
    0003-BRepFill_Filling-WireFromList-We-can-t-assume-that-a.patch \
    0004-BRepFill_Filling-Curve-constraints-confused-by-impli.patch \
    0005-BRepFill_Filling-Don-t-even-attempt-to-build-with-em.patch \
    0006-BRepOffset_Tool-TryProject-Check-return-of-BRepLib-B.patch \
    0007-static-build.patch \
    0008-clang-dllexport-attributes.patch \
    0009-SSE2-detection-gcc.patch \
    0010-Fix-TBB-detection.patch \
    0011-Fix-linking-TKDE.patch \
    0012-Avoid-invalid-conversion-error.patch \
    0013-ExpToCasExe-Fix-building-static.patch \
    0014-aarch64-mm_alloc.patch

  find . -name "*.orig" -exec rm -f {} \;
}

build() {
  local common_config
  common_config=(
    -DUSE_D3D=1
    -D3RDPARTY_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_TK_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_TK_DLL_DIR="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_TK_DLL="${MINGW_PREFIX}/bin/tk86.dll"
    -D3RDPARTY_TK_LIBRARY_DIR="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_TK_LIBRARY="${MINGW_PREFIX}/lib/libtk86.dll.a"
    -D3RDPARTY_TCL_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_TCL_DLL_DIR="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_TCL_DLL="${MINGW_PREFIX}/bin/tcl86.dll"
    -D3RDPARTY_TCL_LIBRARY_DIR="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_TCL_LIBRARY="${MINGW_PREFIX}/lib/libtcl86.dll.a"
    -D3RDPARTY_FREETYPE_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_FREETYPE_DLL_DIR="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_FREETYPE_DLL="${MINGW_PREFIX}/bin/libfreetype-6.dll"
    -D3RDPARTY_FREETYPE_LIBRARY_DIR="${MINGW_PREFIX}/lib/"
    -DUSE_FFMPEG=1
    -D3RDPARTY_FFMPEG_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_FFMPEG_INCLUDE_DIR="${MINGW_PREFIX}/include/ffmpeg4.4"
    -D3RDPARTY_FFMPEG_DLL_DIR_avformat="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_FFMPEG_DLL_avformat="${MINGW_PREFIX}/bin/avformat-58.dll"
    -D3RDPARTY_FFMPEG_LIBRARY_DIR_avformat="${MINGW_PREFIX}/lib/ffmpeg4.4"
    -D3RDPARTY_FFMPEG_DLL_DIR_avutil="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_FFMPEG_DLL_avutil="${MINGW_PREFIX}/bin/avutil-56.dll"
    -D3RDPARTY_FFMPEG_LIBRARY_DIR_avutil="${MINGW_PREFIX}/lib/ffmpeg4.4"
    -D3RDPARTY_FFMPEG_DLL_DIR_avcodec="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_FFMPEG_DLL_avcodec="${MINGW_PREFIX}/bin/avcodec-58.dll"
    -D3RDPARTY_FFMPEG_LIBRARY_DIR_avcodec="${MINGW_PREFIX}/lib/ffmpeg4.4"
    -D3RDPARTY_FFMPEG_DLL_DIR_swscale="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_FFMPEG_DLL_swscale="${MINGW_PREFIX}/bin/swscale-5.dll"
    -D3RDPARTY_FFMPEG_LIBRARY_DIR_swscale="${MINGW_PREFIX}/lib/ffmpeg4.4"
    -DUSE_OPENVR=1
    -D3RDPARTY_OPENVR_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_OPENVR_DLL_DIR_openvr_api="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_OPENVR_DLL_openvr_api="${MINGW_PREFIX}/bin/libopenvr_api.dll"
    -D3RDPARTY_OPENVR_LIBRARY_DIR_openvr_api="${MINGW_PREFIX}/lib/"
    -DUSE_RAPIDJSON=1
    -D3RDPARTY_RAPIDJSON_DIR="${MINGW_PREFIX}"
    -DUSE_FREEIMAGE=1
    -D3RDPARTY_FREEIMAGE_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_FREEIMAGE_DLL_DIR_freeimage="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_FREEIMAGE_DLL_freeimage="${MINGW_PREFIX}/bin/libfreeimage-3.dll"
    -D3RDPARTY_FREEIMAGE_LIBRARY_DIR_freeimage="${MINGW_PREFIX}/lib/"
    -DUSE_TBB=1
    -D3RDPARTY_TBB_DLL_DIR="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_TBB_LIBRARY_DIR="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_TBB_LIBRARY="${MINGW_PREFIX}/lib/libtbb12.dll.a"
    -D3RDPARTY_TBBMALLOC_LIBRARY_DIR="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_TBBMALLOC_LIBRARY="${MINGW_PREFIX}/lib/libtbbmalloc.dll.a"
    -DUSE_VTK=ON
    -D3RDPARTY_VTK_DLL_DIR="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_VTK_LIBRARY_DIR="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_VTK_INCLUDE_DIR="${MINGW_PREFIX}/include/vtk"
  )

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

  msg2 "Building shared libraries"
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
    -GNinja \
    "${_extra_config[@]}" \
    -DINSTALL_DIR_LAYOUT="Unix" \
    -DBUILD_LIBRARY_TYPE="Shared" \
    "${common_config[@]}" \
    -D3RDPARTY_FREETYPE_LIBRARY="${MINGW_PREFIX}/lib/libfreetype.dll.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_avformat="${MINGW_PREFIX}/lib/ffmpeg4.4/libavformat.dll.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_avutil="${MINGW_PREFIX}/lib/ffmpeg4.4/libavutil.dll.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_avcodec="${MINGW_PREFIX}/lib/ffmpeg4.4/libavcodec.dll.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_swscale="${MINGW_PREFIX}/lib/ffmpeg4.4/libswscale.dll.a" \
    -D3RDPARTY_FREEIMAGE_LIBRARY="${MINGW_PREFIX}/lib/libfreeimage.dll.a" \
    -D3RDPARTY_OPENVR_LIBRARY_openvr_api="${MINGW_PREFIX}/lib/libopenvr_api.dll.a" \
    ../OCCT-${_pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  # Shared Install
  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/OCCT-${_pkgver}/LICENSE_LGPL_21.txt \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE_LGPL_21.txt
  install -Dm644 "${srcdir}"/OCCT-${_pkgver}/OCCT_LGPL_EXCEPTION.txt \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/OCCT_LGPL_EXCEPTION.txt

  # Remove unnecessary batch scripts (#16304)
  rm -v "${pkgdir}${MINGW_PREFIX}/bin/"*.bat

  # Fix cmake files
  find "${pkgdir}${MINGW_PREFIX}/lib/cmake" -type f \( -name '*.cmake' \) \
      -exec sed -i -e "s|$(cygpath -m "${MINGW_PREFIX}")|\$\{_IMPORT_PREFIX\}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib/cmake" -type f \( -name '*.cmake' \) \
      -exec sed -i -e 's|\${OCCT_INSTALL_BIN_LETTER}||g' {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib/cmake" -type f \( -name '*.cmake' \) \
      -exec sed -i -e "s|$(cygpath -m "${MINGW_PREFIX}")/lib/||g" {} \;
}
