# Maintainer: Sergey Popov <popov.sa@gmail.com>

_realname=itk
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.4.4
pkgrel=2
pkgdesc='An open-source C++ toolkit for medical image processing (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.itk.org/'
msys2_repository_url="https://github.com/InsightSoftwareConsortium/ITK"
msys2_references=(
  'aur: itk'
)
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-dcmtk"
         "${MINGW_PACKAGE_PREFIX}-double-conversion"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-gdcm"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         #"${MINGW_PACKAGE_PREFIX}-opencl-icd"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "${MINGW_PACKAGE_PREFIX}-gtest"
             "${MINGW_PACKAGE_PREFIX}-opencv"
             "${MINGW_PACKAGE_PREFIX}-vtk"
             "${MINGW_PACKAGE_PREFIX}-adios2"
             $([[ ${MSYSTEM} == MINGW64 ]] || echo "${MINGW_PACKAGE_PREFIX}-anari-sdk")
             "${MINGW_PACKAGE_PREFIX}-cgns"
             "${MINGW_PACKAGE_PREFIX}-ffmpeg"
             "${MINGW_PACKAGE_PREFIX}-cli11"
             "${MINGW_PACKAGE_PREFIX}-fast_float"
             "${MINGW_PACKAGE_PREFIX}-gdal"
             "${MINGW_PACKAGE_PREFIX}-gl2ps"
             "${MINGW_PACKAGE_PREFIX}-libmariadbclient"
             "${MINGW_PACKAGE_PREFIX}-liblas"
             "${MINGW_PACKAGE_PREFIX}-libogg"
             "${MINGW_PACKAGE_PREFIX}-libtheora"
             "${MINGW_PACKAGE_PREFIX}-opencascade"
             "${MINGW_PACKAGE_PREFIX}-openslide"
             "${MINGW_PACKAGE_PREFIX}-openturns"
             "${MINGW_PACKAGE_PREFIX}-openvdb"
             "${MINGW_PACKAGE_PREFIX}-openvr"
             "${MINGW_PACKAGE_PREFIX}-pdal"
             "${MINGW_PACKAGE_PREFIX}-proj"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-qt6-base"
             "${MINGW_PACKAGE_PREFIX}-qt6-declarative"
             "${MINGW_PACKAGE_PREFIX}-unixodbc"
             "${MINGW_PACKAGE_PREFIX}-utf8cpp")
optdepends=("${MINGW_PACKAGE_PREFIX}-opencv: ITK-OpenCV bridge"
            "${MINGW_PACKAGE_PREFIX}-vtk: ITK-VTK bridge")
source=("https://github.com/InsightSoftwareConsortium/ITK/archive/v${pkgver}/ITK-${pkgver}.tar.gz"
        libminc-pr-129.patch)
sha256sums=('683027617442391220eb79869d71652a54c4b47517ca6ca14d4b01c2583f5a1c'
            '14c1fe4b41fa77aed8483dbece655740dd485e8d78184c9351063a5fb77fa0b4')

prepare() {
  cd "ITK-${pkgver}"
  patch -p1 -i "${srcdir}"/libminc-pr-129.patch
}

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

  if [[ ${CARCH} == x86_64 ]]; then
    _extra_config+=("-DVNL_CONFIG_ENABLE_SSE2_ROUNDING=ON")
  else
    _extra_config+=("-DVNL_CONFIG_ENABLE_SSE2_ROUNDING=OFF")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_DOCUMENTATION=OFF \
    -DBUILD_EXAMPLES=OFF \
    -DBUILD_TESTING=OFF \
    -DITK_INSTALL_INCLUDE_DIR=include/ITK \
    -DITK_INSTALL_DATA_DIR=share/ITK \
    -DITK_INSTALL_DOC_DIR=share/doc/ITK \
    -DITK_INSTALL_PACKAGE_DIR=lib/cmake/ITK \
    -DITK_CUSTOM_LIBRARY_SUFFIX="" \
    -DITK_LEGACY_REMOVE=ON \
    -DITK_FUTURE_LEGACY_REMOVE=ON \
    -DITK_BUILD_DEFAULT_MODULES=OFF \
    -DITKGroup_Core=ON \
    -DITKGroup_IO=$([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] && echo "ON" || echo "OFF") \
    -DITKGroup_Filtering=ON \
    -DITKGroup_Registration=ON \
    -DITKGroup_Segmentation=ON \
    -DITKGroup_Numerics=ON \
    -DITKGroup_Video=ON \
    -DITKGroup_ThirdParty=ON \
    -DITKGroup_Bridge=ON \
    -DITKGroup_Nonunit=$([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] && echo "ON" || echo "OFF") \ \
    -DITKGroup_Compatibility=ON \
    -DModule_ITKVideoBridgeOpenCV=ON \
    -DModule_ITKVtkGlue=ON \
    -DITK_USE_GPU=OFF \
    -DUSE_FFTWD=ON \
    -DUSE_FFTWF=ON \
    -DITK_USE_SYSTEM_LIBRARIES=ON \
    -DITK_USE_SYSTEM_DCMTK=ON \
    -DITK_USE_SYSTEM_DOUBLECONVERSION=ON \
    -DITK_USE_SYSTEM_EIGEN=ON \
    -DITK_USE_SYSTEM_EXPAT=ON \
    -DITK_USE_SYSTEM_FFTW=ON \
    -DITK_USE_SYSTEM_GDCM=ON \
    -DITK_USE_SYSTEM_GOOGLETEST=ON \
    -DITK_USE_SYSTEM_HDF5=ON \
    -DITK_USE_SYSTEM_JPEG=ON \
    -DITK_USE_SYSTEM_MINC=OFF \
    -DITK_USE_SYSTEM_PNG=ON \
    -DITK_USE_SYSTEM_TIFF=ON \
    -DITK_USE_SYSTEM_VXL=OFF \
    -DITK_USE_SYSTEM_ZLIB=ON \
    -DITK_SKIP_PATH_LENGTH_CHECKS=1 \
    -DITK_FORBID_DOWNLOADS=ON \
    -DVNL_CONFIG_THREAD_SAFE=ON \
    -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    "${_extra_config[@]}" \
    -S "ITK-${pkgver}" \
    -B "build-${MSYSTEM}"

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

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  sed -s "s|${PREFIX_DEPS}|\${_IMPORT_PREFIX}|g" \
    -i "${pkgdir}${MINGW_PREFIX}/lib/cmake/${_realname}/ITKTargets-release.cmake"
  find "${pkgdir}${MINGW_PREFIX}/lib/cmake/${_realname}" -name '*.cmake' -exec \
    sed -s "s|${PREFIX_DEPS}|\${ITK_INSTALL_PREFIX}|g" -i {} \;

  install -Dm644 "ITK-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -Dm644 "ITK-${pkgver}/NOTICE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/NOTICE"
  install -Dm644 "ITK-${pkgver}/Modules/ThirdParty/KWSys/src/KWSys/Copyright.txt" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/KWSys/Copyright.txt"
}
