name='telegram'
version='7.0.5'
release=1
summary='Official Telegram messaging client'
summary_ru='Официальный клиент мессенджера Telegram'
group='Networking/Instant messaging'
desc='Telegram Desktop is the official Telegram messaging client with cloud-synced chats, voice and video calls, and no limits on file size or media history.'
desc_ru='Telegram Desktop — официальный клиент мессенджера Telegram с облачной синхронизацией чатов, голосовыми и видеозвонками и без ограничений на размер файлов и историю медиа.'
homepage='https://desktop.telegram.org/'
maintainer='Telegram Messenger LLP <https://github.com/telegramdesktop/tdesktop>'

architectures=('amd64')

license=('GPL-3.0-only')

provides=()
replaces=('telegram')
conflicts=()

auto_reqprov_method="dirty"
auto_req=0
auto_prov=0

deps=()
deps_debian=('libc6' 'libglib2.0-0t64 | libglib2.0-0' 'libfontconfig1' 'libfreetype6' 'libharfbuzz0b' 'libpng16-16' 'libpcre2-8-0' 'libbrotli1' 'libbz2-1.0' 'libblkid1' 'libmount1' 'libselinux1' 'libffi8' 'libexpat1' 'libgraphite2-3' 'zlib1g' 'libx11-6' 'hicolor-icon-theme')
deps_ubuntu=("${deps_debian[@]}")
deps_fedora=('glibc' 'glib2' 'fontconfig' 'freetype' 'harfbuzz' 'libpng' 'pcre2' 'brotli' 'bzip2-libs' 'libblkid' 'libmount' 'libselinux' 'libffi' 'expat' 'graphite2' 'zlib' 'libX11' 'hicolor-icon-theme')
deps_arch=('glibc' 'glib2' 'fontconfig' 'freetype2' 'harfbuzz' 'libpng' 'pcre2' 'brotli' 'bzip2' 'util-linux-libs' 'libselinux' 'libffi' 'expat' 'graphite' 'zlib' 'libx11' 'hicolor-icon-theme')
deps_opensuse=('glibc' 'libglib-2_0-0' 'libfontconfig1' 'libfreetype6' 'libharfbuzz0' 'libpng16-16' 'libpcre2-8-0' 'libbrotli1' 'libbz2-1' 'libblkid1' 'libmount1' 'libselinux1' 'libffi8' 'libexpat1' 'libgraphite2-3' 'libz1' 'libX11-6' 'hicolor-icon-theme')
deps_altlinux=('glibc' 'glib2' 'libfontconfig1' 'libfreetype' 'libharfbuzz' 'libpng16' 'libpcre2' 'libbrotlicommon' 'libbrotlidec' 'bzlib' 'libblkid' 'libmount' 'libselinux' 'libffi8' 'libexpat' 'libgraphite2' 'zlib' 'libX11' 'icon-theme-hicolor')

opt_deps=()
opt_deps_debian=('xdg-utils' 'libayatana-appindicator3-1 | libappindicator3-1' 'ca-certificates' 'libasound2t64 | libasound2 | pulseaudio' 'libgl1' 'xdg-desktop-portal' 'xdg-desktop-portal-gtk | xdg-desktop-portal-gnome | xdg-desktop-portal-kde' 'pipewire')
opt_deps_ubuntu=("${opt_deps_debian[@]}")
opt_deps_fedora=('xdg-utils' 'libappindicator-gtk3' 'ca-certificates' 'alsa-lib' 'pulseaudio-libs' 'mesa-libGL' 'xdg-desktop-portal' 'xdg-desktop-portal-gtk' 'pipewire')
opt_deps_arch=('xdg-utils' 'libappindicator-gtk3' 'ca-certificates' 'alsa-lib' 'pulseaudio' 'libglvnd' 'xdg-desktop-portal' 'xdg-desktop-portal-gtk' 'pipewire')
opt_deps_opensuse=('xdg-utils' 'libappindicator3-1' 'ca-certificates' 'libasound2' 'libpulse0' 'Mesa-libGL1' 'xdg-desktop-portal' 'xdg-desktop-portal-gtk' 'pipewire')
opt_deps_altlinux=('xdg-utils' 'libappindicator-gtk3' 'ca-certificates' 'libalsa' 'libGL' 'xdg-desktop-portal' 'xdg-desktop-portal-gtk' 'pipewire')

sources=(
	"https://td.telegram.org/tlinux/tsetup.${version}.tar.xz?~archive=false&~name=tsetup.tar.xz"
	'local:///org.telegram.desktop.desktop'
	'local:///telegram-desktop.png'
	'local:///LICENSE'
)

checksums=(
	'sha256:37e23ce08333340504623042120b826f7a2a5650c831187219ab9c520740eb42'
	'sha256:8ceb3f6aea1003f84f1b5757cd857f8237b294bb18456e5e0e54d96e6fff04ba'
	'sha256:3fb1400c7dc9bbc3b5cb3ffedcbf4a9b09c53e28b57a7ff33a8a6b9048864090'
	'sha256:f3ad1b5ecdc1c9e5ab3719a0b084a3e0b4fc87ad424fbf81043df9637be3d1f5'
)

scripts=(
	['postinstall']='postinstall.sh'
	['postremove']='postremove.sh'
)

package() {
	cd "${srcdir}"

	tar --no-same-owner --no-same-permissions -xJf tsetup.tar.xz

	install -dm755 "${pkgdir}/opt/telegram-desktop"
	install -Dm755 "Telegram/Telegram" "${pkgdir}/opt/telegram-desktop/Telegram"

	install -dm755 "${pkgdir}/usr/bin"
	ln -sfn /opt/telegram-desktop/Telegram "${pkgdir}/usr/bin/telegram-desktop"

	# Telegram's own Launcher::ComputeExternalUpdater() (SourceFiles/core/
	# launcher.cpp) looks for a file under <exe-dir>/externalupdater.d/ that
	# contains a line matching its own full executable path; if found, it
	# calls SetUpdaterDisabledAtStartup(), which is the *documented* way for
	# distro/repo packagers to tell Telegram "I manage updates, not you".
	# This one flag fixes two things at once: it makes InstallLauncher() skip
	# self-writing a shadow ~/.local/share/applications/*.desktop and D-Bus
	# .service file on every launch, and it makes QGuiApplication's
	# desktopFileName() (the Wayland app_id used to match the running window
	# to an icon) use the plain "org.telegram.desktop" id instead of a
	# per-install-path hashed one — which is what was breaking the
	# taskbar/window icon even after the shadow-file duplicate was silenced.
	install -Dm644 /dev/stdin \
		"${pkgdir}/opt/telegram-desktop/externalupdater.d/nivora" <<'EOF'
/opt/telegram-desktop/Telegram
EOF

	install-desktop org.telegram.desktop.desktop
	install -Dm644 telegram-desktop.png \
		"${pkgdir}/usr/share/icons/hicolor/256x256/apps/org.telegram.desktop.png"

	install-license LICENSE "telegram/LICENSE.nivora"
}

files() {
	files-find-binary
	files-find-desktop
	files-find-license
	files-find \
		"/opt/telegram-desktop/**/*" \
		"/usr/share/icons/hicolor/256x256/apps/org.telegram.desktop.png"
}
