# Maintainer: Sergey Popov <popov.sa@gmail.com>
# Contributor: Mario Emmenlauer <memmenlauer@biodataanalysis.de>

_realname=vtk
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=8.2.0
pkgrel=6
pkgdesc="A software system for 3D computer graphics, image processing and visualization (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.vtk.org/"
license=("BSD")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-double-conversion"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-gdal"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-intel-tbb"
         "${MINGW_PACKAGE_PREFIX}-jsoncpp"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libharu"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libogg"
         "${MINGW_PACKAGE_PREFIX}-libtheora"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-pugixml"
         "${MINGW_PACKAGE_PREFIX}-qt5"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-libmariadbclient"
             "${MINGW_PACKAGE_PREFIX}-postgresql"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-tk")
optdepends=("${MINGW_PACKAGE_PREFIX}-boost: InfovisBoost and InfovisBoostGraphAlgorithms modules"
            "${MINGW_PACKAGE_PREFIX}-libmariadbclient: interface to MariaDB"
            "${MINGW_PACKAGE_PREFIX}-postgresql: interface to PostgreSQL"
            "${MINGW_PACKAGE_PREFIX}-python: Python bindings"
            "${MINGW_PACKAGE_PREFIX}-python-matplotlib: Matplotlib renderer"
            "${MINGW_PACKAGE_PREFIX}-tk: TCL bindings, Python Tk widgets")
source=(https://www.vtk.org/files/release/${pkgver%.*}/VTK-${pkgver}.tar.gz
        "001-vtk-mingw-w64.patch"
        "004-fix-linking-rendering-tk.patch"
        "005-remove-bogus-stdint-check.patch"
        "006-win7-defines-guard.patch"
        "007-fix-linking-movie.patch"
        "008-fix-python-modules-path.patch"
        "020-python38-compat.patch"
        "030-pugixml.patch"
        "031-fno-common.patch"
        "032-freetype-2.10.3-provide-FT_CALLBACK_DEF.patch"
        "033-qt-5.15.patch")
sha256sums=('34c3dc775261be5e45a8049155f7228b6bd668106c72a3c435d95730d17d57bb'
            'db3163012fc7d90f6ffd8300a7a306a609e7c69a88a1dcfb9cb5b5df4391e3ad'
            '7f428a9c15b2dfc23fff2a19a11c48a628c9b52f82ec0d1dca37d0a5cffc9aef'
            '769e28e106e612671433eb2f84ab6c0c9ef676109d690f6fe330641631c1fd2b'
            '087d570104d2ae7f00437897e88225a1eb904b125e3a27bb9392ddf8ef068f6c'
            'ec39407b6174c9b1326389b4badf1c0c377d74a6dd2c53caf983613fafe43c81'
            'b6d6f4de0e58251843dec3dfad0ae5d5308c66ab8e55a56eeb3855cd699b02f3'
            '1d9a3227feb50d08cb507d2a750b480f15eb1819f15c5b88474ba944c51f256a'
            'cbcc8a0c81450e015c2ce4035f2f128956d20a22be581199446c4a72c0396190'
            'a4597f0410bea856b06d023d9db7d2661b505eec110f4beccc155f6aa2c593f7'
            'aa43d6b702e3e1bca2cb8560c1c7e6ce5681cef8a89e9b6442f8037bc295c21c'
            'de1d2088c54bc7fdd494cb8a90464d2f53dc7cabddcd0415d9fb8cc4de448d0e')

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

  patch -Np1 -i "${srcdir}/001-vtk-mingw-w64.patch"
  patch -Np1 -i "${srcdir}/004-fix-linking-rendering-tk.patch"
  # it will find stdint from msys2-runtime-devel, but that's useless
  patch -Np1 -i "${srcdir}/005-remove-bogus-stdint-check.patch"
  patch -Np1 -i "${srcdir}/006-win7-defines-guard.patch"
  patch -Np1 -i "${srcdir}/007-fix-linking-movie.patch"
  patch -Np1 -i "${srcdir}/008-fix-python-modules-path.patch"
  patch -Np1 -i "${srcdir}/020-python38-compat.patch"
  patch -Np1 -i "${srcdir}/030-pugixml.patch"
  patch -Np1 -i "${srcdir}/031-fno-common.patch"
  patch -Np1 -i "${srcdir}/032-freetype-2.10.3-provide-FT_CALLBACK_DEF.patch"
  patch -Np1 -i "${srcdir}/033-qt-5.15.patch"
}

build() {
  CFLAGS+=" ${CPPFLAGS}"
  CXXFLAGS+=" ${CPPFLAGS} -std=gnu++11"
  [[ "$CARCH" = 'i686' ]] && {
    _cmakeopts=('-DVTK_USE_64BIT_IDS=OFF')
  }
  [[ "$CARCH" = 'x86_64' ]] && {
    _cmakeopts=('-DVTK_USE_64BIT_IDS=ON')
  }

  [[ -d ${srcdir}/build-${CARCH} ]] && rm -rf ${srcdir}/build-${CARCH}
  mkdir -p ${srcdir}/build-${CARCH} && cd ${srcdir}/build-${CARCH}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DVTK_INSTALL_QT_PLUGIN_DIR=;-DVTK_INSTALL_TCL_DIR=" \
  "${MINGW_PREFIX}/bin/cmake.exe" -Wno-dev \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DVTK_INSTALL_QT_PLUGIN_DIR="${MINGW_PREFIX}/share/qt5/plugins/designer" \
    -DVTK_INSTALL_TCL_DIR="${MINGW_PREFIX}/lib/vtk-${pkgver%.*}" \
    -DBUILD_DOCUMENTATION=OFF \
    -DBUILD_EXAMPLES=OFF \
    -DBUILD_TESTING=OFF \
    -DVTK_LEGACY_REMOVE=OFF \
    -DVTK_Group_Imaging=ON \
    -DVTK_Group_MPI=OFF \
    -DVTK_Group_Qt=ON \
    -DVTK_Group_Rendering=ON \
    -DVTK_Group_StandAlone=ON \
    -DVTK_Group_Tk=ON \
    -DVTK_Group_Views=ON \
    -DVTK_Group_Web=OFF \
    -DVTK_USE_SYSTEM_DOUBLECONVERSION=ON \
    -DVTK_USE_SYSTEM_EXPAT=ON \
    -DVTK_USE_SYSTEM_FREETYPE=ON \
    -DVTK_USE_SYSTEM_HDF5=ON \
    -DVTK_USE_SYSTEM_JPEG=ON \
    -DVTK_USE_SYSTEM_JSONCPP=ON \
    -DVTK_USE_SYSTEM_LIBPROJ=OFF \
    -DVTK_USE_SYSTEM_LIBHARU=OFF \
    -DVTK_USE_SYSTEM_LIBXML2=ON \
    -DVTK_USE_SYSTEM_LZ4=ON \
    -DVTK_USE_SYSTEM_NETCDF=OFF \
    -DVTK_USE_SYSTEM_OGG=ON \
    -DVTK_USE_SYSTEM_PUGIXML=ON \
    -DVTK_USE_SYSTEM_THEORA=ON \
    -DVTK_USE_SYSTEM_PNG=ON \
    -DVTK_USE_SYSTEM_TIFF=ON \
    -DVTK_USE_SYSTEM_ZLIB=ON \
    -DVTK_USE_VIDEO_FOR_WINDOWS=ON \
    -DModule_vtkIOFFMPEG=ON \
    -DModule_vtkIOMySQL=ON \
    -DMYSQL_INCLUDE_DIRECTORIES="${MINGW_PREFIX}/include/mariadb" \
    -DMYSQL_LIBRARY="${MINGW_PREFIX}/lib/libmariadb.dll.a" \
    -DModule_vtkInfovisBoost=ON \
    -DModule_vtkInfovisBoostGraphAlgorithms=ON \
    -DModule_vtkIOGDAL=ON \
    -DModule_vtkIOODBC=OFF \
    -DModule_vtkIOPostgreSQL=ON \
    -DModule_vtkPythonInterpreter=ON \
    -DModule_vtkFiltersReebGraph=ON \
    -DModule_vtkRenderingFreeTypeFontConfig=ON \
    -DModule_vtkRenderingMatplotlib=ON \
    -DVTK_WRAP_JAVA=OFF \
    -DVTK_WRAP_PYTHON=ON \
    -DVTK_QT_VERSION=5 \
    -DPYTHON_EXECUTABLE="${MINGW_PREFIX}/bin/python.exe" \
    -DVTK_SMP_IMPLEMENTATION_TYPE=TBB \
    -DCMAKE_RC_FLAGS=--use-temp-file \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=ON \
    ${_cmakeopts[@]} \
    ../${_realname}-${pkgver}

  make
}

package() {
  cd "${srcdir}/build-${CARCH}"
  make -j1 DESTDIR="${pkgdir}" install

  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})

  pushd "${pkgdir}${MINGW_PREFIX}/lib/cmake/${_realname}-${pkgver%.*}" > /dev/null
  sed -s 's|Qt5::|Qt5|g' -i ./VTKTargets*.cmake
  sed -s "s|${PREFIX_DEPS}|\${_IMPORT_PREFIX}|g" -i ./VTKTargets.cmake
  find . -name '*.cmake' -exec \
    sed -s "s|${PREFIX_DEPS}|\${VTK_INSTALL_PREFIX}|g" -i {} \;
  popd > /dev/null

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