# Description: Qt Free Edition, version 5.x.
# URL: http://www.qt.io/
# Maintainer: CRUX System Team, core-ports at crux dot nu
# Depends on: dbus gdk-pixbuf gst-plugins-base libepoxy libmng libwebp libxkbcommon xorg-libxcomposite xorg-libxcursor xorg-libxi xorg-libxinerama xorg-xcb-util-image xorg-xcb-util-keysyms xorg-xcb-util-renderutil xorg-xcb-util-wm
# Optional: assimp bluez cups double-conversion gtk3 ibus icu krb5 libinput libsdl2 mariadb mtdev openal pulseaudio unixodbc vulkan-headers wayland-protocols xkeyboard-config

name=qt5
version=5.15.12
release=1
source=(https://download.qt.io/archive/qt/5.15/$version/single/qt-everywhere-opensource-src-$version.tar.xz
	qt-everywhere-opensource-src-$version-kf5-1.patch
	qtbase-5.15.8-fix-missing-qtsan-include.patch
	qt-5.15.11-xkbcommon160.patch
	qt5-cflags.patch
	qt5-logo.png
	qtbase-everywhere-src-5.14.2-no_relocatable.patch
	qtbase-everywhere-src-5.15.2-libglvnd.patch
	qtbase-use-wayland-on-gnome.patch)

build() {
	if [[ ! -e /usr/include/xkbcommon/xkbcommon-x11.h ]]; then
		printf '\e[31m%s\e[m\n' "libxkbcommon is not built with x11 support!"
		printf '\e[31m%s\e[m\n' "qtx11extras will not be built!"
		printf '\e[33m%s\e[m\n' "Install xkeyboard-config and rebuild libxkbcommon if you need that"
	fi

	cd qt-everywhere-src-$version

	patch -p1 -i $SRC/qt-everywhere-opensource-src-$version-kf5-1.patch
	patch -p1 -i $SRC/qt-5.15.11-xkbcommon160.patch
	patch -d qtbase -p1 -i $SRC/qt5-cflags.patch
	patch -d qtbase -p1 -i $SRC/qtbase-everywhere-src-5.14.2-no_relocatable.patch
	patch -d qtbase -p1 -i $SRC/qtbase-everywhere-src-5.15.2-libglvnd.patch
	patch -d qtbase -p1 -i $SRC/qtbase-use-wayland-on-gnome.patch
	patch -d qtbase -p1 -i $SRC/qtbase-5.15.8-fix-missing-qtsan-include.patch

	export PYTHON='/usr/bin/python3'
	mkdir $SRC/bin
	ln -s /usr/bin/python3 $SRC/bin/python
	export PATH="$SRC/bin:$PATH"

	export QTDIR="$PWD"
	export LD_LIBRARY_PATH="$QTDIR/qtbase/lib:$QTDIR/qttools/lib:$LD_LIBRARY_PATH"
	export QT_PLUGIN_PATH="$QTDIR/qtbase/plugins"

	prt-get isinst ccache && PKGMK_QT5+=' -ccache' && \
		PATH="$(echo ${PATH} | awk -v RS=: -v ORS=: '/ccache/ {next} {print}' | sed 's/:*$//')"
	prt-get isinst assimp && PKGMK_QT5+=' -system-assimp'
	#prt-get isinst clang && PKGMK_QT5+=' -platform linux-clang'
	#prt-get isinst clang lld && PKGMK_QT5+=' -linker lld' || PKGMK_QT5+=' -linker gold'

	sed -i '/utility/a #include <limits>'     qtbase/src/corelib/global/qglobal.h
	sed -i '/string/a #include <limits>'      qtbase/src/corelib/global/qfloat16.h
	sed -i '/qbytearray/a #include <limits>'  qtbase/src/corelib/text/qbytearraymatcher.h
	sed -i '/type_traits/a #include <limits>' qtdeclarative/src/qmldebug/qqmlprofilerevent_p.h

	#pushd qtbase/src/3rdparty
	#mv freetype libjpeg libpng zlib sqlite xcb pcre2 $SRC
	#prt-get isinst assimp && mv assimp $SRC
	#popd

	./configure $PKGMK_QT5 \
		-prefix /usr/ \
		-archdatadir /usr/lib/qt5 \
		-bindir /usr/lib/qt5/bin \
		-plugindir /usr/lib/qt5/plugins \
		-importdir /usr/lib/qt5/imports \
		-datadir /usr/share/qt5 \
		-docdir /usr/share/doc/qt5 \
		-translationdir /usr/share/qt5/translations \
		-examplesdir /usr/share/doc/qt5/examples \
		-headerdir /usr/include/qt5 \
		-libdir /usr/lib \
		-sysconfdir /etc/xdg \
		QMAKE_CFLAGS_RELEASE="$CFLAGS -Wno-enum-constexpr-conversion" \
		QMAKE_CXXFLAGS_RELEASE="$CXXFLAGS -Wno-enum-constexpr-conversion" \
		QMAKE_LFLAGS_RELEASE="${LDFLAGS} -fuse-ld=gold" \
		-confirm-license \
		-opensource \
		-release \
		-dbus-linked \
		-opengl desktop \
		-openssl-linked \
		-system-harfbuzz \
		-system-libjpeg \
		-system-libpng \
		-system-pcre \
		-system-sqlite \
		-system-zlib \
		-nomake examples \
		-no-mimetype-database \
		-no-rpath \
		-no-strip \
		-no-reduce-relocations \
		-no-feature-relocatable \
		-no-pch \
		-skip qtwebengine \
		-silent

	make
	make -j1 INSTALL_ROOT=$PKG install

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

	sed -e "s|$PWD/qtbase|/usr/lib/qt5|g" \
		-i $PKG/usr/lib/qt5/mkspecs/modules/qt_lib_bootstrap_private.pri

	rm -r $PKG/usr/share/doc

	# Install useful symlinks
	install -d $PKG/usr/bin
	for file in $PKG/usr/lib/qt5/bin/*; do
		ln -s ../lib/qt5/bin/$(basename $file) $PKG/usr/bin/$(basename $file)-qt5
		ln -s ../lib/qt5/bin/$(basename $file) $PKG/usr/bin/$(basename $file)
	done

	# Add menu entries for all those hidden but great Qt applications:
	# # Qt5 logo:
	install -d $PKG/usr/share/icons/hicolor/48x48/apps
	# requires imagemagick
	#convert qtdoc/doc/src/images/qt-logo.png  -resize 48x48 $PKG/usr/share/icons/hicolor/48x48/apps/qt5-logo.png
	install -m 0644 $SRC/qt5-logo.png $PKG/usr/share/icons/hicolor/48x48/apps/qt5-logo.png

	# Assistant icons
	install -m 0644 -D qttools/src/assistant/assistant/images/assistant.png \
		$PKG/usr/share/icons/hicolor/32x32/apps/qt5-assistant.png
	install -m 0644 -D qttools/src/assistant/assistant/images/assistant-128.png \
		$PKG/usr/share/icons/hicolor/128x128/apps/qt5-assistant.png

	# Designer icon
	install -m 0644 -D qttools/src/designer/src/designer/images/designer.png \
		$PKG/usr/share/icons/hicolor/128x128/apps/qt5-designer.png

	# QDbusViewer icons
	install -m 0644 qttools/src/qdbus/qdbusviewer/images/qdbusviewer.png \
		$PKG/usr/share/icons/hicolor/32x32/apps/qt5-qdbusviewer.png
	install -m 0644 qttools/src/qdbus/qdbusviewer/images/qdbusviewer-128.png \
		$PKG/usr/share/icons/hicolor/128x128/apps/qt5-qdbusviewer.png

	# Linguist icons
	for icon in qttools/src/linguist/linguist/images/icons/linguist-*-32.png ; do
		size=$(echo $(basename ${icon}) | cut -d- -f2)
		install -m 0644 -D ${icon} $PKG/usr/share/icons/hicolor/${size}x${size}/apps/qt5-linguist.png
	done

	# And the .desktop files too:
	install -d $PKG/usr/share/applications
	cat <<EOF > $PKG/usr/share/applications/qt5-designer.desktop
[Desktop Entry]
Name=Qt5 Designer
GenericName=Interface Designer
Comment=Design GUIs for Qt5 applications
Exec=designer-qt5 -qt=5
Icon=qt5-designer
MimeType=application/x-designer;
Terminal=false
Encoding=UTF-8
Type=Application
Categories=Qt;Development;
EOF
	cat <<EOF > $PKG/usr/share/applications/qt5-assistant.desktop
[Desktop Entry]
Name=Qt5 Assistant
Comment=Shows Qt5 documentation and examples
Exec=assistant-qt5 -qt=5
Icon=qt5-assistant
Terminal=false
Encoding=UTF-8
Type=Application
Categories=Qt;Development;Documentation;
EOF
	cat <<EOF > $PKG/usr/share/applications/qt5-linguist.desktop
[Desktop Entry]
Name=Qt5 Linguist
Comment=Add translations to Qt5 applications
Exec=linguist-qt5 -qt=5
Icon=qt5-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/qt5-qdbusviewer.desktop
[Desktop Entry]
Name=Qt5 QDbusViewer
GenericName=Qt5 D-Bus Debugger
Comment=Debug D-Bus applications
Exec=qdbusviewer-qt5
Icon=qt5-qdbusviewer
Terminal=false
Type=Application
Categories=Qt;Development;Debugger;
EOF
}
