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

_realname=gdcm
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.0.8
pkgrel=3
pkgdesc="The Grassroots DICOM library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://gdcm.sourceforge.io/wiki/index.php"
license=("BSD")
depends=("${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-json-c"
         #"${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-docbook-xsl"
             "${MINGW_PACKAGE_PREFIX}-ninja")
options=('staticlibs' 'strip')
source=(#"https://downloads.sourceforge.net/project/gdcm/gdcm%202.x/GDCM%20${pkgver}/gdcm-${pkgver}.tar.bz2"
        gdcm-${pkgver}.tar.gz::https://github.com/malaterre/GDCM/archive/v${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-cmake-use-mingw-getopt.patch)
sha256sums=('47b96be345b1611784f9e65fc39367c7450c9a1ef81c21f8acddfb6207098315'
            '94e68cbc4d7582e4cc90dccb6762a4c46c3c4b191ee54aa53a79449650e6ccd6'
            '77645101ea8425c601fb0717630b334c8a4ced3ea7f3625c11d338c56529e5fb'
            '018b083e74091776afaef9894fcb1c7554b36828f162bc4c9b2ea2a6b154b159'
            '008635edc8c200b2adf8adf33c3439d39706e9b7ed0e644d7d0ebe18d9a020eb'
            'ed9c4020bd39cadf4d78c72aa4be5afaf3d0a743406e8bee6c5936299e5d1d33'
            '85fd9a6a52ec8d6f756022b94545707f1bda1661bf82e2a7d3132ccf85b1ea86')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/0002-cmake-config-destination.patch
  patch -p1 -i ${srcdir}/0004-fix-find-openssl.patch
  patch -p1 -i ${srcdir}/0005-fix-cross-initializing.patch
  patch -p1 -i ${srcdir}/0006-openjpeg2-visibility.patch
  patch -p1 -i ${srcdir}/0007-getopt-global-var.patch

  #https://github.com/malaterre/GDCM/pull/124
  patch -p1 -i ${srcdir}/0008-cmake-use-mingw-getopt.patch
}

build() {
  echo "==> Build static version"
  [[ -d "${srcdir}/build-static-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-static-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-static-${MINGW_CHOST}" && cd "${srcdir}/build-static-${MINGW_CHOST}"

  CXXFLAGS+=" -Wno-ignored-attributes" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_BUILD_TYPE=Release \
    -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=ON \
    -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 ./

  echo "==> Build shared version"
  [[ -d "${srcdir}/build-shared-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-shared-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-shared-${MINGW_CHOST}" && cd "${srcdir}/build-shared-${MINGW_CHOST}"

  CXXFLAGS+=" -Wno-ignored-attributes" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_BUILD_TYPE=Release \
    -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=ON \
    -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-${MINGW_CHOST}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

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