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

_realname=pdf2djvu
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.9.19
pkgrel=17
pkgdesc="Creates DjVu files from PDF files (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url="https://jwilk.net/software/pdf2djvu"
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-djvulibre"
         "${MINGW_PACKAGE_PREFIX}-exiv2"
         "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-graphicsmagick"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-poppler")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools"
             "autoconf-archive")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python2-nose")
source=(https://github.com/jwilk/pdf2djvu/releases/download/${pkgver}/pdf2djvu-${pkgver}.tar.xz{,.asc}
        "001-cxx17-for-poppler.patch")
sha256sums=('eb45a480131594079f7fe84df30e4a5d0686f7a8049dc7084eebe22acc37aa9a'
            'SKIP'
            '4c50966b0fc05d78f6ec78b20df587fb5e1861f3ff326ee66c8baf10ec3cb39f')
validpgpkeys=('CDB5A1243ACDB63009AD07212D4EB3A6015475F5') # Jakub Wilk <jwilk@jwilk.net>
noextract=(pdf2djvu-${pkgver}.tar.xz)

prepare() {
  [[ -d ${_realname}-${pkgver} ]] && rm -rf ${_realname}-${pkgver}
  tar -xf pdf2djvu-${pkgver}.tar.xz || true

  cp "${srcdir}"/${_realname}-${pkgver}/tests/test-antialias-off.tex \
     "${srcdir}"/${_realname}-${pkgver}/tests/test-antialias-on.tex
  cd "${srcdir}"/${_realname}-${pkgver}

  patch -p1 -i "${srcdir}/001-cxx17-for-poppler.patch"

  autoreconf -fvi
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  cp -rf ${_realname}-${pkgver} build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  CXXFLAGS+=" -Wno-ignored-attributes -std=c++20 -DWIN32" \
  ./configure \
      --prefix=${MINGW_PREFIX} \
      --with-libiconv-prefix=${MINGW_PREFIX}

  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install

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