#!/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 ""
echo -e "\033[1;31m Definindo Data/Hora. \033[0m"
echo ""
echo -e "\033[1;31m Selecionado SAO PAULO. \033[0m"
echo America/Sao_Paulo > /etc/timezone && rm /etc/localtime && ln -snf /usr/share/zoneinfo/America/Sao_Paulo /etc/localtime > /dev/null 2>&1
clear
echo ""

echo "            ${txtblue}++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++${txtrst}"
echo "                            ${txtgreen}SRV SISTEMAS SCRIPT DO CSP 2021 ${txtrst}"
echo "            ${txtblue}++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++${txtrst}"











echo ""
tput setaf 3 ; tput bold ; read -n 1 -s -p "Pressione ENTER para continuar" ; echo "" ; echo "" ; tput sgr0

             








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


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
cd /opt 
sleep 1s
wget wget http://montagemcsp.dyndns.org/csp/csp228opt.zip > /dev/null 2>&1
sleep 20s
tput setaf 3 ; tput bold ; read -n 1 -s -p "Aperte qualquer tecla para continuar..." ; echo "" ; echo "" ; tput sgr0
unzip csp228opt.zip > /dev/null 2>&1
sleep 1s
chmod -R 7777 /opt/iniciarcsp
sleep 1s
chmod 775 /opt/csp/. 
sleep 1s
chmod 775 /opt/csp/cardproxy.sh
sleep 1s
rm -rvf csp228opt.zip
sleep 1s
rm -rvf /root/installcsp
sleep 1s
cd /opt/csp
sleep 1s
chmod -R 7777 cardproxy.sh
sleep 1s
chmod -R 7777 cluster
sleep 1s
cd /opt/csp
sleep 1s
./cardproxy.sh start
clear
printf '%s\n' '#!/bin/bash' 'exit 0' | sudo tee -a /etc/rc.local
sed --in-place '/exit 0/d' /etc/rc.local
echo "cd /opt/csp" >> /etc/rc.local
echo "./cardproxy.sh start" >> /etc/rc.local
echo "chown root:syslog /var/log" >> /etc/rc.local
echo "exit 0" >> /etc/rc.local
systemctl enable rc-local > /dev/null 2>&1
cd /etc/systemd/system
echo -e "\033[1;31m Caso o arquivo rc.local.service já existir ele perguntara se quer substituir coloque a letra n e aperte enter. \033[0m"
wget http://montagemcsp.dyndns.org/csp/rc-local.zip > /dev/null 2>&1
unzip rc-local.zip > /dev/null 2>&1
rm rc-local.zip > /dev/null 2>&1
chmod +x /etc/systemd/system/rc-local.service > /dev/null 2>&1
chmod +x /etc/rc.local > /dev/null 2>&1
systemctl enable rc-local > /dev/null 2>&1
cd /root
rm installcsp > /dev/null 2>&1
sleep 1s



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




reboot