# Maintainer: Mario Emmenlauer <memmenlauer@biodataanalysis.de>

_realname=gdcm
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.0.24
pkgrel=18
pkgdesc="The Grassroots DICOM library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://gdcm.sourceforge.io/wiki/index.php"
msys2_repository_url="https://github.com/malaterre/GDCM"
license=("spdx:BSD-3-Clause-Clear")
depends=("${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-json-c"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-libxslt"
             "${MINGW_PACKAGE_PREFIX}-docbook-xsl")
source=(#"https://downloads.sourceforge.net/project/gdcm/gdcm%202.x/GDCM%20${pkgver}/gdcm-${pkgver}.tar.bz2"
        https://github.com/malaterre/GDCM/archive/v${pkgver}/gdcm-${pkgver}.tar.gz
        0002-cmake-config-destination.patch
        0004-fix-find-openssl.patch
        0005-fix-cross-initializing.patch
        0006-openjpeg2-visibility.patch
        0007-getopt-global-var.patch
        0008-no-posix_memalign-on-MINGW.patch
        0009-link-to-iconv.patch
        0010-poppler.patch)
sha256sums=('d88519a094797c645ca34797a24a14efc10965829c4c3352c8ef33782a556336'
            '94e68cbc4d7582e4cc90dccb6762a4c46c3c4b191ee54aa53a79449650e6ccd6'
            '77645101ea8425c601fb0717630b334c8a4ced3ea7f3625c11d338c56529e5fb'
            '018b083e74091776afaef9894fcb1c7554b36828f162bc4c9b2ea2a6b154b159'
            '008635edc8c200b2adf8adf33c3439d39706e9b7ed0e644d7d0ebe18d9a020eb'
            'ed9c4020bd39cadf4d78c72aa4be5afaf3d0a743406e8bee6c5936299e5d1d33'
            'c2222761e9a2b7572a553d6af22dfd87c3a1ff53e8d876b8c26f47436cf54218'
            '6cd51dcc48fe587f655fdcb4ba1b15adff8b95a97a943d94b59d6cb85143e0e9'
            '03459fc07a5bb1c87e5d41cd5d9026bc697ce361cecf1016131814a86660b9df')

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

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  apply_patch_with_msg \
    0002-cmake-config-destination.patch \
    0004-fix-find-openssl.patch \
    0005-fix-cross-initializing.patch \
    0006-openjpeg2-visibility.patch \
    0007-getopt-global-var.patch \
    0008-no-posix_memalign-on-MINGW.patch \
    0009-link-to-iconv.patch \
    0010-poppler.patch
}

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 [[ ${CARCH} == aarch64 ]]; then
    _extra_config+=("-DGDCM_BUILD_DOCBOOK_MANPAGES=OFF")
  fi

  echo "==> Build static version"
  mkdir -p "${srcdir}/build-static-${MSYSTEM}" && cd "${srcdir}/build-static-${MSYSTEM}"

  CXXFLAGS+=" -Wno-ignored-attributes" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_CXX_STANDARD=20 \
    "${_extra_config[@]}" \
    -DCMAKE_SYSTEM_PREFIX_PATH="${MINGW_PREFIX}" \
    -DGDCM_BUILD_SHARED_LIBS=OFF \
    -DGDCM_USE_SYSTEM_ZLIB=ON \
    -DGDCM_USE_SYSTEM_OPENSSL=ON \
    -DGDCM_USE_SYSTEM_UUID=OFF \
    -DGDCM_USE_SYSTEM_EXPAT=ON \
    -DGDCM_USE_SYSTEM_JSON=ON \
    -DGDCM_USE_SYSTEM_PAPYRUS3=OFF \
    -DGDCM_USE_SYSTEM_SOCKETXX=OFF \
    -DGDCM_USE_SYSTEM_LJPEG=OFF \
    -DGDCM_USE_SYSTEM_OPENJPEG=OFF \
    -DGDCM_USE_SYSTEM_CHARLS=OFF \
    -DGDCM_USE_SYSTEM_POPPLER=ON \
    -DGDCM_USE_SYSTEM_LIBXML2=ON \
    -DGDCM_BUILD_EXAMPLES=OFF \
    -DGDCM_BUILD_TESTING=OFF \
    -DGDCM_WRAP_PYTHON=OFF \
    -DGDCM_WRAP_PERL=OFF \
    -DGDCM_WRAP_PHP=OFF \
    -DGDCM_WRAP_JAVA=OFF \
    -DGDCM_WRAP_CSHARP=OFF \
    -DGDCM_USE_JPEGLS=ON \
    -DGDCM_DOCUMENTATION=OFF \
    -DGDCM_USE_VTK=OFF \
    -DGDCM_USE_PARAVIEW=OFF \
    -DGDCM_BUILD_APPLICATIONS=OFF \
    ../${_realname}-${pkgver}

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

  echo "==> Build shared version"
  mkdir -p "${srcdir}/build-shared-${MSYSTEM}" && cd "${srcdir}/build-shared-${MSYSTEM}"

  CXXFLAGS+=" -Wno-ignored-attributes" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_CXX_STANDARD=20 \
    "${_extra_config[@]}" \
    -DCMAKE_SYSTEM_PREFIX_PATH="${MINGW_PREFIX}" \
    -DGDCM_BUILD_SHARED_LIBS=ON \
    -DGDCM_USE_SYSTEM_ZLIB=ON \
    -DGDCM_USE_SYSTEM_OPENSSL=ON \
    -DGDCM_USE_SYSTEM_UUID=OFF \
    -DGDCM_USE_SYSTEM_EXPAT=ON \
    -DGDCM_USE_SYSTEM_JSON=ON \
    -DGDCM_USE_SYSTEM_PAPYRUS3=OFF \
    -DGDCM_USE_SYSTEM_SOCKETXX=OFF \
    -DGDCM_USE_SYSTEM_LJPEG=OFF \
    -DGDCM_USE_SYSTEM_OPENJPEG=ON \
    -DGDCM_USE_SYSTEM_CHARLS=OFF \
    -DGDCM_USE_SYSTEM_POPPLER=ON \
    -DGDCM_USE_SYSTEM_LIBXML2=ON \
    -DGDCM_BUILD_EXAMPLES=OFF \
    -DGDCM_BUILD_TESTING=OFF \
    -DGDCM_WRAP_PYTHON=OFF \
    -DGDCM_WRAP_PERL=OFF \
    -DGDCM_WRAP_PHP=OFF \
    -DGDCM_WRAP_JAVA=OFF \
    -DGDCM_WRAP_CSHARP=OFF \
    -DGDCM_USE_JPEGLS=ON \
    -DGDCM_DOCUMENTATION=OFF \
    -DGDCM_USE_VTK=OFF \
    -DGDCM_USE_PARAVIEW=OFF \
    -DGDCM_BUILD_APPLICATIONS=ON \
    ../${_realname}-${pkgver}

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

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

  cd "${srcdir}/build-shared-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .
}
