GREEN='\033[0;32m'
NC='\033[0m' # No Color

### folders ###
mkdir -p /root/alireza/cache
mkdir -p /root/source
mkdir -p /cache/tmp
## activate rc.local 
bash <(curl -s http://conf.sitegozar.com/dl/debian/10_buster/rc-local.txt)

## install packages
wget -O /etc/apt/sources.list http://conf.sitegozar.com/dl/debian/10_buster/sources.list
apt-get update
yes | apt-get install -y screen git aria2 curl wget apt-transport-https dirmngr zip unzip atop htop vnstat dnsutils whois nmap build-essential tree software-properties-common ncdu parted ifenslave lftp xfsprogs perl libperl-dev libgd3 libgd-dev libgeoip1 libgeoip-dev geoip-bin libxml2 libxml2-dev libxslt1.1 libxslt1-dev gcc libssl-dev make libpcre3-dev zlib1g-dev libxslt-dev autoconf libmaxminddb-dev psmisc dnsutils iftop mlocate nocache rsync proftpd snmpd python3-certbot-dns-cloudflare ethtool

#configure proftpd,snmpd,vnstat
wget -O /etc/proftpd/proftpd.conf http://conf.sitegozar.com/dl/debian/10_buster/proftpd.conf
systemctl restart proftpd

wget -O /etc/snmp/snmpd.conf http://conf.sitegozar.com/dl/debian/10_buster/snmpd.conf
systemctl restart snmpd

#vnstat stuff
sed -i 's/MaxBandwidth\ 1000/MaxBandwidth\ 0/g' /etc/vnstat.conf
if [[ $(ip link show up | grep mtu | awk {'print $2'} | grep -vi 'lo' | sed 's/\://g') = *bond0* ]]; then 
		eth=bond0
		sed -i 's/eth0/bond0' /etc/vnstat.conf
else
        eth=$(ip link show up | grep mtu | awk {'print $2'} | grep -vi 'lo\|bond' | sed 's/\://g' | awk 'FNR <= 1')
        sed -i "s/eth0/$eth/" /etc/vnstat.conf
fi
vnstat -u -i $eth

#write out current crontab
crontab -l > mycron
#echo new cron into cron file
echo "*/5 * * * * vnstat -u -i $eth" >> mycron
#install new cron file
crontab mycron
rm mycron

systemctl restart vnstat



#configure sysctl
curl 'http://conf.sitegozar.com/dl/debian/10_buster/sysctl.conf' >> /etc/sysctl.conf
sysctl -p

#configure Disks
disk_info=$(fdisk -l | grep /dev | grep -i tib | awk {'print $2'} | sed 's/\://g' | sed ':a;N;$!ba;s/\n/,/g'); curl -d "disks=$disk_info"  http://conf.sitegozar.com/dl/debian/10_buster/disk.php -o /root/alireza/disks.sh
echo "### Configure Disks ###" >> /etc/rc.local
echo "sh /root/alireza/disks.sh" >> /etc/rc.local

disk_info=$(fdisk -l | grep /dev | grep -i tib | awk {'print $2'} | sed 's/\://g' | sed ':a;N;$!ba;s/\n/,/g'); curl -d "disks=$disk_info" 'http://conf.sitegozar.com/dl/debian/10_buster/disk.php?fstab=yes' >> /etc/fstab


#configure IRQ balance
wget -O /root/alireza/set_irq_affinity.sh http://conf.sitegozar.com/dl/debian/10_buster/set_irq_affinity.sh
chmod +x /root/alireza/set_irq_affinity.sh
echo "### Configure IRQ on interfaces ###" >> /etc/rc.local
(ip link show up | grep mtu | awk {'print $2'} | grep -vi 'lo\|bond0' | sed 's/\://g') | while read line; do echo "/root/alireza/set_irq_affinity.sh local $line" >> /etc/rc.local ;done

#nginx stuff
yes | apt-get install -y nginx 
yes | apt-get remove -y nginx nginx-full
cd /root/source

git clone https://github.com/leev/ngx_http_geoip2_module /root/source/ngx_http_geoip2_module

wget $(curl -L http://conf.sitegozar.com/dl/debian/10_buster/nginx.php) -P /root/source
tar -zxf nginx*
cd "$(find nginx* | head -1)"
./configure --prefix=/usr --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-pcre --with-file-aio --with-threads --with-http_realip_module --user=www-data --group=www-data --with-http_stub_status_module --with-http_gzip_static_module --with-http_secure_link_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --add-module=/root/source/ngx_http_geoip2_module/
make
make install

mkdir -p /etc/nginx/vh/
mkdir -p /etc/nginx/config/
mkdir -p /etc/letsencrypt/live/ftk.pw
#~download VH Example file
wget -O /etc/nginx/vh/ http://conf.sitegozar.com/dl/debian/10_buster/nginx/vh/ftm_14.conf

wget -O /etc/nginx/nginx.conf http://conf.sitegozar.com/dl/debian/10_buster/nginx/nginx.conf
wget -O /etc/nginx/config/config.zip http://conf.sitegozar.com/dl/debian/10_buster/nginx/config.zip
unzip /etc/nginx/config/config.zip -d /etc/nginx/config/
rm -rf /etc/nginx/config/config.zip

#~download SSL
wget -O /etc/letsencrypt/live/ftk.pw/fullchain.pem http://certbot.sitegozar.com/ssl/ftk.pw/fullchain.pem
wget -O /etc/letsencrypt/live/ftk.pw/privkey.pem http://certbot.sitegozar.com/ssl/ftk.pw/privkey.pem


#~download fresh maxmindDB
wget -O /etc/nginx/config/GeoLite2-Country.mmdb http://certbot.sitegozar.com/geo/GeoLite2-Country.mmdb

#~remove trash folders
mkdir /etc/nginx/trash
mv -f /etc/nginx/modules-enabled/ /etc/nginx/trash
mv -f /etc/nginx/snippets/ /etc/nginx/trash
mv -f /etc/nginx/sites-available/ /etc/nginx/trash
mv -f /etc/nginx/sites-enabled/ /etc/nginx/trash
mv -f /etc/nginx/conf.d/ /etc/nginx/trash

#install cache system
yes | apt -y install php7.3
wget -O /root/alireza/cache/cache.zip http://conf.sitegozar.com/dl/debian/10_buster/nginx/cache.zip
unzip /root/alireza/cache/cache.zip -d /root/alireza/cache/
rm -rf /root/alireza/cache/cache.zip

#cloudflare stuff 
mkdir -p /root/alireza/certbot
wget -O /root/alireza/certbot/cloudflare_ush.txt http://conf.sitegozar.com/dl/debian/10_buster/cloudflare_ush.txt

#prompet stuff
wget -O /root/.bashrc http://conf.sitegozar.com/dl/debian/10_buster/bashrc

#change timezone
timedatectl set-timezone Asia/Tehran

#download help file
wget -O /root/help.txt http://conf.sitegozar.com/dl/debian/10_buster/help.txt

printf "${GREEN}*** Completed ***${NC}\n"
#trashgan
#return /dev/sda1 stend of /dev/sda
#fdisk -l | awk {'print $1,$5'} | grep dev | grep -i t | awk {'print $1'} | sed ':a;N;$!ba;s/\n/,/g'