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

_realname=dcmtk
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.6.8
pkgrel=4
pkgdesc="A collection of libraries and applications implementing large parts of the DICOM standard (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='http://dicom.offis.de/dcmtk'
msys2_repository_url="https://git.dcmtk.org/?p=dcmtk.git"
msys2_references=(
  "cpe: cpe:/a:offis:dcmtk"
)
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-icu"
             "${MINGW_PACKAGE_PREFIX}-libiconv"
             "${MINGW_PACKAGE_PREFIX}-autotools")
optdepends=("${MINGW_PACKAGE_PREFIX}-icu"
            "${MINGW_PACKAGE_PREFIX}-libiconv")
source=("https://dicom.offis.de/download/dcmtk/dcmtk${pkgver//./}/${_realname}-${pkgver}.tar.gz"
        "001-fix-cmake-config-install-destination.patch"
        "002-properly-detect-canonical-host.patch"
        "003-fixed-compilation-with-gcc-15.patch"::"https://git.dcmtk.org/?p=dcmtk.git;a=patch;h=c29bebcd19bb963d72dcd00ca8745f60f6b9a39e")
sha256sums=('232076655503138debf2f624109f1799e539354f186ce4e04b27cf82a9d8720f'
            'ff124b6f26c5a5982268597c6285e2e320a248a090046b45618e79cb8a6fc099'
            'b9cd641267f9aeb055608f045cc91ad5b99b071897aa849ed3282aa03b5ae925'
            '46ead518b98b55d8eb442f7550b080891d74d6612bb89a1fd9426ee31a598ba1')

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 \
    001-fix-cmake-config-install-destination.patch \
    002-properly-detect-canonical-host.patch \
    003-fixed-compilation-with-gcc-15.patch

  cd config
  ./rootconf
}

build() {
  # this is just to get arith.h
  cd "${srcdir}"/${_realname}-${pkgver}

  ./configure --ignore-deprecation
  make config-tests-all
  install -D config/include/dcmtk/config/arith.h "${srcdir}"/build-${MSYSTEM}/config/include/dcmtk/config/arith.h

  # continue the build using cmake
  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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DDCMTK_WITH_ICONV=ON \
      -DDCMTK_WITH_ICU=ON \
      -DDCMTK_WITH_LIBPNG=ON \
      -DDCMTK_WITH_LIBXML=ON \
      -DDCMTK_WITH_OPENJPEG=ON \
      -DDCMTK_WITH_OPENSSL=ON \
      -DDCMTK_WITH_SNDFILE=ON \
      -DDCMTK_WITH_TIFF=ON \
      -DDCMTK_WITH_ZLIB=ON \
      ../${_realname}-${pkgver}

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

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

  "${MINGW_PREFIX}"/bin/ctest.exe
}

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

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

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # Fix pkgconfig file
  sed -e "s|${PREFIX_WIN}|\"\$\{prefix\}\"|g" -i "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/dcmtk.pc
  # Fix cmake-config files
  find "${pkgdir}${MINGW_PREFIX}/lib/cmake" -type f -name '*.cmake' \
    -exec sed -i -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" {} \;

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYRIGHT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
