SUMMARY="Experimental Telegram Desktop fork"
DESCRIPTION="Kotatogram Desktop, being based on Telegram Desktop, has all \
its features, but it also has some more useful and cosmetic features."
HOMEPAGE="https://kotatogram.github.io/"
COPYRIGHT="2013-2021 Telegram
	2021 Kotatogram Team"
LICENSE="GNU GPL v3"
REVISION="2"
SOURCE_URI="https://github.com/kotatogram/kotatogram-desktop/archive/refs/tags/k$portVersion.tar.gz"
CHECKSUM_SHA256="ba65b7a056caca2c91b590463cf54fffe8dbab7b76d145bbfd64a8bb03978aac"
SOURCE_FILENAME="kotatogram-$portVersion.tar.gz"
SOURCE_DIR="kotatogram-desktop-k$portVersion"
#libtgvoip
srcGitRev_2="a090c6a8f7bfb365b301d85bb8c9664d71321c5c"
SOURCE_URI_2="https://github.com/telegramdesktop/libtgvoip/archive/$srcGitRev_2.tar.gz"
CHECKSUM_SHA256_2="34f1b49cfdc71dd05477986998c0e3888bac411853d8b2ee9ddd48dfc416e88e"
SOURCE_FILENAME_2="libtgvoip-$srcGitRev_2.tar.gz"
SOURCE_DIR_2="libtgvoip-$srcGitRev_2"
#rlottie
srcGitRev_3="8c69fc20cf2e150db304311f1233a4b55a8892d7"
SOURCE_URI_3="https://github.com/desktop-app/rlottie/archive/$srcGitRev_3.tar.gz"
CHECKSUM_SHA256_3="cbc1f4bf8c28ffeb89852eda7056ff6aa80d49cb45736a583c931d5ceeccdcdd"
SOURCE_FILENAME_3="rlottie-$srcGitRev_3.tar.gz"
SOURCE_DIR_3="rlottie-$srcGitRev_3"
#lib_crl
srcGitRev_4="ec103d6bccaa59b56537c8658c9e41415bb9ccaf"
SOURCE_URI_4="https://github.com/desktop-app/lib_crl/archive/$srcGitRev_4.tar.gz"
CHECKSUM_SHA256_4="64edbf9083c9ce7f6e98e741596e47dafc9dbcc818e45b3288186c9668fad153"
SOURCE_FILENAME_4="lib_crl-$srcGitRev_4.tar.gz"
SOURCE_DIR_4="lib_crl-$srcGitRev_4"
#lib_rpl
srcGitRev_5="94a42b775ab4e46e5edeb88d8ed6c06f9e869c61"
SOURCE_URI_5="https://github.com/desktop-app/lib_rpl/archive/$srcGitRev_5.tar.gz"
CHECKSUM_SHA256_5="355477e3b53ffff6260bab92f8acd859db4e6cfc7a69286ec49da85ad67decd3"
SOURCE_FILENAME_5="lib_rpl-$srcGitRev_5.tar.gz"
SOURCE_DIR_5="lib_rpl-$srcGitRev_5"
#lib_base
srcGitRev_6="087bf4e6159678fada93eea39e923ce634f51caf"
SOURCE_URI_6="https://github.com/desktop-app/lib_base/archive/$srcGitRev_6.tar.gz"
CHECKSUM_SHA256_6="95e29518b33f775c03d9fdd22d459d5bb4576178d1a10ad845b847d50a1724b8"
SOURCE_FILENAME_6="lib_base-$srcGitRev_6.tar.gz"
SOURCE_DIR_6="lib_base-$srcGitRev_6"
#codegen
srcGitRev_7="a60edf917419407cfe5c6095a56ccf363417ebd8"
SOURCE_URI_7="https://github.com/desktop-app/codegen/archive/$srcGitRev_7.tar.gz"
CHECKSUM_SHA256_7="06ee75ceb3be9cbd1471f14c1edce07b30189b012edb71a3c8ea1fdf8e30ad52"
SOURCE_FILENAME_7="codegen-$srcGitRev_7.tar.gz"
SOURCE_DIR_7="codegen-$srcGitRev_7"
#lib_ui
srcGitRev_8="f8f407004b139a1cd882c538f00e8a87b5e65b9a"
SOURCE_URI_8="https://github.com/kotatogram/lib_ui/archive/$srcGitRev_8.tar.gz"
CHECKSUM_SHA256_8="1e54c8b6b6da81c52a23d338947220ce30594ad565ddfa5424e909f9cae02531"
SOURCE_FILENAME_8="lib_ui-$srcGitRev_8.tar.gz"
SOURCE_DIR_8="lib_ui-$srcGitRev_8"
#lib_rlottie
srcGitRev_9="0671bf70547381effcf442ec9618e04502a8adbc"
SOURCE_URI_9="https://github.com/desktop-app/lib_rlottie/archive/$srcGitRev_9.tar.gz"
CHECKSUM_SHA256_9="28b68af080b03bf0dd66bab5f9dceb712e0b3203fd5ad7f8096050272234941b"
SOURCE_FILENAME_9="lib_rlottie-$srcGitRev_9.tar.gz"
SOURCE_DIR_9="lib_rlottie-$srcGitRev_9"
#lib_lottie
srcGitRev_10="ad7fce76f3b403471a296c928bae67cd36b8b2cf"
SOURCE_URI_10="https://github.com/desktop-app/lib_lottie/archive/$srcGitRev_10.tar.gz"
CHECKSUM_SHA256_10="b206fa1af74bf81bc54abcf14dc573e22d4e647de38b3c977d9e3fe18c99ec95"
SOURCE_FILENAME_10="lib_lottie-$srcGitRev_10.tar.gz"
SOURCE_DIR_10="lib_lottie-$srcGitRev_10"
#lib_tl
srcGitRev_11="45faed44e7f4d11fec79b7a70e4a35dc91ef3fdb"
SOURCE_URI_11="https://github.com/desktop-app/lib_tl/archive/$srcGitRev_11.tar.gz"
CHECKSUM_SHA256_11="1db86a372f9c5d87b836a60ee16bf6a69dd343840498bc0121118ac9fc2b80e1"
SOURCE_FILENAME_11="lib_tl-$srcGitRev_11.tar.gz"
SOURCE_DIR_11="lib_tl-$srcGitRev_11"
#lib_spellcheck
srcGitRev_12="b1b295fee18e44534d9a96ec9858baa4c932f7d2"
SOURCE_URI_12="https://github.com/desktop-app/lib_spellcheck/archive/$srcGitRev_12.tar.gz"
CHECKSUM_SHA256_12="2a3958a2d7b5db14b73508dd525b847e5fac20b2168a58fad83d4e33ac007ed1"
SOURCE_FILENAME_12="lib_spellcheck-$srcGitRev_12.tar.gz"
SOURCE_DIR_12="lib_spellcheck-$srcGitRev_12"
#lib_storage
srcGitRev_13="73d57840ac603107381e0e6b22d5b3bdcae492c6"
SOURCE_URI_13="https://github.com/desktop-app/lib_storage/archive/$srcGitRev_13.tar.gz"
CHECKSUM_SHA256_13="ce440ae1cbfc35a4e1f04adfe14497991a9347045970620b199abfd3f162b65b"
SOURCE_FILENAME_13="lib_storage-$srcGitRev_13.tar.gz"
SOURCE_DIR_13="lib_storage-$srcGitRev_13"
#cmake_helpers
srcGitRev_14="7d2037630abad09ee7b8857710aa7a0efd0c5672"
SOURCE_URI_14="https://github.com/kotatogram/cmake_helpers/archive/$srcGitRev_14.tar.gz"
CHECKSUM_SHA256_14="035954107782d63781f2328ce11c051e74a379d5cf63f4d38d6c77502f979a97"
SOURCE_FILENAME_14="cmake_helpers-$srcGitRev_14.tar.gz"
SOURCE_DIR_14="cmake_helpers-$srcGitRev_14"
#QR-Code-generator
srcGitRev_15="67c62461d380352500fc39557fd9f046b7fe1d18"
SOURCE_URI_15="https://github.com/nayuki/QR-Code-generator/archive/$srcGitRev_15.tar.gz"
CHECKSUM_SHA256_15="1f1218c0a0abfc420cbc651675434d971b5e672b54428860339c51ecdf1958fc"
SOURCE_FILENAME_15="QR-Code-generator-$srcGitRev_15.tar.gz"
SOURCE_DIR_15="QR-Code-generator-$srcGitRev_15"
#lib_qr
srcGitRev_16="2b08c71c6edcfc3e31f7d7f518cc963493b6e189"
SOURCE_URI_16="https://github.com/desktop-app/lib_qr/archive/$srcGitRev_16.tar.gz"
CHECKSUM_SHA256_16="f1aee181ebbcec2e8b8f4d8321d966f8a51ee0f015153602987e88113d6816e9"
SOURCE_FILENAME_16="lib_qr-$srcGitRev_16.tar.gz"
SOURCE_DIR_16="lib_qr-$srcGitRev_16"
#lib_webrtc
srcGitRev_17="04cc1ff4a6fdade551e26441488ac0d0a208e96c"
SOURCE_URI_17="https://github.com/desktop-app/lib_webrtc/archive/$srcGitRev_17.tar.gz"
CHECKSUM_SHA256_17="e2fd20f6b059ad6654abe640a48dea183cfc880c7c33424c168c5e86fad316c4"
SOURCE_FILENAME_17="lib_webrtc-$srcGitRev_17.tar.gz"
SOURCE_DIR_17="lib_webrtc-$srcGitRev_17"
#tgcalls
srcGitRev_18="955e51f15ba1b2e35a910064388e5c0737032bb7"
SOURCE_URI_18="https://github.com/TelegramMessenger/tgcalls/archive/$srcGitRev_18.tar.gz"
CHECKSUM_SHA256_18="694f6a5ce50e0b3ae0204c6d3692a9ca71b6347f35d41a45b52e5a04f7f7aeee"
SOURCE_FILENAME_18="tgcalls-$srcGitRev_18.tar.gz"
SOURCE_DIR_18="tgcalls-$srcGitRev_18"
#lib_webview
srcGitRev_19="8be9c0ff274569cbb1bc9dd4ea9ed9089c37ca93"
SOURCE_URI_19="https://github.com/desktop-app/lib_webview/archive/$srcGitRev_19.tar.gz"
CHECKSUM_SHA256_19="1dce150a586ca8ab8716c7880723ae5a8c390eeb689ed727bcd4f64311d3e88d"
SOURCE_FILENAME_19="lib_webview-$srcGitRev_19.tar.gz"
SOURCE_DIR_19="lib_webview-$srcGitRev_19"
#expected
srcGitRev_20="1d9c5d8c0da84b8ddc54bd3d90d632eec95c1f13"
SOURCE_URI_20="https://github.com/TartanLlama/expected/archive/$srcGitRev_20.tar.gz"
CHECKSUM_SHA256_20="95b82e57a6214ca84105016670eb7f080ad54f71290d8169fcaa969cb6dfc6c8"
SOURCE_FILENAME_20="expected-$srcGitRev_20.tar.gz"
SOURCE_DIR_20="expected-$srcGitRev_20"
#GSL
srcGitRev_21="1999b48a519196711f0d03af3b7eedd49fcc6db3"
SOURCE_URI_21="https://github.com/Microsoft/GSL/archive/$srcGitRev_21.tar.gz"
CHECKSUM_SHA256_21="486d9c18da2303e212165bee7e14fbe5565084b30b1ad7810e26a8a64059492f"
SOURCE_FILENAME_21="GSL-$srcGitRev_21.tar.gz"
SOURCE_DIR_21="GSL-$srcGitRev_21"
#APIKEY
srcGitRev_22="2b383fe05f8ae78ac99470b9a2b9ea22b3ee5a92"
SOURCE_URI_22="https://salsa.debian.org/debian/telegram-desktop/-/raw/$srcGitRev_22/debian/rules#noarchive"
CHECKSUM_SHA256_22="613e7e357518739e1f7d035337f37c344b248283fd4d916ddc95df73c2ff84ad"

PATCHES="kotatogram_desktop-$portVersion.patchset"
PATCHES_2="libtgvoip-$portVersion.patchset"
PATCHES_6="lib_base-$portVersion.patchset"
PATCHES_8="lib_ui-$portVersion.patchset"
PATCHES_14="cmake_helpers-$portVersion.patchset"
PATCHES_17="lib_webrtc-$portVersion.patchset"
PATCHES_18="tgcalls-$portVersion.patchset"

ADDITIONAL_FILES="kotatogram_desktop.rdef.in"

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

PROVIDES="
	kotatogram_desktop$secondaryArchSuffix = $portVersion
	app:Kotatogram
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libabsl_strings$secondaryArchSuffix
	lib:libabsl_throw_delegate$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:libabsl_throw_delegate$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:make
	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" $sourceDir22/rules | cut -d'=' -f2 | cut -d' ' -f1`
	local TELEGRAM_API_HASH=`sed -n "/TDESKTOP_API_HASH/p" $sourceDir22/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
	
	# link submodules
	rm -rf $sourceDir/cmake
	rm -rf $sourceDir/Telegram/ThirdParty/{libtgvoip,rlottie,QR,tgcalls,expected,GSL}
	rm -rf $sourceDir/Telegram/{lib_crl,lib_rpl,lib_base,codegen,lib_ui,lib_lottie,lib_rlottie}
	rm -rf $sourceDir/Telegram/{lib_tl,lib_spellcheck,lib_storage,lib_qr,lib_webrtc,lib_webview}

	ln -sf $sourceDir2 $sourceDir/Telegram/ThirdParty/libtgvoip
	ln -sf $sourceDir3 $sourceDir/Telegram/ThirdParty/rlottie
	ln -sf $sourceDir4 $sourceDir/Telegram/lib_crl
	ln -sf $sourceDir5 $sourceDir/Telegram/lib_rpl
	ln -sf $sourceDir6 $sourceDir/Telegram/lib_base
	ln -sf $sourceDir7 $sourceDir/Telegram/codegen
	ln -sf $sourceDir8 $sourceDir/Telegram/lib_ui
	ln -sf $sourceDir9 $sourceDir/Telegram/lib_rlottie
	ln -sf $sourceDir10 $sourceDir/Telegram/lib_lottie
	ln -sf $sourceDir11 $sourceDir/Telegram/lib_tl
	ln -sf $sourceDir12 $sourceDir/Telegram/lib_spellcheck
	ln -sf $sourceDir13 $sourceDir/Telegram/lib_storage
	ln -sf $sourceDir14 $sourceDir/cmake
	ln -sf $sourceDir15 $sourceDir/Telegram/ThirdParty/QR
	ln -sf $sourceDir16 $sourceDir/Telegram/lib_qr
	ln -sf $sourceDir17 $sourceDir/Telegram/lib_webrtc
	ln -sf $sourceDir18 $sourceDir/Telegram/ThirdParty/tgcalls
	ln -sf $sourceDir19 $sourceDir/Telegram/lib_webview
	ln -sf $sourceDir20 $sourceDir/Telegram/ThirdParty/expected
	ln -sf $sourceDir21 $sourceDir/Telegram/ThirdParty/GSL

	# build telegram
	mkdir -p build
	cd build

	cmake .. \
		-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

	make -j1
}

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

	local APP_SIGNATURE="application/x-vnd.kotatogram"
	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/kotatogram_desktop.rdef.in > build/kotatogram_desktop.rdef

	addResourcesToBinaries build/kotatogram_desktop.rdef $appsDir/Kotatogram
	addAppDeskbarSymlink $appsDir/Kotatogram
}
