# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=qt5-pdf
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
pkgver=5.15.18
pkgrel=1
pkgdesc='Qt5 PDF module'
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.qt.io'
license=('spdx:BSD-3-Clause AND GFDL-1.3-no-invariants-only AND GPL-2.0-only AND GPL-3.0-only AND LGPL-3.0-only' 'custom')
depends=("${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-gperf"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-nodejs"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-html5lib"
             "${MINGW_PACKAGE_PREFIX}-qt5-base"
             "${MINGW_PACKAGE_PREFIX}-qt5-declarative"
             "${MINGW_PACKAGE_PREFIX}-qt5-tools"
             "rsync")
_pkgfn="qtwebengine-everywhere-opensource-src-${pkgver}"
source=("https://download.qt.io/archive/qt/${pkgver%.*}/${pkgver}/submodules/${_pkgfn}.tar.xz"
        0001-qmake-configure-fixes.patch
        0002-chromium-add-mingw-toolchain.patch
        0106-chromium-third-party-pdfium.patch
        0201-gn-mingw-fixes.patch)
sha256sums=('018e7bb549a858a2075869eb1d7e0555974876625e0d5e437a50f6962d9fcadf'
            '3ecb09b542dde54d517983e520c16489b83b8a1ece056f2ad98e038106573dd2'
            'a81dcc8ceadcac77c8769a33dd224f103c1a3173b544db2a1eb40d3a2b257bfd'
            '8513924be7b0efc69a3d125839921d4011cb81ddb7e839d5232032c0915dcf1a'
            '411e853329d9b05567828939af578626487237242a18cb914b4174065b8216fa')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Np1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${_pkgfn/-opensource/}

  apply_patch_with_msg \
    0001-qmake-configure-fixes.patch \
    0002-chromium-add-mingw-toolchain.patch \
    0106-chromium-third-party-pdfium.patch \
    0201-gn-mingw-fixes.patch
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  qmake ../${_pkgfn/-opensource/} \
    CONFIG-='debug_and_release debug_and_release_target' \
    CONFIG+='silent' -- \
    -no-build-qtwebengine-core

  make
}

package_qt5-pdf() {
  depends+=("${MINGW_PACKAGE_PREFIX}-qt5-base"
            "${MINGW_PACKAGE_PREFIX}-qt5-declarative")
  groups=("${MINGW_PACKAGE_PREFIX}-qt5")
  pkgdesc+=' (mingw-w64)'

  cd build-${MSYSTEM}

  # Fix Makefiles
  local PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  find "${srcdir}/build-${MSYSTEM}" -type f \( -name 'Makefile*' \) \
      -exec sed -i -e "s|${PREFIX_WIN:0:2}\$(INSTALL_ROOT|\$(INSTALL_ROOT|g" {} \;
  find "${srcdir}/build-${MSYSTEM}" -type f \( -name 'Makefile*' \) \
      -exec sed -i -e "s|)${PREFIX_WIN:2}|)${MINGW_PREFIX}|g" {} \;

  make INSTALL_ROOT="${pkgdir}" install

  # Separate debug-info files
  rsync -armR --remove-source-files --include="*/" --include="*.debug" --exclude="*" \
    --prune-empty-dirs "${pkgdir}/.${MINGW_PREFIX}" "${srcdir}/${MSYSTEM}-debug/"

  install -d "${pkgdir}${MINGW_PREFIX}/share/licenses/qt5-pdf"
  install -Dm644 ../qtwebengine-everywhere-src-${pkgver}/LICENSE* -t "${pkgdir}${MINGW_PREFIX}/share/licenses/qt5-pdf/"

  # Fix paths in *.pri, *prl and *.pc files:
  #   $(cygpath -m ${MINGW_PREFIX}) -> $(cygpath -m "${pkgdir}"${MINGW_PREFIX})
  local PKGDIR_PREFIX_WIN=$(cygpath -m "${pkgdir}"${MINGW_PREFIX})
  local FAKE_PREFIX_FOR_REPLACE="@@QT_REAL_PREFIX/dir@@"

  find "${pkgdir}${MINGW_PREFIX}/lib" -type f \( -name '*.pri' -o -name '*.prl' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" -type f -name '*.pc' \
      -exec sed -i -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" -type f -name '*.pc' \
      -exec sed -i -e "s|${PKGDIR_PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;
}

package_qt5-pdf-debug() {
  depends+=("${MINGW_PACKAGE_PREFIX}-qt5-base-debug"
            "${MINGW_PACKAGE_PREFIX}-qt5-declarative-debug"
            "${MINGW_PACKAGE_PREFIX}-qt5-pdf")
  groups=("${MINGW_PACKAGE_PREFIX}-qt5-debug")
  pkgdesc+=' (debug) (mingw-w64)'
  options=('!strip')

  cp -rf "${srcdir}/${MSYSTEM}-debug${MINGW_PREFIX}" "${pkgdir}/"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
