# Maintainer: Diego Sogari <diego.sogari@gmail.com>

_realname=wkhtmltopdf
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
_basever=0.13
pkgver=0.13.r1049.51f9658
pkgrel=1
pkgdesc="Convert HTML to PDF using QtWebkit (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://wkhtmltopdf.org/"
license=('LGPL')
makedepends=("git" "${MINGW_PACKAGE_PREFIX}-gcc")
depends=("${MINGW_PACKAGE_PREFIX}-qt5"
         "${MINGW_PACKAGE_PREFIX}-qtwebkit")
source=("${_realname}::git+https://github.com/${_realname}/${_realname}.git#branch=merge-${_basever}")
sha256sums=('SKIP')

pkgver() {
  cd "${srcdir}/${_realname}"
  printf "%s.r%s.%s" "${_basever}" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "${srcdir}/${_realname}"
  sed -e '/QMAKE_LFLAGS/d' -i "./common.pri"
  sed -e 's|CONFIG += dll|& skip_target_version_ext|g' -i "./src/lib/lib.pro"
}

build() {
  rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  ln -fs `which windres` ${MINGW_CHOST}-windres
  PATH=`pwd`:$PATH
  export WKHTMLTOX_VERSION=${_basever}

  ${MINGW_PREFIX}/bin/qmake -set CROSS_COMPILE "${MINGW_CHOST}-"
  ${MINGW_PREFIX}/bin/qmake -spec win32-g++ "../${_realname}/${_realname}.pro"
  make release
}

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

  install -D ../${_realname}/include/wkhtmltox/dllbegin.inc "${pkgdir}${MINGW_PREFIX}/include/wkhtmltox/dllbegin.inc"
  install -D ../${_realname}/include/wkhtmltox/dllend.inc "${pkgdir}${MINGW_PREFIX}/include/wkhtmltox/dllend.inc"
  install -D ../${_realname}/include/wkhtmltox/image.h "${pkgdir}${MINGW_PREFIX}/include/wkhtmltox/image.h"
  install -D ../${_realname}/include/wkhtmltox/pdf.h "${pkgdir}${MINGW_PREFIX}/include/wkhtmltox/pdf.h"

  install -d "${pkgdir}${MINGW_PREFIX}/bin"
  install "./bin/"*.dll "${pkgdir}${MINGW_PREFIX}/bin"
  install "./bin/"*.exe "${pkgdir}${MINGW_PREFIX}/bin"

  install -d "${pkgdir}${MINGW_PREFIX}/lib"
  install "./bin/"*.a "${pkgdir}${MINGW_PREFIX}/lib"
}
