#/bin/bash

fun_bar () {
comando[0]="$1"
comando[1]="$2"
 (
[[ -e $HOME/fim ]] && rm $HOME/fim
${comando[0]} -y > /dev/null 2>&1
${comando[1]} -y > /dev/null 2>&1
touch $HOME/fim
 ) > /dev/null 2>&1 &
 tput civis
echo -ne "\033[1;33mAGUARDE \033[1;37m- \033[1;33m["
while true; do
   for((i=0; i<18; i++)); do
   echo -ne "\033[1;31m#"
   sleep 0.1s
   done
   [[ -e $HOME/fim ]] && rm $HOME/fim && break
   echo -e "\033[1;33m]"
   sleep 1s
   tput cuu1
   tput dl1
   echo -ne "\033[1;33mAGUARDE \033[1;37m- \033[1;33m["
done
echo -e "\033[1;33m]\033[1;37m -\033[1;32m OK !\033[1;37m"
tput cnorm
}

cd $HOME
clear
if [ $(id -u) -eq 0 ]
then
	clear
else
	if echo $(id) |grep sudo > /dev/null 2>&1
	then
	clear
	echo -e "\033[1;37mVoce não é root"
	echo -e "\033[1;37mSeu usuario esta no grupo sudo"
	echo -e "\033[1;37mPara virar root execute \033[1;31msudo su\033[1;37m ou execute \033[1;31msudo $0\033[0m"
	exit
	else
	clear
	echo -e "Vc nao esta como usuario root, nem com seus direitos (sudo)\nPara virar root execute \033[1;31msu\033[0m e digite sua senha root"
	exit
	fi
fi

function sshd_config(){ echo "Port 22
Protocol 2
KeyRegenerationInterval 3600
ServerKeyBits 1024
SyslogFacility AUTH
LogLevel INFO
LoginGraceTime 120
PermitRootLogin yes
StrictModes yes
RSAAuthentication yes
PubkeyAuthentication yes
IgnoreRhosts yes
RhostsRSAAuthentication no
HostbasedAuthentication no
PermitEmptyPasswords no
ChallengeResponseAuthentication no
PasswordAuthentication yes
X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
UsePAM yes" > /etc/ssh/sshd_config
}

function apch2(){
    sed -i "s/Listen 80/Listen 81/g" /etc/apache2/ports.conf > /dev/null 2>&1
}

function inst_pct () {
_pacotes=("bc" "screen" "nano" "unzip" "man" "dos2unix" "nload" "htop" "jq" "curl" "python")
for _prog in ${_pacotes[@]}; do
    apt-get install $_prog -y > /dev/null 2>&1
done
}

clear
echo -e "\033[01;31mINICIANDO CONFIGURAÇÃO\033[0m"
echo ""

apch2
sshd_config
fun_bar 'inst_pct'

mkdir /etc/VpsPackdir 2>/dev/null
mkdir /etc/VpsPackdir/limite 2>/dev/null
mkdir /etc/VpsPackdir/senha 2>/dev/null
rm -rf /bin/limite 2>/dev/null
wget -o /dev/null -O- https://raw.githubusercontent.com/twossh/vpspack/master/limite > /bin/limite
chmod +x /bin/limite
rm -rf /bin/criarusuario 2>/dev/null
wget -o /dev/null -O- https://raw.githubusercontent.com/twossh/vpspack/master/criarusuario > /bin/criarusuario
chmod +x /bin/criarusuario
rm -rf /bin/deletarusuario 2>/dev/null
wget -o /dev/null -O- https://raw.githubusercontent.com/twossh/vpspack/master/deletarusuario > /bin/deletarusuario
chmod +x /bin/deletarusuario
rm -rf /bin/redefinirusuario 2>/dev/null
wget -o /dev/null -O- https://raw.githubusercontent.com/twossh/vpspack/master/redefinirusuario > /bin/redefinirusuario
chmod +x /bin/redefinirusuario
rm -rf /bin/menu 2>/dev/null
wget -o /dev/null -O- https://raw.githubusercontent.com/twossh/vpspack/master/menu > /bin/menu
chmod +x /bin/menu
rm -rf /bin/speedtest.py 2>/dev/null
wget -o /dev/null -O- https://raw.githubusercontent.com/twossh/vpspack/master/speedtest.py > /bin/speedtest.py
chmod +x /bin/speedtest.py
rm -rf /bin/proxy.py 2>/dev/null
wget https://raw.githubusercontent.com/twossh/vpspack/master/proxy.py -O /bin/proxy.py > /dev/null 2>&1
chmod +x /bin/proxy.py
rm -rf /bin/badvpn-udpgw 2>/dev/null
wget -O /bin/badvpn-udpgw https://github.com/twossh/vpspack/blob/master/badvpn-udpgw?raw=true -o /dev/null
chmod +x /bin/badvpn-udpgw
rm -rf /etc/autostart 2>/dev/null
wget -o /dev/null -O- https://raw.githubusercontent.com/twossh/vpspack/master/autostart > /etc/autostart
chmod +x /etc/autostart
[[ $(crontab -l|grep -c "autostart") = '0' ]] && (crontab -l 2>/dev/null; echo -e "@reboot /etc/autostart\n* * * * * /etc/autostart") | crontab -
service apache2 restart > /dev/null 2>&1
service ssh restart > /dev/null 2>&1
service apache2 restart > /dev/null 2>&1
clear
echo -e "\033[1;31mINSTALACAO CONCLUIDA\n\n\033[1;31mCOMANDO PRINCIPAL: \033[1;33mmenu\033[0m"
rm -rf install