#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# qt6-webengine.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for Slackware ARM.
#
# Copyright 2026  Patrick J. Volkerding, Sebeka, Minnesota, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Merged some ideas from Alex Sarmardzic's script for qt4 at SlackBuilds.org
# ^^ Modified by Robby Workman <rworkman@slackware.com> for QT4 & KDE4
#
# Modifications 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2023  Eric Hameleers, Eindhoven, NL
# Modifications for qt 5.2.0 2013 by Michael James, AU
# Modifications for qt 5.x by Eric Hameleers, Eindhoven, NL
# Modifications for qt 6.x 2023, 2024 by Eric Hameleers, Eindhoven, NL

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# Detect whether we're building for /patches:
# This function sets the variable 'SLACKPATCHING'
slack_findpkgstore_is_stablerelease

# Build settings and versions of bundled packages and names of source archives.
# To save on maintenance, and since we carry the same versions as x86 Slackware,
# let's pull details from the x86 trunk:
slack_setvarfromupstream USE_CODECS SYSTEM_FFMPEG PKGSRC PKGVER

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Architecture settings:
case $ARCH in
   aarch64)
           export SLKCONFARGS="-DQT_FEATURE_sse2=OFF"
           export SLKCFLAGS="-O2" # ${SLKCFLAGS}" # use defaults from Slackkit
           export SLKLDFLAGS="-L/usr/lib${LIBDIRSUFFIX}" # -Wl,-z,relro -Wl,--as-needed -Wl,-z,now";;
           # The qtquick3dphysics only compiles on a limited set of architectures:
           DO_PHYSX3D="ON" ;;
       	*) export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar xf $CWD/qtwebengine-everywhere-src-*.tar.!(*sign|*asc|*sig)
cd qtwebengine*/ || failextract
slackhousekeeping

#########################################################################
# Apply patches from Slackware upstream:
#########################################################################

# Note: We omit the 32bit patches here since ARM no longer supports it.
# and they're relevant to x86 anyway.

# Patch header issue that causes the compile to fail with "x11 not defined"
cat $CWD/patches/qtwebengine-6.7.0-displaykey-header.patch | patch -p1 --verbose || exit 1

# CVE-2026-107080:
cat $CWD/patches/CVE-2026-107080.patch | patch -p1 --verbose || exit 1

# Fix compile with icu4c-79.1:
cat $CWD/patches/qtwebengine-6.140.0-icu4c_79-1.patch | patch -p1 --verbose || exit 1

#########################################################################
# Apply patches for ARM/AArch64:
#########################################################################

# Required if 'rpi-userland' package is installed:
#auto_apply_patch $PORTCWD/sources/patches/ftbfs-qt.diff || failpatch

# AArch64: https://src.fedoraproject.org/rpms/qt6-qtwebengine/tree/rawhide
# https://archlinuxarm.org/packages/aarch64/qt6-webengine/files
#     qtwebengine-ffmpeg-first_dts.patch \
for pf in \
     qtwebengine-aarch64-new-stat.patch \
     qtwebengine-fix-arm-build.patch.xz \
  ; do auto_apply_patch $PORTCWD/sources/patches/${pf}* || failpatch
done

# delete all "toolprefix = " lines from build/toolchain/linux/BUILD.gn, as we
# don't cross-compile. Fixes aarch64 FTBFS
# Source: https://src.fedoraproject.org/rpms/qt6-qtwebengine/raw/rawhide/f/qt6-qtwebengine.spec
sed -i -e '/toolprefix = /d' -e 's/\${toolprefix}//g' \
  src/3rdparty/chromium/build/toolchain/linux/BUILD.gn

#for pf in \
#  ; do auto_apply_patch $PORTCWD/sources/patches/$pf || exit 1
#done

############################################################################

# Previously we didn't have a protobuf package in Slackware.
# Now that we do, it's apparently too new to use with Qt6, so let's just
# disable it to get the same features that we previously did.
# Patches would be accepted here. :-)
#PROTOBUF="-DQT_FEATURE_qtprotobufgen=OFF"

# Use our custom compiler and linker flags:
#sed -e "s|^\(QMAKE_CFLAGS_RELEASE.*\)|\1 ${SLKCFLAGS}|" \
#  -i qtbase/mkspecs/common/gcc-base.conf || exit 1
#sed -e "s|^\(QMAKE_LFLAGS_RELEASE.*\)|\1 ${SLKLDFLAGS}|" \
#  -i qtbase/mkspecs/common/g++-unix.conf || exit 1

# As SBOM generation is failing with 6.8.1 (and was newly changed to be ON by
# default, we'll turn it OFF. But we should test in the future to see if it
# starts to work.

export CFLAGS="$SLKCFLAGS"
export CXXFLAGS="$SLKCFLAGS"
export OPENSOURCE_CXXFLAGS="$SLKCFLAGS"
export QTDIR="${TMPBULID}/qtwebengine-everywhere-src-${PKGSRC}"
if [ ! -z "${LD_LIBRARY_PATH}" ]; then
  export LD_LIBRARY_PATH="${QTDIR}/qtbase/lib:${QTDIR}/qttools/lib:${LD_LIBRARY_PATH}"
else
  export LD_LIBRARY_PATH="${QTDIR}/qtbase/lib:${QTDIR}/qttools/lib"
fi
export QT_PLUGIN_PATH="${QTDIR}/qtbase/plugins"

echo "***==============***"
echo "*** Configuring  ***"
echo "***==============***"
cmake -S $(pwd) -B build-${PKGNAM} \
   -G Ninja \
   -Wno-dev \
   -DCMAKE_BUILD_TYPE=Release \
   -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
   -DCMAKE_C_FLAGS_RELEASE:STRING="$SLKCFLAGS" \
   -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
   -DCMAKE_CXX_FLAGS_RELEASE:STRING="$SLKCFLAGS" \
   -DCMAKE_INSTALL_PREFIX=/usr \
   -DINSTALL_BINDIR=/usr/lib${LIBDIRSUFFIX}/qt6/bin \
   -DINSTALL_PUBLICBINDIR=/usr/bin \
   -DINSTALL_LIBDIR=/usr/lib${LIBDIRSUFFIX} \
   -DINSTALL_LIBEXECDIR=/usr/lib${LIBDIRSUFFIX}/qt6/bin \
   -DINSTALL_ARCHDATADIR=/usr/lib${LIBDIRSUFFIX}/qt6 \
   -DINSTALL_DATADIR=/usr/share/qt6 \
   -DINSTALL_INCLUDEDIR=/usr/include/qt6 \
   -DINSTALL_MKSPECSDIR=lib${LIBDIRSUFFIX}/qt6/mkspecs \
   -DINSTALL_DOCDIR=/usr/doc/qt6-${PKGVER} \
   -DINSTALL_EXAMPLESDIR=/usr/doc/qt6-${PKGVER}/examples \
   -DFEATURE_webengine_proprietary_codecs="${USE_CODECS}" \
   -DFEATURE_webengine_system_ffmpeg="${SYSTEM_FFMPEG}" \
   -DFEATURE_webengine_system_icu=ON \
   -DFEATURE_webengine_system_pulseaudio=ON \
   -DFEATURE_webengine_webrtc_pipewire=ON \
   -DFEATURE_quick3dxr_openxr:BOOL=OFF \
   -DFEATURE_system_openxr:BOOL=OFF \
   $PROTOBUF \
   $FIXBUILD \
   -DQT_GENERATE_SBOM=OFF \
   -DQT_BUILD_EXAMPLES=OFF || failconfig

echo "***==============***"
echo "*** Building     ***"
echo "***==============***"
cmake --build build-${PKGNAM} --parallel $NINJAJOBS || failmake

echo "*****=============****"
echo "***** Installing  ****"
echo "*****=============****"
DESTDIR=$PKG cmake --install build-${PKGNAM} || failinstall

# Fix the path in prl files:
find "$PKG/usr/lib${LIBDIRSUFFIX}" -type f -name '*.prl' \
  -exec sed -i -e '/^QMAKE_PRL_BUILD_DIR/d;s/\(QMAKE_PRL_LIBS =\).*/\1/' {} \;

# Add documentation:
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION
cp -fav \
  CHROMIUM_VERSION* CODE_OF_CONDUCT* CONTRIBUTING* LICENSE* README* SECURITY* \
  $PKG/usr/doc/$PKGNAM-$VERSION
#changelogliposuction ChangeLog $PKGNAM $VERSION # Trim down a "ChangeLog" file

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall        # strip all .a archives and all ELFs
#slackstriprpaths    # strip rpaths
slack_delete_lafiles # delete usr/lib{,64}/*.la
slackgzpages -i      # compress man & info pages and delete usr/info/dir
slackslack           # set standard Slackware file/dir permissions and ownerships
slackdesc            # install slack-desc and doinst.sh
slackmp              # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
