# Contributor: Leo <thinkabit.ukim@gmail.com>
# Contributor: Nulo <alpine@nulo.in>
# Contributor: Donoban <donoban@riseup.net>
# Maintainer: Milan P. Stanić <mps@arvanta.net>
pkgname=telegram-desktop
# NOTE: Keep in mind that seemingly normal releases may be pre-releases.
# Check GitHub to make sure:
# https://github.com/telegramdesktop/tdesktop/releases
pkgver=5.14.3
pkgrel=0
pkgdesc="Telegram Desktop messaging app"
options="!check" # no test suite
url="https://desktop.telegram.org/"
# armhf blocked by qt5-qtwayland
# ppc64le and s390x blocked by tg_owt
# x86, riscv64: textrels
# riscv64: libdispatch
arch="all !x86 !ppc64le !s390x !armhf !riscv64"
license="GPL-3.0-or-later WITH OpenSSL"
_commitid=51743dfd01dff6179e2d8f7095729caa4e2222e9
depends="qt6-qtimageformats font-opensans"

# Sorted according to
# https://github.com/telegramdesktop/tdesktop/wiki/The-Packaged-Building-Mode
# libtgvoip is bundled as suggested by upstream
# jemalloc is disabled
# rlottie is bundled as suggested by upstream
# We are forcing it to use packaged libvpx with a patch below
# kcoreaddons is bundled as it requires to be built with Qt6.
makedepends="
	ada-dev
	python3
	extra-cmake-modules
	qt6-qt5compat-dev
	qt6-qtsvg-dev
	qt6-qtwayland-dev

	tg_owt-dev<20230314
	gperf
	opus-dev
	alsa-lib-dev
	pulseaudio-dev

	boost-dev
	fmt-dev

	abseil-cpp-dev
	ffmpeg-dev
	gobject-introspection-dev
	libdispatch-dev
	gsl-dev
	hunspell-dev
	lz4-dev
	minizip-dev
	msgsl
	openal-soft-dev
	openssl-dev>3
	range-v3-dev
	rnnoise-dev
	tl-expected
	wayland-dev
	libxcb-dev
	xcb-util-keysyms-dev
	xxhash-dev
	zlib-dev

	libvpx-dev
	protobuf-dev

	cmake
	samurai
	meson
	"

# small-sizes.patch is for postmarketOS. It makes UI elements fit in small
# screens.
# https://github.com/telegramdesktop/tdesktop/issues/8058#issuecomment-643461605
source="
	https://github.com/telegramdesktop/tdesktop/releases/download/v$pkgver/tdesktop-$pkgver-full.tar.gz
	tdlib-$_commitid.tar.gz::https://github.com/tdlib/td/archive/$_commitid.tar.gz
	small-sizes.patch
	fix-include.patch
	"
builddir="$srcdir/tdesktop-$pkgver-full"

build() {
	LDFLAGS="$LDFLAGS -Wl,-z,stack-size=1024768"
	cmake -S $srcdir/td-$_commitid -B $srcdir/td-$_commitid/build \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX="$srcdir/td-$_commitid/install" \
		-Wno-dev \
		-DTD_E2E_ONLY=ON
	cmake --build $srcdir/td-$_commitid/build
	cmake --install $srcdir/td-$_commitid/build

	cmake -B build -G Ninja . \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-Dtde2e_DIR="$srcdir/td-$_commitid/install/lib/cmake/tde2e" \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DDESKTOP_APP_DISABLE_CRASH_REPORTS=ON \
		-DTDESKTOP_API_ID=17349 \
		-DTDESKTOP_API_HASH=344583e45741c457fe1862106095a5eb \
		-DDESKTOP_APP_DISABLE_JEMALLOC=ON \
		-DDESKTOP_APP_USE_PACKAGED=ON
	cmake --build build
}

package() {
	DESTDIR="$pkgdir" cmake --install build
}

sha512sums="
1c8d6f3f858789c716bdaef8ef00896aac07ef8e6792489b80bf162b40db80dc2cf1831335269c4bc12e11c1b6326cbf8ffd40fc9d847c13196f540e15a1978d  tdesktop-5.14.3-full.tar.gz
c0ec498011b821d545904674ed3534a5a2be4b38025daa4bb4e9661ec62c1583bc1edadb1dae2bc2619dc50e72baf5a3690e9aed6e87b2c123ce27370d00d9ff  tdlib-51743dfd01dff6179e2d8f7095729caa4e2222e9.tar.gz
396b5577bdeb852be3659fcbf09a1ebdc33432baea49296869e2f968d61343679187ed4b923c51c8f417b8d4679aedd7ac863abed255b5b6b1b589029fdc34f6  small-sizes.patch
b86048c271fdb9cbd0d8d62ca536c8e2d9c25e10b58fbb7932781ba100cc1b4e6c030e00477a11548b9acd0abfd4a0752f4c8de10faf6e61052f8b52354b82c6  fix-include.patch
"
