# Maintainer: Johannes Schindelin <johannes.schindelin@gmx.de>

_realname=xpdf
pkgbase="mingw-w64-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.03
pkgrel=1
pkgdesc="Utilities for PDF files (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
license=('GPL2+')
url="https://www.xpdfreader.com/"
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
depends=("${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpaper"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-qt5"
         "${MINGW_PACKAGE_PREFIX}-zlib")
options=('strip')
source=(https://dl.xpdfreader.com/xpdf-${pkgver}.tar.gz{,.sig}
        001-cmake.patch
        002-xpdf-4.00-64bit.patch
        005-xpdf-4.00-permissions.patch
        006-hack-argument-list-under-win.patch)
sha256sums=('0fe4274374c330feaadcebb7bd7700cb91203e153b26aa95952f02bf130be846'
            'SKIP'
            'ea32dc0cd4ef9a23e4452e550a84c3880d022cd2d50c9c4d2c1deafbe1425c1f'
            '75f5d7ae4bb83d2c380dfc9f65bad78574ac3f038fdd6be73c29fc9ea817e715'
            '22e0afda66f036cea8643252762e1391ed078102dd5f76f8cf862976b4b37089'
            'c2af0cd7fde36e7ba3f3935e3fa919b65ac157fd13bcca46338b3a00d5b74673')
validpgpkeys=('A56006CA75CF8B13FA2F120DF4825F5397271342')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-cmake.patch
  patch -p1 -i ${srcdir}/002-xpdf-4.00-64bit.patch
  patch -p1 -i ${srcdir}/005-xpdf-4.00-permissions.patch
  patch -p1 -i ${srcdir}/006-hack-argument-list-under-win.patch
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DMULTITHREADED=ON \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_VERBOSE_MAKEFILE=OFF \
    ../${_realname}-${pkgver}

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

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

  cd ${srcdir}/${_realname}-${pkgver}
  install -Dm 644 xpdf-qt/xpdf-icon.svg "${pkgdir}${MINGW_PREFIX}/share/pixmaps/xpdf.svg"
  install -Dm 644 README CHANGES -t "${pkgdir}${MINGW_PREFIX}/share/doc/${pkgname}"

  # remove stuff provided by poppler
  for tool in pdfdetach pdffonts pdfimages pdfinfo pdftoppm pdftops pdftotext pdftohtml; do
    rm "${pkgdir}${MINGW_PREFIX}/bin/${tool}" "${pkgdir}${MINGW_PREFIX}/share/man/man1/${tool}.1"
  done

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING3 ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING3
}
