#!/bin/bash
# First check if Telegram is downloaded or not
if [ -f "~/.TelegramDesktop/application/Telegram" ]
then
	# If installed then just run it
	~/.TelegramDesktop/application/Telegram
else
	# We need to downoad it from telegram.org, for that we need to know the arch we are running on
	arch=$(uname -m)
	tsuffix=''
	if [ "$arch" != "x86_64" ]
	then
		tsuffix='32'
	fi
	
	# Let's download Telegram
	mkdir -p ~/.TelegramDesktop
	wget https://tdesktop.com/linux$tsuffix -O ~/.TelegramDesktop/telegram 2>&1 | sed -u 's/.* \([0-9]\+%\)\ \+\([0-9.]\+.\) \(.*\)/\1\n# Downloading at \2\/s, ETA \3/' | zenity --progress --auto-close --auto-kill --title="Downloading..."

	# Now we just unpack it and run
	cd ~/.TelegramDesktop && tar xf ~/.TelegramDesktop/telegram | zenity --progress --auto-close --auto-kill --text="Unpacking..." --pulsate && rm -f ~/.TelegramDesktop/telegram && mv Telegram application && ~/.TelegramDesktop/application/Telegram
fi
