SUMMARY="Telegram Desktop Messenger"
DESCRIPTION="Unofficial build of the original Telegram client for Haiku."
HOMEPAGE="https://www.telegram.org/"
COPYRIGHT="2013-2022 Telegram"
LICENSE="GNU GPL v3"
REVISION="1"
SOURCE_URI="https://github.com/telegramdesktop/tdesktop/releases/download/v$portVersion/tdesktop-$portVersion-full.tar.gz"
CHECKSUM_SHA256="2fb9d276e67c05f1843bad3cbe327079ae0f47710d155748b5d442fb349a58ac"
SOURCE_FILENAME="tdesktop-$portVersion-full.tar.gz"
SOURCE_DIR="tdesktop-$portVersion-full"
srcGitRev_2="2b383fe05f8ae78ac99470b9a2b9ea22b3ee5a92"
SOURCE_URI_2="https://salsa.debian.org/debian/telegram-desktop/-/raw/$srcGitRev_2/debian/rules#noarchive"
CHECKSUM_SHA256_2="613e7e357518739e1f7d035337f37c344b248283fd4d916ddc95df73c2ff84ad"

PATCHES="
	libtgvoip-$portVersion.patchset
	telegram_desktop-$portVersion.patchset
	"
ADDITIONAL_FILES="telegram_desktop.rdef.in"

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

PROVIDES="
	telegram_desktop$secondaryArchSuffix = $portVersion
	app:Telegram
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libabsl_strings$secondaryArchSuffix
	lib:libatomic$secondaryArchSuffix
	lib:libavcodec$secondaryArchSuffix
	lib:libavformat$secondaryArchSuffix
	lib:libavutil$secondaryArchSuffix
	lib:libcrypto$secondaryArchSuffix
	lib:libgiomm_2.4$secondaryArchSuffix
	lib:libglib_2.0$secondaryArchSuffix
	lib:libglibmm_2.4$secondaryArchSuffix
	lib:libgthread_2.0$secondaryArchSuffix
	lib:libhunspell_1.7$secondaryArchSuffix
	lib:libintl$secondaryArchSuffix
	lib:libjpeg$secondaryArchSuffix
	lib:liblz4$secondaryArchSuffix
	lib:liblzma$secondaryArchSuffix
	lib:libminizip$secondaryArchSuffix
	lib:libopenal$secondaryArchSuffix
	lib:libopus$secondaryArchSuffix
	lib:libQt6Core$secondaryArchSuffix
	lib:libQt6Core5Compat$secondaryArchSuffix
	lib:libQt6Gui$secondaryArchSuffix
	lib:libQt6Network$secondaryArchSuffix
	lib:libQt6Svg$secondaryArchSuffix
	lib:libQt6Widgets$secondaryArchSuffix
	lib:librnnoise$secondaryArchSuffix
	lib:libsigc_2.0$secondaryArchSuffix
	lib:libstdc++$secondaryArchSuffix
	lib:libswresample$secondaryArchSuffix
	lib:libswscale$secondaryArchSuffix
	lib:libvpx$secondaryArchSuffix
	lib:libxxhash$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	qt6_tools${secondaryArchSuffix}_devel
	devel:libabsl_strings$secondaryArchSuffix
	devel:libavcodec$secondaryArchSuffix
	devel:libavformat$secondaryArchSuffix
	devel:libavutil$secondaryArchSuffix
	devel:libcrypto$secondaryArchSuffix
	devel:libglib_2.0$secondaryArchSuffix
	devel:libglibmm_2.4$secondaryArchSuffix
	devel:libgthread_2.0$secondaryArchSuffix
	devel:libhunspell_1.7$secondaryArchSuffix
	devel:libjpeg$secondaryArchSuffix
	devel:liblz4$secondaryArchSuffix
	devel:liblzma$secondaryArchSuffix
	devel:libminizip$secondaryArchSuffix
	devel:libopenal$secondaryArchSuffix
	devel:libopus$secondaryArchSuffix
	devel:libqrcodegen$secondaryArchSuffix
	devel:libQt6Core$secondaryArchSuffix
	devel:libQt6Core5Compat$secondaryArchSuffix
	devel:libQt6Gui$secondaryArchSuffix
	devel:libQt6Network$secondaryArchSuffix
	devel:libQt6Svg$secondaryArchSuffix
	devel:libQt6Widgets$secondaryArchSuffix
	devel:librapidjson$secondaryArchSuffix
	devel:librnnoise$secondaryArchSuffix
	devel:libswresample$secondaryArchSuffix
	devel:libswscale$secondaryArchSuffix
	devel:libtg_owt$secondaryArchSuffix
	devel:libvpx$secondaryArchSuffix
	devel:libxxhash$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	devel:range_v3$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:cmake
	cmd:gawk
	cmd:gcc$secondaryArchSuffix
	cmd:ninja
	cmd:pkg_config$secondaryArchSuffix
	cmd:python
	cmd:sed
	cmd:yasm
	"

BUILD()
{
	export DISABLE_ASLR=1

	# get API_ID and API_HASH from Debian
	local TELEGRAM_API_ID=`sed -n "/TDESKTOP_API_ID/p" $sourceDir2/rules | cut -d'=' -f2 | cut -d' ' -f1`
	local TELEGRAM_API_HASH=`sed -n "/TDESKTOP_API_HASH/p" $sourceDir2/rules | cut -d'=' -f2 | cut -d' ' -f1`

	if [ -z $TELEGRAM_API_ID ] || [ -z $TELEGRAM_API_HASH ]; then
		TELEGRAM_API_ID="17349"
		TELEGRAM_API_HASH="344583e45741c457fe1862106095a5eb"
		echo -e "\e[91m***************************************************************************\e[39m"
		echo -e "\e[91m Use demo API_ID = $TELEGRAM_API_ID and API_HASH = $TELEGRAM_API_HASH      \e[39m"
		echo -e "\e[91m***************************************************************************\e[39m"
	else
		echo -e "\e[32m***************************************************************************\e[39m"
		echo -e "\e[32m Use custom API_ID = $TELEGRAM_API_ID and API_HASH = $TELEGRAM_API_HASH    \e[39m"
		echo -e "\e[32m***************************************************************************\e[39m"
	fi

	# build telegram
	cmake -S . -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=Release \
		-DDESKTOP_APP_QT6=ON \
		-DTDESKTOP_API_TEST=OFF \
		-DTDESKTOP_DISABLE_GTK_INTEGRATION=ON \
		-DDESKTOP_APP_DISABLE_CRASH_REPORTS=ON \
		-DDESKTOP_APP_DISABLE_DBUS_INTEGRATION=ON \
		-DDESKTOP_APP_DISABLE_GTK_INTEGRATION=ON \
		-DDESKTOP_APP_DISABLE_WAYLAND_INTEGRATION=ON \
		-DDESKTOP_APP_DISABLE_X11_INTEGRATION=ON \
		-DDESKTOP_APP_DISABLE_AUTOUPDATE=ON \
		-DDESKTOP_APP_DISABLE_WEBKITGTK=ON \
		-DDESKTOP_APP_USE_PACKAGED_FONTS=OFF \
		-DDESKTOP_APP_USE_HUNSPELL_ONLY=ON \
		-DTDESKTOP_USE_PACKAGED_TGVOIP=OFF \
		-DLIBTGVOIP_DISABLE_ALSA=ON \
		-DLIBTGVOIP_DISABLE_PULSEAUDIO=ON \
		-DTDESKTOP_API_ID=$TELEGRAM_API_ID \
		-DTDESKTOP_API_HASH=$TELEGRAM_API_HASH

	ninja -C build $jobArgs
}

INSTALL()
{
	mkdir -p $appsDir
	cp build/bin/Telegram $appsDir
	strip $appsDir/Telegram

	local APP_SIGNATURE="application/x-vnd.telegram"
	local MAJOR="`echo "$portVersion" | cut -d. -f1`"
	local MIDDLE="`echo "$portVersion" | cut -d. -f2`"
	local MINOR="`echo "$portVersion" | cut -d. -f3`"
	local LONG_INFO="$SUMMARY"
	sed \
		-e "s|@APP_SIGNATURE@|$APP_SIGNATURE|" \
		-e "s|@MAJOR@|$MAJOR|" \
		-e "s|@MIDDLE@|$MIDDLE|" \
		-e "s|@MINOR@|$MINOR|" \
		-e "s|@LONG_INFO@|$LONG_INFO|" \
		$portDir/additional-files/telegram_desktop.rdef.in > build/telegram_desktop.rdef

	addResourcesToBinaries build/telegram_desktop.rdef $appsDir/Telegram
	addAppDeskbarSymlink $appsDir/Telegram
}
