# Description: The Qt WebEngine module provides a web browser engine
# URL: https://www.qt.io/
# Maintainer: Tim Biermann, tbier at posteo dot de
# Depends on: ffmpeg libvpx nodejs nss qt5 xorg-libxkbfile xorg-libxscrnsaver xorg-libxtst
# Optional: glib jsoncpp krb5 krb5 lcms2 libevent libsrtp libxml2 minizip opus pipewire pulseaudio re2 snappy

name=qtwebengine
version=5.15.17
release=1

# https://github.com/qt/qtwebengine/commits/5.15
_qtwebengine_commit=79a4e92cb0c9fc946f81e13b392a04be51d95991
# from $_qtwebengine_commit/src/3rdparty
_chromium_commit=ce00f9b5aa761866b24d6460e10aacb671c92cf0

source=(https://github.com/qt/qtwebengine/archive/$_qtwebengine_commit/$name-$version.tar.gz
  https://github.com/qt/qtwebengine-chromium/archive/$_chromium_commit/$name-chromium-$_chromium_commit.tar.gz
  qtwebengine-build_fixes-2.patch
  qtwebengine-5.15.2-disable-fatal-warnings.patch
  qtwebengine-5.15.2_p20210224-chromium-87-v8-icu68.patch
  qtwebengine-5.15.2_p20210521-clang-libc++.patch
  qtwebengine-5.15.2_p20211015-pdfium-system-lcms2.patch
  qtwebengine-5.15.3_p20220406-ffmpeg5.patch
  qtwebengine-5.15.13-icu_73-1.patch
  qt5-webengine-pipewire-0.3.patch
  qtwebengine-5.15.15-c++17.patch
  qtwebengine-system-absl-compat.patch
  programm_manager.patch
  python.patch)

build() {
  cd qtwebengine-$_qtwebengine_commit

  rm -rf src/3rdparty
  mv $SRC/qtwebengine-chromium-$_chromium_commit src/3rdparty
  mkdir -pv .git src/3rdparty/chromium/.git

  # https://www.linuxfromscratch.org/blfs/view/svn/x/qtwebengine.html
  patch -p1 -i $SRC/qtwebengine-build_fixes-2.patch
  patch -p1 -i $SRC/qtwebengine-5.15.2-disable-fatal-warnings.patch
  patch -p1 -i $SRC/qtwebengine-5.15.2_p20210224-chromium-87-v8-icu68.patch
  patch -p1 -i $SRC/qtwebengine-5.15.2_p20210521-clang-libc++.patch
  patch -p1 -i $SRC/qtwebengine-5.15.2_p20211015-pdfium-system-lcms2.patch
  patch -p1 -i $SRC/qtwebengine-5.15.3_p20220406-ffmpeg5.patch
  patch -p1 -i $SRC/qtwebengine-5.15.13-icu_73-1.patch
  patch -p1 -d src/3rdparty -i $SRC/qt5-webengine-pipewire-0.3.patch
  patch -p1 -i $SRC/python.patch

  if prt-get isinst re2; then
    patch -p1 -i $SRC/qtwebengine-system-absl-compat.patch
    patch -p1 -i $SRC/programm_manager.patch
    cp -f /usr/include/absl/base/options.h \
      src/3rdparty/chromium/third_party/abseil-cpp/absl/base/options.h
    sed -i -e 's,#define ABSL_OPTION_USE_STD_OPTIONAL 1,#define ABSL_OPTION_USE_STD_OPTIONAL 0,' src/3rdparty/chromium/third_party/abseil-cpp/absl/base/options.h
    patch -p1 -i $SRC/qtwebengine-5.15.15-c++17.patch
    export CXXFLAGS+=' -std=gnu++17'
    find . -name "*.pro" -o -name "*.pri" |while read r; do
      echo 'QMAKE_CXXFLAGS_GNUCXX14 = -std=gnu++17' >>$r
    done
  fi

  sed -e '/^MODULE_VERSION/s/5.*/5.15.2/' -i .qmake.conf
  find -type f -name "*.pr[io]" | \
    xargs sed -i -e 's|INCLUDEPATH += |&$$QTWEBENGINE_ROOT/include |'

  prt-get isinst pulseaudio && sed -e '/link_pulseaudio/s/false/true/' \
    -i src/3rdparty/chromium/media/media_options.gni

  # Ninja, by default, sets the number of jobs to the number of availalble threads
  test -n "$JOBS" && export NINJAFLAGS+=" -j ${JOBS-1}"

  # since 5.15.14 unsupported
  #prt-get isinst clang && export CC=clang CXX=clang++ QMAKE_XSPEC=linux-clang
  #prt-get isinst lld && export AR=llvm-ar NM=llvm-nm RANLIB=llvm-ranlib LDFLAGS+=' -fuse-ld=lld'

  prt-get isinst ccache && export \
    QMAKE_CC="ccache ${CC:-gcc}" \
    QMAKE_LINK_C="ccache ${CC:-gcc}" \
    QMAKE_LINK_C_SHLIB="ccache ${CC:-gcc}" \
    QMAKE_CXX="ccache ${CXX:-g++}" \
    QMAKE_LINK="ccache ${CXX:-g++}" \
    QMAKE_LINK_SHLIB="ccache ${CXX:-g++}" \
    QMAKESPEC=/usr/lib/qt5/mkspecs/${QMAKE_XSPEC:-linux-g++}
    PATH="$(echo ${PATH} | awk -v RS=: -v ORS=: '/ccache/ {next} {print}' | sed 's/:*$//')"
  sed -i -e 's|"-Wno-unknown-attributes",|"-Wno-enum-constexpr-conversion",\n      &|' \
    src/3rdparty/chromium/build/config/compiler/BUILD.gn

  install -d build
  cd build

  prt-get isinst pulseaudio && PKGMK_QTWEBENGINE+=' -pulseaudio' || PKGMK_QTWEBENGINE+=' -no-pulseaudio'
  prt-get isinst pipewire && PKGMK_QTWEBENGINE+=' -webengine-webrtc-pipewire'
  prt-get isinst icu && PKGMK_QTWEBENGINE+=' -webengine-icu' || PKGMK_QTWEBENGINE+=' -no-webengine-icu'

  qmake-qt5 .. -- \
    $PKGMK_QTWEBENGINE \
    -proprietary-codecs \
    -system-ffmpeg \
    -system-opus \
    -system-webp \
    -no-feature-webengine-embedded-build

  make
  make INSTALL_ROOT=$PKG install

  find "$PKG/usr/lib" -type f -name '*.prl' \
    -exec sed -i -e '/^QMAKE_PRL_BUILD_DIR/d' {} \;
}
