# 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=2
_commit=51f96581f878c15ac1fbf1bb62fb9cc3a4a53917
pkgdesc="Convert HTML to PDF using QtWebkit (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url="https://wkhtmltopdf.org/"
msys2_repository_url="https://github.com/wkhtmltopdf/wkhtmltopdf"
msys2_references=(
  "cpe: cpe:/a:wkhtmltopdf:wkhtmltopdf"
)
license=('spdx:LGPL-3.0-only')
makedepends=("git" "${MINGW_PACKAGE_PREFIX}-cc")
depends=("${MINGW_PACKAGE_PREFIX}-qt5-svg"
         "${MINGW_PACKAGE_PREFIX}-qt5-xmlpatterns"
         "${MINGW_PACKAGE_PREFIX}-qtwebkit")
source=(git+https://github.com/${_realname}/${_realname}.git#commit=$_commit)
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() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  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-${MSYSTEM}"

  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"
}
