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

_realname=paraview
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=6.0.1
pkgrel=1
pkgdesc="Parallel Data Analysis and Visualization Application based on VTK. (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.paraview.org/'
msys2_repository_url="https://gitlab.kitware.com/paraview/paraview"
msys2_references=(
  'archlinux: paraview'
  'gentoo: sci-visualization/paraview'
)
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-double-conversion"
         "${MINGW_PACKAGE_PREFIX}-cgns"
         "${MINGW_PACKAGE_PREFIX}-fmt"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-jsoncpp"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-netcdf"
         "${MINGW_PACKAGE_PREFIX}-pugixml"
         "${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-qt6-5compat"
         "${MINGW_PACKAGE_PREFIX}-qt6-base"
         "${MINGW_PACKAGE_PREFIX}-qt6-tools"
         "${MINGW_PACKAGE_PREFIX}-tbb"
         "${MINGW_PACKAGE_PREFIX}-vtk"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-libxslt"
             "${MINGW_PACKAGE_PREFIX}-proj"
             "${MINGW_PACKAGE_PREFIX}-qt6-svg"
             "${MINGW_PACKAGE_PREFIX}-python-numpy"
             "${MINGW_PACKAGE_PREFIX}-cli11"
             "${MINGW_PACKAGE_PREFIX}-fast_float"
             "${MINGW_PACKAGE_PREFIX}-nlohmann-json")
optdepends=("${MINGW_PACKAGE_PREFIX}-python-numpy"
            "${MINGW_PACKAGE_PREFIX}-python-matplotlib")
source=("https://www.paraview.org/files/v${pkgver%.*}/ParaView-v${pkgver}.tar.xz")
sha256sums=('5e56ac7af5e925b3cfd3fab82470933cbabc7e8fda87e14af64f995d6064eb06')

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

prepare() {
  cd "${srcdir}"/ParaView-v${pkgver}
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

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

  _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")

  CXXFLAGS+=" -w -Wno-register" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe -Wno-dev \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DPARAVIEW_BUILD_SHARED_LIBS=ON \
      -DPARAVIEW_USE_PYTHON=ON \
      -DPARAVIEW_USE_QT=ON \
      -DPARAVIEW_USE_MPI=OFF \
      -DPARAVIEW_ENABLE_GDAL=ON \
      -DPARAVIEW_ENABLE_LAS=ON \
      -DPARAVIEW_ENABLE_OPENTURNS=ON \
      -DPARAVIEW_ENABLE_PDAL=ON \
      -DPARAVIEW_VERSIONED_INSTALL=OFF \
      -DPARAVIEW_BUILD_WITH_EXTERNAL=ON \
      -DPARAVIEW_PYTHON_SITE_PACKAGES_SUFFIX="lib/python${_pyver}/site-packages/pv/" \
      -DPARAVIEW_USE_EXTERNAL_VTK=ON \
      -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
      ../ParaView-v${pkgver}

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

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  MSYS2_ARG_CONV_EXCL="-p" python -m compileall \
    -o 0 -o 1 -q -s"${pkgdir}" -p"/" "${pkgdir}${MINGW_PREFIX}/lib/python"*

  install -Dm644 "${srcdir}"/ParaView-v${pkgver}/Copyright.txt \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
