# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=qt6-pdf
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
_qtver=6.10.1
pkgver=${_qtver/-/}
pkgrel=1
pkgdesc='Qt6 PDF module (mingw-w64)'
arch=(any)
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://www.qt.io'
msys2_repository_url="https://code.qt.io/cgit/qt/qtwebengine.git"
msys2_references=(
  'archlinux: qt6-webengine'
  "cpe: cpe:/a:qt:qt"
)
license=('spdx:LGPL-3.0-only WITH Qt-GPL-exception-1.0 AND BSD-3-Clause AND GFDL-1.3-no-invariants-only AND GPL-2.0-only AND GPL-3.0-only')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-qt6-base"
             "${MINGW_PACKAGE_PREFIX}-qt6-declarative"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools"
             "rsync")
_pkgfn="qtwebengine-everywhere-src-${_qtver}"
source=("https://download.qt.io/official_releases/qt/${pkgver%.*}/${_qtver}/submodules/${_pkgfn}.tar.xz")
sha256sums=('77b5ea6186a0429a6b8e656faedd5cd3e8019d33856ee59637698ab578ead1e3')

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

prepare() {
  cd ${_pkgfn}

  sed -i 's|is_win \&\& !is_msvc|is_win \&\& !is_mingw \&\& is_clang|g' \
    src/3rdparty/chromium/chrome/updater/tools/BUILD.gn
}

build() {
  PYTHON3_PATH=${MINGW_PREFIX}/bin \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  cmake -Wno-dev \
    -GNinja \
    -DCMAKE_BUILD_TYPE=RelWithDebInfo \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_MESSAGE_LOG_LEVEL=STATUS \
    -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DQT_FEATURE_qtwebengine_build=OFF \
    -DQT_FEATURE_system_png=OFF \
    -S ${_pkgfn} \
    -B build-${MSYSTEM}

  cmake --build build-${MSYSTEM}
}

package_qt6-pdf() {
  depends=("${MINGW_PACKAGE_PREFIX}-qt6-base"
           "${MINGW_PACKAGE_PREFIX}-qt6-declarative")
  groups=("${MINGW_PACKAGE_PREFIX}-qt6")

  DESTDIR="${pkgdir}" cmake --install build-${MSYSTEM}

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

  # Some cleanups (conflicts with qt6-webengine)
  rm -r "$pkgdir${MINGW_PREFIX}"/lib/cmake/Qt6
  rm -r "$pkgdir${MINGW_PREFIX}"/share/qt6/bin

  install -d "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
  install -Dm644 ${_pkgfn}/LICENSES/* -t "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
}

package_qt6-pdf-debug() {
  depends=("${MINGW_PACKAGE_PREFIX}-qt6-base-debug"
           "${MINGW_PACKAGE_PREFIX}-qt6-declarative-debug"
           "${MINGW_PACKAGE_PREFIX}-${_realname}")
  groups=("${MINGW_PACKAGE_PREFIX}-qt6-debug")
  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;
