# Maintainer: Oleg Tolmatcev <oleg.tolmatcev@gmail.com>

_realname=qpdf
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=12.2.0
pkgrel=1
pkgdesc="QPDF: A Content-Preserving PDF Transformation System (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://qpdf.sourceforge.io/'
msys2_repository_url="https://github.com/qpdf/qpdf"
msys2_references=(
  "cpe: cpe:/a:qpdf_project:qpdf"
)
license=('spdx:Apache-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-gnutls"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-zlib")
source=(https://github.com/qpdf/qpdf/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz{,.asc}
        001-do-not-copy-dlls.patch
        002-do-not-change-library-prefix-and-suffix.patch)
sha256sums=('b3d1575b2218badc3549d6977524bb0f8c468c6528eebc8967bbe3078cf2cace'
            'SKIP'
            '4e1193db33b7887d3a751e41463af0e1fb3734aadcf5c5ffed1336b5ec54b84e'
            '423f029fd9edd3712423e69aadcac704e4c04e2220f47e17c2585c332def23a1')
validpgpkeys=('C2C96B10011FE009E6D1DF828A75D10998012C7E') # Jay Berkenbilt <ejb@ql.org>

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}"/001-do-not-copy-dlls.patch
  patch -p1 -i "${srcdir}"/002-do-not-change-library-prefix-and-suffix.patch
}

build() {
  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 \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config[@]} \
    -DREQUIRE_CRYPTO_GNUTLS=ON \
    -DREQUIRE_CRYPTO_OPENSSL=OFF \
    -DUSE_IMPLICIT_CRYPTO=OFF \
    -DBUILD_STATIC_LIBS=OFF \
    -DBUILD_DOC=OFF \
    -DINSTALL_EXAMPLES=OFF \
    ../${_realname}-${pkgver}

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

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  PATH=$PWD/libqpdf/build:$PATH ${MINGW_PREFIX}/bin/ctest || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
