#!/bin/bash

mv /etc/apt/sources.list /etc/apt/sources.list.old

echo "deb http://ftp.br.debian.org/debian/ stretch main contrib non-free" > /etc/apt/sources.list
echo "deb-src http://ftp.br.debian.org/debian/ stretch main contrib non-free" >> /etc/apt/sources.list
echo "deb http://security.debian.org/debian-security stretch/updates main contrib non-free" >> /etc/apt/sources.list
echo "deb-src http://security.debian.org/debian-security stretch/updates main contrib non-free" >> /etc/apt/sources.list
echo "deb http://ftp.br.debian.org/debian/ stretch-updates main contrib non-free" >> /etc/apt/sources.list
echo "deb-src http://ftp.br.debian.org/debian/ stretch-updates main contrib non-free" >> /etc/apt/sources.list
rm /var/lib/apt/lists/lock 2>&1 > /dev/null 2>&1
rm /var/lib/dpkg/lock 2>&1 > /dev/null 2>&1
rm -r /var/cache/apt/archives/*.deb 2>&1 > /dev/null 2>&1
rm -r /var/cache/apt/archives/partial/* 2>&1 > /dev/null 2>&1
rm -r /var/lib/apt/lists/* 2>&1 > /dev/null 2>&1

apt-get update && apt-get install -y git libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev libjansson-dev libpython-dev make build-essential zlib1g-dev libcrypto++-dev libgcrypt11-dev 

cd /tmp && git clone --recursive https://github.com/vysheng/tg.git && cd tg && ./configure --disable-openssl && make

mkdir /opt/telegram && cp bin/telegram-cli tg-server.pub /opt/telegram/ && ln -s /opt/telegram/telegram-cli /usr/bin/

mv /etc/apt/sources.list.old /etc/apt/sources.list && apt-get update
