#!/bin/bash
#Instalação do UNIFI 7.4.162
#links uteis https://repology.org/project/unifi/information
####################################################################################
#Variaveis de instalação
RED='\033[0;31m'
NC='\033[0m' # No Color

####################################################################################
disable_selinux(){
        sed -i /etc/selinux/config -r -e 's/^SELINUX=.*/SELINUX=disabled/g'
}
backup_unifi(){
        cp -r /opt/UniFi /opt/UniFi_bkp_$(date +%d%m%Y_%H%M%S)
}
backup_nxrelay(){
        cp -r /nxrelay /nxrelay_bkp_$(date +%d%m%Y)
}
clean_old_install(){
        /usr/bin/systemctl stop nxrelay
        /usr/bin/systemctl stop unifi
        yum erase -y -q java-1.8.0-openjdk-headless
}
install_components(){
        #yum -q -y install epel-release java-1.8.0-openjdk unzip wget
        yum -q -y install java-11-openjdk-devel epel-release unzip wget
}
configure_mongorepo(){
        cat >/etc/yum.repos.d/mongodb-org-5.0.repo<<EOF
[mongodb-org-5.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/5.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-5.0.asc
EOF
}
install_mongo(){
        yum install -q -y mongodb-org
}
creating_user(){
        adduser -r -s /bin/nologin ubnt
}
install_unifi(){
        #wget https://dl.ui.com/unifi/7.3.83/UniFi.unix.zip -P /opt -q
        wget https://dl.ui.com/unifi/7.4.162/UniFi.unix.zip -P /opt -q
        unzip -qq -o /opt/UniFi.unix.zip -d /opt
        chown -R ubnt:ubnt /opt/UniFi
        rm -rf /opt/UniFi.unix.zip
}
install_nxrelay(){
        mkdir /nxrelay
        cd /nxrelay
        /usr/bin/wget --quiet --no-check-certificate http://git.usb.org.br/infra/scripts/raw/master/nxrelay-2.7.8-adv.zip
        /usr/bin/zip -qq -d nxrelay-2.7.8-adv.zip "conf/cfg.properties" 
        /usr/bin/zip -qq -d nxrelay-2.7.8-adv.zip "conf/log4j.properties" 
        /usr/bin/zip -qq -d nxrelay-2.7.8-adv.zip "bin/startup.sh"
        /usr/bin/unzip -qq -o nxrelay-2.7.8-adv.zip
}
reconfigure_nxrelay(){
        cp /nxrelay_bkp_$(date +%d%m%Y)/conf/cfg.properties /nxrelay/conf/cfg.properties
        cp /nxrelay_bkp_$(date +%d%m%Y)/conf/log4j.properties /nxrelay/conf/log4j.properties
        cp /nxrelay_bkp_$(date +%d%m%Y)/bin/startup.sh /nxrelay/bin/startup.sh
}
creating_service_unifi(){
        cat >/etc/systemd/system/unifi.service<<EOF
#
# Systemd unit file for UniFi Controller
#

[Unit]
Description=UniFi AP Web Controller
After=syslog.target network.target

[Service]
Type=simple
User=ubnt
WorkingDirectory=/opt/UniFi
ExecStart=/usr/bin/java -Xmx1024M -jar /opt/UniFi/lib/ace.jar start
ExecStop=/usr/bin/java -jar /opt/UniFi/lib/ace.jar stop
SuccessExitStatus=143


[Install]
WantedBy=multi-user.target
EOF
}
creating_service_nxrelay(){
        cat >/etc/systemd/system/nxrelay.service<<EOF
#
# Systemd unit file for nxrelay
#

[Unit]
Description=NxRelay
After=network.target

[Service]
Type=simple
ExecStart=/bin/bash /nxrelay/bin/startup.sh
ExecStop=/bin/bash /nxrelay/bin/shutdown.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF
}
starting_services(){
        /usr/bin/systemctl enable unifi.service --quiet
        /usr/bin/systemctl start unifi.service --quiet
        /usr/bin/systemctl enable nxrelay --quiet
        /usr/bin/systemctl start nxrelay --quiet
}
stty echo
echo -e "${RED}Iniciando Instalação${NC}"
echo -e "${RED}Fazendo BKP do UNIFI${NC}"
backup_unifi
echo -e "${RED}Fazendo BKP do NXRELAY{NC}"
backup_nxrelay
echo -e "${RED}Limpando Instalações antigas{NC}"
clean_old_install
echo -e "${RED}Desabilitando SE Linux${NC}"
disable_selinux
echo -e "${RED}Instalando Componentes${NC}"
install_components
configure_mongorepo
echo -e "${RED}Instalando MongoDB${NC}"
install_mongo
echo -e "${RED}Criando Usuário${NC}"
creating_user
echo -e "${RED}Instalando UniFi${NC}"
install_unifi
echo -e "${RED}Instalando Nxrelay{NC}"
install_nxrelay
echo -e "${RED}Configurando serviços${NC}"
creating_service_unifi
creating_service_nxrelay
echo -e "${RED}Reconfigurando Nxrelay${NC}"
reconfigure_nxrelay
echo -e "${RED}Iniciando Unifi e Nxrelay{NC}"
starting_services
echo -e "${RED}Instalação Concluida${NC}"
