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

_realname=pdf2djvu
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.9.17.1
pkgrel=2
pkgdesc="Creates DjVu files from PDF files"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://jwilk.net/software/pdf2djvu"
license=('GPL2')
depends=("${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-djvulibre"
         "${MINGW_PACKAGE_PREFIX}-exiv2"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-graphicsmagick"
         "${MINGW_PACKAGE_PREFIX}-libiconv")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python2-nose")
source=(https://github.com/jwilk/pdf2djvu/releases/download/${pkgver}/pdf2djvu-${pkgver}.tar.xz{,.asc})
sha256sums=('5bbbb7bdc7858a1eeac6ff270e5a323390f2abb6bd3b0b2ae08c1965272226a3'
            'SKIP')
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}

  # https://github.com/jwilk/pdf2djvu/issues/144
  sed -i 's/poppler-splash/poppler/' configure
}

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

  ./configure \
      --prefix=${MINGW_PREFIX} \
      --build=${MINGW_CHOST} \
      --host=${MINGW_CHOST} \
      --target=${MINGW_CHOST} \
      --with-libiconv-prefix=${MINGW_PREFIX}
  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install
  
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/doc/COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
