# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Contributor: Andrea Scarpino <andrea@archlinux.org>

pkgname=qt5-webengine
_basever=5.15.11
pkgver=5.15.15
pkgrel=4
arch=(x86_64 powerpc64le) #FIXME: powerpc64)
url='https://www.qt.io'
license=('LGPL3' 'LGPL2.1' 'BSD')
pkgdesc='Provides support for web applications using the Chromium browser project'
depends=('qt5-webchannel' 'qt5-location' 'libxcomposite' 'libxrandr' 'pciutils' 'libxss' 'libxkbfile' 
         'libevent' 'snappy' 'nss' 'libxslt' 'minizip' 'ffmpeg' 're2' 'libvpx' 'libxtst' 'ttf-font')
makedepends=('git' 'python' 'gperf' 'jsoncpp' 'ninja' 'qt5-tools' 'poppler' 'pipewire' 'nodejs' 'clang' 'gn')
optdepends=('pipewire: WebRTC desktop sharing under Wayland')
groups=('qt' 'qt5')
_pkgfqn=${pkgname/5-/}
source=(git+https://code.qt.io/qt/qtwebengine.git#tag=v${pkgver}-lts
        git+https://code.qt.io/qt/qtwebengine-chromium.git
        qt5-webengine-python3.patch
        qt5-webengine-chromium-python3.patch
        qt5-webengine-ffmpeg5.patch
        qt5-webengine-pipewire-0.3.patch
        qt5-webengine-libxml-2.12.patch
        qt5-webengine-icu-74.patch
        https://repo.archlinuxpower.org/distfiles/qtwebengine/archpower-qtwebengine-${pkgver}-patchset.tar.zst{,.asc})
validpgpkeys=('07F27AE287075F9A18E79E0DF4D5724CC08E7B94')
sha256sums=('SKIP'
            'SKIP'
            '0ad5d1660886f7bbf5108b071bf5d7bbbabf1cd1258ce9d4587a01dec4a1aa89'
            'd8beb3d65a1aaf927285e6f055a9d1facd0f9c3fd851f91ba568389fb3137399'
            'c50d3019626183e753c53a997dc8a55938847543aa3178d4c51f377be741c693'
            '5e3a3c4711d964d5152a04059a2b5c1d14bb13dd29bce370120f60e85b476b6f'
            'bfae9e773edfd0ddbc617777fdd4c0609cba2b048be7afe40f97768e4eb6117e'
            '547e092f6a20ebd15e486b31111145bc94b8709ec230da89c591963001378845'
            '1711dd3a268e5c6784253a9c3f12adfe43d0700cafe26572c455f3ba0b45a164'
            'SKIP')

prepare() {
  mkdir -p build

  cd ${_pkgfqn}
  git submodule init
  git submodule set-url src/3rdparty "$srcdir"/qtwebengine-chromium
  git submodule set-branch --branch 87-based src/3rdparty
  git -c protocol.file.allow=always submodule update

set -x
  #patch -p1 -i "$srcdir"/qt5-webengine-python3.patch # Fix build with Python 3
  #patch -p1 -d src/3rdparty -i "$srcdir"/qt5-webengine-chromium-python3.patch

  patch -d src/3rdparty -Np1 -i ${srcdir}/qt5-webengine-ffmpeg5.patch
  #patch -p1 -d src/3rdparty -i "$srcdir"/qt5-webengine-pipewire-0.3.patch # Port to pipewire 0.3
  patch -p1 -d src/3rdparty/chromium -i "$srcdir"/qt5-webengine-libxml-2.12.patch # Fix build with libxml 2.12
  patch -p1 -d src/3rdparty/chromium -i "$srcdir"/qt5-webengine-icu-74.patch # Fix build with ICU 74 - patch from Alpine

  ${srcdir}/archpower-qtwebengine-${pkgver}-patchset/apply.sh
set +x

  pushd ${srcdir}/qtwebengine/src/3rdparty/chromium/third_party/libvpx
  mkdir -p source/config/linux/ppc64
  mkdir -p source/libvpx/test
  touch source/libvpx/test/test.mk
  ./generate_gni.sh
  popd
}

build() {
  cd build

  export CXXFLAGS+=' -std=c++14'

  qmake ../${_pkgfqn} CONFIG+=force_debug_info -- \
    -proprietary-codecs \
    -system-ffmpeg \
    -webp \
    -spellchecker \
    -webengine-icu \
    -webengine-kerberos \
    -webengine-webrtc-pipewire \
    -no-build-qtpdf \
    -printing-and-pdf \
    -system-opus \
    -system-webp

  make
}

package() {
  cd build
  make INSTALL_ROOT="$pkgdir" install

  # Drop QMAKE_PRL_BUILD_DIR because reference the build dir
  find "$pkgdir/usr/lib" -type f -name '*.prl' \
    -exec sed -i -e '/^QMAKE_PRL_BUILD_DIR/d' {} \;

  install -Dm644 "$srcdir"/${_pkgfqn}/src/3rdparty/chromium/LICENSE "$pkgdir"/usr/share/licenses/${pkgname}/LICENSE.chromium

  # Fix cmake dependency versions
  sed -e "s|$pkgver\ |$_basever |" -i "$pkgdir"/usr/lib/cmake/*/*Config.cmake
}
