if [[ "${CROSS:-$ARCH}" != "amd64" && "${CROSS:-$ARCH}" != "arm64" ]]; then
    abinfo "FIXME: Skipping QtWebEngine on loongson3, ppc64el and riscv64 ..."
    QTWEOPT="-skip qtwebengine"
else
    QTWEOPT=" \
        -system-webengine-ffmpeg \
        -webengine-proprietary-codecs \
        -webengine-webp \
        -webengine-kerberos \
        -webengine-webrtc-pipewire"
fi

if [[ "${CROSS:-$ARCH}" = "amd64" ]]; then
    abinfo "Enabling use of SSE2 instructions on AMD64 ..."
    SSEOPT="-sse2"
fi

if [[ "${CROSS:-$ARCH}" = "armv4" || \
      "${CROSS:-$ARCH}" = "armv6hf" || \
      "${CROSS:-$ARCH}" = "armv7hf" ]]; then
    abinfo "Using GLES for ARM* ..."
    OPENGL="-opengl es2"
fi

abinfo "Configuring Qt 5 ..."
"$SRCDIR"/configure \
        -release \
        -force-debug-info \
        -no-strip \
        -verbose \
        -confirm-license \
        -opensource \
        -prefix /usr \
        -bindir /usr/bin \
        -docdir /usr/share/doc/qt-5 \
        -headerdir /usr/include/qt5 \
        -archdatadir /usr/lib/qt5 \
        -datadir /usr/share/qt5 \
        -sysconfdir /etc/xdg \
        -examplesdir /usr/share/doc/qt-5/examples \
        -shared \
        -nomake examples \
        -nomake tests \
        -no-rpath \
        -no-reduce-relocations \
        -accessibility \
        -fontconfig \
        -system-harfbuzz \
        -system-libjpeg \
        -system-libpng \
        -system-sqlite \
        -system-pcre \
        -system-zlib \
        -plugin-sql-psql \
        -plugin-sql-sqlite \
        -plugin-sql-odbc \
        -openssl-linked \
        -dbus-linked \
        -optimized-qmake \
        -glib \
        -icu \
        -journald \
        -libinput \
        -libproxy \
        -feature-vulkan \
        $QTWEOPT $SSEOPT $OPENGL $GOLD

abinfo "Building Qt 5 ..."
make V=1 VERBOSE=1

abinfo "Installing Qt 5 ..."
# FIXME: Paralel install breaks installations of components in this all-in-one
# package when host has too many threads, so you may be able to set a value
# larger than 1, but no without `-j` flag.
make install V=1 VERBOSE=1 INSTALL_ROOT="$PKGDIR" -j1

abinfo "Configuring QtWebKit ..."
QTWKSRCDIR="$SRCDIR"/qtwebkit
export Qt5_DIR="$PKGDIR"/usr

if [[ "${CROSS:-$ARCH}" != "amd64" && "${CROSS:-$ARCH}" != arm* ]]; then
    abinfo "FIXME: Disabling JIT on !x86, !arm ..."
    QTWKAFTER+=" -DENABLE_JIT=OFF"
else
    QTWKAFTER+=" -DENABLE_JIT=ON"
fi

abinfo "Creating directory for shadow build ..."
cd "$QTWKSRCDIR"
for i in "$SRCDIR"/autobuild/patches/*.patch.deferred; do
    abinfo "Applying deferred patch $i ..."
    patch -Np1 -i "$i"
done
if ab_match_arch riscv64; then
    for i in "$SRCDIR"/autobuild/patches/*.patch.deferred.riscv64; do
        abinfo "Applying deferred patch $i ..."
        patch -Np1 -i "$i"
    done
fi
mkdir -pv "$QTWKSRCDIR"/abbuild
cd "$QTWKSRCDIR"/abbuild
abinfo "Building QtWebKit ..."
(
    cmake "$QTWKSRCDIR" \
        -GNinja \
        ${CMAKE_DEF[@]} ${QTWKAFTER} \
        -DPORT=Qt \
        -DECM_MKSPECS_INSTALL_DIR=/usr/lib/qt5/mkspecs/modules \
        -DENABLE_TOOLS=OFF
    abinfo "Appending -latomic linkage to build.ninja ..."
    sed -e '/LINK_LIBRARIES = / s/$/ -latomic/' \
        -i "$QTWKSRCDIR"/abbuild/build.ninja
    abinfo "Building and installing QtWebKit binaries ..."
    DESTDIR="$PKGDIR" ninja install
)
