SUMMARY="A cross-platform application and UI framework (Development Tools, QtHelp)"
DESCRIPTION="The QtTools modules contains some tools mostly useful for application development. \
Included are Qt Designer (GUI design), QDbusViewer and more."
HOMEPAGE="https://qt.io/"
COPYRIGHT="2015-2021 The Qt Company Ltd."
LICENSE="GNU LGPL v2.1
	GNU LGPL v3
	GNU FDL v1"
REVISION="1"
SOURCE_URI="https://download.qt.io/official_releases/qt/${portVersion%.*}/$portVersion/submodules/qttools-everywhere-src-$portVersion.tar.xz"
CHECKSUM_SHA256="c0542ecb4a0aec21246a1cb5cd9f8f51b368929ce2065a455b13c3b47a94ecd1"
SOURCE_DIR="qttools-everywhere-src-$portVersion"
ADDITIONAL_FILES="
	Assistant.rdef.in
	Designer.rdef.in
	Linguist.rdef.in
	QDBusViewer.rdef.in
	FindWrapLibClang.cmake.in
	"

ARCHITECTURES="all !x86_gcc2"
SECONDARY_ARCHITECTURES="x86"

PROVIDES="
	qt6_tools$secondaryArchSuffix = $portVersion compat >= 6
	lib:libQt6Designer$secondaryArchSuffix = $portVersion compat >= 6
	lib:libQt6DesignerComponents$secondaryArchSuffix = $portVersion compat >= 6
	lib:libQt6Help$secondaryArchSuffix = $portVersion compat >= 6
	lib:libQt6UiTools$secondaryArchSuffix = $portVersion compat >= 6
	cmd:assistant6$secondaryArchSuffix = $portVersion compat >= 6
	cmd:designer6$secondaryArchSuffix = $portVersion compat >= 6
	cmd:linguist6$secondaryArchSuffix = $portVersion compat >= 6
	cmd:pixeltool6$secondaryArchSuffix = $portVersion compat >= 6
	cmd:qdbus6$secondaryArchSuffix = $portVersion compat >= 6
	cmd:qdbusviewer6$secondaryArchSuffix = $portVersion compat >= 6
	cmd:qdistancefieldgenerator6$secondaryArchSuffix = $portVersion compat >= 6
	cmd:qdoc6$secondaryArchSuffix = $portVersion compat >= 6
	cmd:qtdiag6$secondaryArchSuffix = $portVersion compat >= 6
	cmd:qtplugininfo6$secondaryArchSuffix = $portVersion compat >= 6
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libclang$secondaryArchSuffix
	lib:libclang_cpp$secondaryArchSuffix
	lib:libLLVM_9$secondaryArchSuffix
	lib:libGL$secondaryArchSuffix
	lib:libQt6Core$secondaryArchSuffix
	lib:libQt6Gui$secondaryArchSuffix
	lib:libQt6QmlModels$secondaryArchSuffix
	lib:libQt6Quick$secondaryArchSuffix
	lib:libQt6Qml$secondaryArchSuffix
	lib:libQt6Widgets$secondaryArchSuffix
	"

PROVIDES_devel="
	qt6_tools${secondaryArchSuffix}_devel = $portVersion compat >= 6
	devel:libQt6Designer$secondaryArchSuffix = $portVersion compat >= 6
	devel:libQt6DesignerComponents$secondaryArchSuffix = $portVersion compat >= 6
	devel:libQt6Help$secondaryArchSuffix = $portVersion compat >= 6
	devel:libQt6UiTools$secondaryArchSuffix = $portVersion compat >= 6
	"
REQUIRES_devel="
	qt6_tools$secondaryArchSuffix == $portVersion base
	haiku$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libclang$secondaryArchSuffix >= 9
	devel:libclang_cpp$secondaryArchSuffix >= 9
	devel:libLLVM_9$secondaryArchSuffix
	devel:libcrypto$secondaryArchSuffix
	devel:libQt6Core$secondaryArchSuffix
	devel:libQt6Gui$secondaryArchSuffix
	devel:libQt6QmlModels$secondaryArchSuffix
	devel:libQt6Quick$secondaryArchSuffix
	devel:libQt6Qml$secondaryArchSuffix
	devel:libQt6Widgets$secondaryArchSuffix
	devel:libssl$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:awk
	cmd:cmake
	cmd:find
	cmd:g++$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:make
	cmd:ninja
	cmd:perl
	cmd:pkg_config$secondaryArchSuffix
	cmd:python3.7
	cmd:sed
	cmd:xargs
	"

BUILD()
{
	export DISABLE_ASLR=1

	local LLVM_VERSION=$(llvm-config --version)
	local LLVM_VERION_MAJOR="`echo "$LLVM_VERSION" | cut -d. -f1`"
	local LLVM_VERION_MINOR="`echo "$LLVM_VERSION" | cut -d. -f2`"
	local LLVM_VERION_PATCH="`echo "$LLVM_VERSION" | cut -d. -f3`"
	
	sed \
			-e "s|@CLANG_VERSION_MAJOR@|$LLVM_VERION_MAJOR|" \
			-e "s|@CLANG_VERSION_MINOR@|$LLVM_VERION_MINOR|" \
			-e "s|@CLANG_VERSION_PATCH@|$LLVM_VERION_PATCH|" \
			-e "s|@CLANG_VERSION@|$LLVM_VERSION|" \
			-e "s|@CLANG_LIBDIR@|/system/$relativeDevelopLibDir|" \
			-e "s|@CLANG_INCLUDE_DIR@|/system/$relativeLibDir/clang/$LLVM_VERSION/include|" \
			$portDir/additional-files/FindWrapLibClang.cmake.in > $sourceDir/cmake/FindWrapLibClang.cmake

	cmake -B build -S $sourceDir -G Ninja \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_CXX_FLAGS=-fPIC \
		-DINSTALL_PUBLICBINDIR=$binDir

	find -name cmake_install.cmake -exec sed -i "s|set(CMAKE_INSTALL_PREFIX.*|set(CMAKE_INSTALL_PREFIX \"$prefix\")|" {} \;

	ninja -C build $jobArgs
}

INSTALL()
{
	ninja -C build install

	prepareInstalledDevelLibs libQt6Designer libQt6DesignerComponents libQt6Help libQt6UiTools

	cd $libDir
	for i in lib*.so.6.*;do
		ln -fs $i $(echo $i | cut -f1,2 -d.)
	done

	mkdir -p \
		$binDir \
		$dataDir/deskbar/menu/Applications/Qt

	# install symlinks for user-facing tools
	while read _line; do
		ln -s $_line
	done < $sourceDir/build/user_facing_tool_links.txt

	local MAJOR="`echo "$portVersion" | cut -d. -f1`"
	local MIDDLE="`echo "$portVersion" | cut -d. -f2`"
	local MINOR="`echo "$portVersion" | cut -d. -f3`"
	local VARIETY="B_APPV_FINAL"

	resFiles="Assistant Designer Linguist QDBusViewer"
	for j in $resFiles; do
		sed \
			-e "s|@MAJOR@|$MAJOR|" \
			-e "s|@MIDDLE@|$MIDDLE|" \
			-e "s|@MINOR@|$MINOR|" \
			-e "s|@VARIETY@|$VARIETY|" \
			$portDir/additional-files/$j.rdef.in > $sourceDir/$j.rdef
		mv $libDir/Qt6/${j,,} $libDir/Qt6/$j
		ln -s $libDir/Qt6/$j $libDir/Qt6/${j,,}
		addResourcesToBinaries $sourceDir/$j.rdef $libDir/Qt6/$j
		addAppDeskbarSymlink $libDir/Qt6/$j "Qt/Qt6 $j"
		mimeset -f $libDir/Qt6/$j
	done

	packageEntries devel \
		$developDir \
		$libDir/cmake \
		$dataDir/Qt6/mkspecs
}
