#!/bin/bash

txtrst=$(tput sgr0) # Text reset
txtred=$(tput setab 1) # Red Background
textpurple=$(tput setab 5) #Purple Background
txtblue=$(tput setab 4) #Blue Background
txtgreen=$(tput bold ; tput setaf 2) # GreenBold
txtyellow=$(tput bold ; tput setaf 3) # YellowBold
iplocal=$(ifconfig  | grep 'inet addr' | awk '{print $2}' | cut -d ':' -f2 |grep -v 127)

# FUNCTION: $OS - $VER
distro(){
if [ -f /etc/lsb-release ]; then
    . /etc/lsb-release
        if [ $DISTRIB_ID == $OS - $VER ]; then
            if [ $DISTRIB_RELEASE != "$OS - $VER" ]; then
                error
            fi
        else
            error
        fi
fi
}

# FUNCTION: ERROR
error(){
    sleep 2
    echo -ne '\n'"${txtyellow}--PROBLEMAS!--${txtrst}"
    echo -ne '\n'"${txtred}Support: csjhost@gmail.com${txtrst}" '\n'
exit
}





echo "${txtblue}++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++${txtrst}"
echo "${txtgreen}               INSTALANDO RECURSOS${txtrst}"
echo "${txtblue}++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++${txtrst}"



apt-get update
apt-get upgrade

apt-get install unzip unrar -y > /dev/null 2>&1





echo -e "\033[1;31m              Aguarde o processo e demorado! \033[0m"






echo ""
echo "${txtblue}++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++${txtrst}"
echo "${txtgreen}[+]                INSTALANDO O JAVA${txtrst}"
echo "${txtblue}++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++${txtrst}"



tput setaf 3 ; tput bold ; read -n 1 -s -p "Aperte qualquer tecla para continuar..." ; echo "" ; echo "" ; tput sgr0
apt-get update > /dev/null 2>&1
apt-get upgrade -y > /dev/null 2>&1
sleep 1s
apt-get install unzip unrar -y > /dev/null 2>&1
sleep 1s
apt-get install python-software-properties software-properties-common -y > /dev/null 2>&1
apt-get install default-jdk -y > /dev/null 2>&1
apt-get update > /dev/null 2>&1
cd /tmp
wget http://montagemcsp.dyndns.org/csp/jdk-8u162-linux-x64.tar.gz > /dev/null 2>&1
tar -xvzf jdk-8u162-linux-x64.tar.gz > /dev/null 2>&1
mv jdk1.8.0_162 /jre-oracle > /dev/null 2>&1
mv /jre-oracle /usr/lib > /dev/null 2>&1
update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jre-oracle/bin/java" 1 > /dev/null 2>&1
update-alternatives --set java /usr/lib/jre-oracle/bin/java > /dev/null 2>&1


tput setaf 3 ; tput bold ; read -n 1 -s -p "Aperte qualquer tecla para continuar..." ; echo "" ; echo "" ; tput sgr0



echo "${txtblue}++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++${txtrst}"
echo "${txtgreen}[+] ATUALIZANDO O SISTEMA${txtrst}"
echo "${txtblue}++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++${txtrst}"

apt-get update 
apt-get upgrade 



echo "${txtblue}++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++${txtrst}"
echo "${txtgreen}[+] MODIFICANDO HORA DO SISTEMA${txtrst}"
echo "${txtblue}++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++${txtrst}"

rm /etc/localtime
ln -s /usr/share/zoneinfo/America/Sao_Paulo /etc/localtime




echo "${txtblue}++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++${txtrst}"
echo "${txtgreen}[+] INSTALANDO SEU CSP ${txtrst}"
echo "${txtblue}++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++${txtrst}"



cd /usr/local
wget http://montagemcsp.dyndns.org/csp/csp.zip
cd /usr/local
unzip csp.zip
chmod 775 -R /usr/local/csp/
rm -r csp.zip
sleep 5


echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"


echo "[+] LIGANDO CSP"
#ligar cardproxy
/usr/local/csp/cardproxy.sh restart
/usr/local/csp/cardproxy.sh start
./csp/cardproxy.sh start
sleep 5
echo "CONFIGURANDO CRONTAB "
cd /etc/
rm -r crontab
wget http://montagemcsp.dyndns.org/csp/crontab

sleep 5
echo "CONFIGURANDO START AUTOMATICO "
cd /root
wget http://montagemcsp.dyndns.org/csp/inicio
wget http://montagemcsp.dyndns.org/csp/restart
wget http://montagemcsp.dyndns.org/csp/parar
chmod 775 -R inicio
chmod 775 -R restart
chmod 775 -R parar

sleep 5



echo "COMANDO PARA START AUTOMATICO CSP"

sed --in-place '/exit 0/d' /etc/rc.local
echo "sleep 10" >> /etc/rc.local
echo "/usr/local/csp/cardproxy.sh start &" >> /etc/rc.local
echo "exit 0" >> /etc/rc.local


echo "${txtblue}++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++${txtrst}"
echo "${txtgreen}[+]          CSP  INSTALADO COM SUCESSO${txtrst}"
echo "${txtblue}++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++${txtrst}"



reboot