#!/bin/bash

# Slackware build script for qt6

# Copyright 2022-2023 Steven Voges <Oregon, USA>
# Copyright 2025 Ruoh-Shoei LIN
# 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.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=qt6
SRCNAM=qt-everywhere-src
VERSION=${VERSION:-6.8.3}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/home/tmp}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION.tar.xz
cd $SRCNAM-$VERSION
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

if [ "$QTWEBENGINE" = "OFF" ]; then
   QTWEBENGINE="-skip qtwebengine"
else
   QTWEBENGINE="-webengine-proprietary-codecs -webengine-printing-and-pdf"
fi

if [ "$QT3D" = "OFF" ]; then
   QT3D="-skip qt3d -skip qtquick3dphysics"
fi

if [ "${QTGRPC:-OFF}" = "OFF" ]; then
   QTGRPC="-skip qtgrpc"
fi

pushd qtbase
patch -Np1 -i $CWD/CVE-2025-3512-qtbase-6.8.diff
patch -Np1 -i $CWD/CVE-2025-4211-qtbase-6.8.diff
patch -Np1 -i $CWD/CVE-2025-5455-qtbase-6.8.patch

sed -i '21,36d' $CWD/CVE-2025-5992-qtbase-6.8.patch
patch -Np1 -i $CWD/CVE-2025-5992-qtbase-6.8.patch

sed -i 's/QPCCertContextPointer certificateContext;/const CERT_CONTEXT *certificateContext = nullptr;/g' $CWD/CVE-2025-6338-qtbase-6.8.patch
patch -Np1 -i $CWD/CVE-2025-6338-qtbase-6.8.patch
popd

pushd qtsvg
patch -Np1 -i $CWD/CVE-2025-10728-qtsvg-6.8.diff
patch -Np1 -i $CWD/CVE-2025-10729-qtsvg-6.8.diff
popd

pushd qtimageformats
patch -Np1 -i $CWD/CVE-2025-5683-qtimageformats-6.8.patch
popd

pushd qtdeclarative
patch -Np1 -i $CWD/CVE-2025-12385-qtdeclarative-6.8-0001.diff
patch -Np1 -i $CWD/CVE-2025-12385-qtdeclarative-6.8-0002.diff
popd

export QT6PREFIX="/usr"
export CFLAGS="$SLKCFLAGS"
export CXXFLAGS="$SLKCFLAGS"
export OPENSOURCE_CXXFLAGS="$SLKCFLAGS"
export QTDIR="${TMP}/$SRCNAM-$VERSION"

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"

if [ "${LLVM_OPT:-ON}" = "ON" ]; then
    export PATH="/opt/llvm-opt/bin:$PATH"
    if [ -z "$LD_LIBRARY_PATH" ]; then
	export LD_LIBRARY_PATH="/opt/llvm-opt/lib$LIBDIRSUFFIX"
    else
	export LD_LIBRARY_PATH="/opt/llvm-opt/lib$LIBDIRSUFFIX:$LD_LIBRARY_PATH"
    fi
fi

./configure \
  -prefix $QT6PREFIX \
  -sysconfdir /etc/xdg \
  -libdir $QT6PREFIX/lib${LIBDIRSUFFIX} \
  -release \
  -dbus-linked \
  -nomake examples \
  -no-rpath \
  -no-sbom \
  -syslog \
  ${QT3D} \
  ${QTGRPC} \
  ${QTWEBENGINE} \
  -- -DCMAKE_MESSAGE_LOG_LEVEL=STATUS \
  -DQT_INSTALL_CONFIG_INFO_FILES=ON \
  -DINSTALL_GN=OFF \
  -DINSTALL_BINDIR=$QT6PREFIX/lib${LIBDIRSUFFIX}/$PRGNAM/bin \
  -DINSTALL_PUBLICBINDIR=$QT6PREFIX/bin \
  -DINSTALL_LIBDIR=$QT6PREFIX/lib${LIBDIRSUFFIX} \
  -DINSTALL_LIBEXECDIR=$QT6PREFIX/lib${LIBDIRSUFFIX}/$PRGNAM/bin \
  -DINSTALL_ARCHDATADIR=$QT6PREFIX/lib${LIBDIRSUFFIX}/$PRGNAM \
  -DINSTALL_DATADIR=$QT6PREFIX/share/$PRGNAM \
  -DINSTALL_INCLUDEDIR=$QT6PREFIX/include/$PRGNAM \
  -DINSTALL_MKSPECSDIR=lib${LIBDIRSUFFIX}/$PRGNAM/mkspecs \
  -DINSTALL_DOCDIR=$QT6PREFIX/doc/$PRGNAM-${VERSION} \
  -DINSTALL_EXAMPLESDIR=$QT6PREFIX/doc/$PRGNAM-${PKGVER}/examples \
  -Wno-dev

ninja || ninja -j1 -v
DESTDIR=$PKG ninja install

pushd "$PKG/$QT6PREFIX/lib${LIBDIRSUFFIX}/$PRGNAM/bin"
for i in \
    assistant canbusutil cooker designer \
    linguist lrelease lupdate pixeltool \
    qdbus qdbusviewer qdistancefieldgenerator qdoc \
    qml qmleasing qmlls qmlpreview qmlscene \
    qqem qtplugininfo; do
    ln -sfv "${i}" "${i}${VERSION%.*.*}"
done
popd

find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

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

install -Dm644 qttools/src/assistant/assistant/images/assistant-128.png \
  $PKG/usr/share/icons/hicolor/128x128/apps/$PRGNAM-assistant.png
install -Dm644 qttools/src/designer/src/designer/images/designer.png \
  $PKG/usr/share/icons/hicolor/128x128/apps/$PRGNAM-designer.png
install -Dm644 qttools/src/qdbus/qdbusviewer/images/qdbusviewer-128.png \
  $PKG/usr/share/icons/hicolor/128x128/apps/$PRGNAM-qdbusviewer.png
install -Dm644 qttools/src/linguist/linguist/images/icons/linguist-128-32.png \
  $PKG/usr/share/icons/hicolor/128x128/apps/$PRGNAM-linguist.png

# And the .desktop files too:
mkdir -p $PKG/usr/share/applications
cat <<EOF > $PKG/usr/share/applications/$PRGNAM-designer.desktop
[Desktop Entry]
Name=Qt6 Designer
GenericName=Interface Designer
Comment=Design GUIs for Qt6 applications
Exec=$QT6PREFIX/bin/designer
Icon=qt6-designer
MimeType=application/x-designer;
Terminal=false
Encoding=UTF-8
Type=Application
Categories=Qt;Development;
EOF
cat <<EOF > $PKG/usr/share/applications/$PRGNAM-assistant.desktop
[Desktop Entry]
Name=Qt6 Assistant
Comment=Shows Qt6 documentation and examples
Exec=$QT6PREFIX/bin/assistant
Icon=qt6-assistant
Terminal=false
Encoding=UTF-8
Type=Application
Categories=Qt;Development;Documentation;
EOF
cat <<EOF > $PKG/usr/share/applications/$PRGNAM-linguist.desktop
[Desktop Entry]
Name=Qt6 Linguist
Comment=Add translations to Qt6 applications
Exec=$QT6PREFIX/bin/linguist
Icon=qt6-linguist
MimeType=text/vnd.trolltech.linguist;application/x-linguist;
Terminal=false
Encoding=UTF-8
Type=Application
Categories=Qt;Development;
EOF
cat <<EOF > $PKG/usr/share/applications/$PRGNAM-qdbusviewer.desktop
[Desktop Entry]
Name=Qt6 QDbusViewer
GenericName=Qt6 D-Bus Debugger
Comment=Debug D-Bus applications
Exec=$QT6PREFIX/bin/qdbusviewer
Icon=qt6-qdbusviewer
Terminal=false
Type=Application
Categories=Qt;Development;Debugger;
EOF

cat > $CWD/$PRGNAM.csh << "EOF"
# Begin /etc/profile.d/qt6.csh

if ( ! $?QT6DIR ) then
    if ( -d /usr/libLIBDIRSUFFIX/qt6 ) then
	setenv QT6DIR /usr/libLIBDIRSUFFIX/qt6
    else
	foreach qtd ( /usr/libLIBDIRSUFFIX/qt6-* )
	    if ( -d $qtd ) then
		setenv QT6DIR $qtd
	    endif
	end
    endif
endif
set path = ( $path $QT6DIR/bin )

# End /etc/profile.d/qt6.csh
EOF

cat > $CWD/$PRGNAM.sh << "EOF"
# Begin /etc/profile.d/qt6.sh

if [ -d /usr/libLIBDIRSUFFIX/qt6 ]; then
  QT6DIR=/usr/libLIBDIRSUFFIX/qt6
else
  # Find the newest Qt directory and set $QT6DIR to that:
  for qtd in /usr/libLIBDIRSUFFIX/qt6-* ; do
    if [ -d $qtd ]; then
      QT6DIR=$qtd
    fi
  done
fi
PATH="$PATH:$QT6DIR/bin"
export QT6DIR

# End /etc/profile.d/qt6.sh
EOF

sed -i "/LIBDIRSUFFIX/ s/LIBDIRSUFFIX/${LIBDIRSUFFIX}/g" \
    $CWD/$PRGNAM.sh $CWD/$PRGNAM.csh
install -Dm755 $CWD/$PRGNAM.sh $CWD/$PRGNAM.csh -t $PKG/etc/profile.d

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
   LICENSES README.md \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
