#!/bin/bash
#Instalação do UNIFI 7.5.187
##dnf install java-11-openjdk-headless
##dnf install java-17-openjdk-headless
##dnf install java-latest-openjdk
#https://repology.org/project/unifi/information
####################################################################################
#Variaveis de instalação
RED='\033[0;31m'
NC='\033[0m' # No Color
DT=$(date +%d%m%Y_%H%M%S)

####################################################################################
disable_selinux(){
        sed -i /etc/selinux/config -r -e 's/^SELINUX=.*/SELINUX=disabled/g'
}
stoping_service(){
        /usr/bin/systemctl stop unifi
}
install_components(){
        #yum -q -y install epel-release java-1.8.0-openjdk unzip wget
        yum -q -y install java-17-openjdk-headless 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
}
backup_unifi(){
        cp -r /opt/UniFi /opt/UniFi_bkp_sys_$DT
        mongodump --port 27117 --db ace --out /opt/unifi_bkp_db_$DT
}
install_unifi(){
        #wget https://dl.ui.com/unifi/7.3.83/UniFi.unix.zip -P /opt -q
        wget https://dl.ui.com/unifi/7.5.187/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
}
creating_service_unifi(){
        cat >/etc/systemd/system/unifi.service<<EOF
[Unit]
Description=UniFi Network Application
After=local-fs.target remote-fs.target network-online.target
Wants=network-online.target

[Service]
Type=simple
User=unifi
WorkingDirectory=/usr/share/unifi
EnvironmentFile=-/etc/sysconfig/unifi
Environment=JAVA_HOME=/usr/lib/jvm/jre-17
ExecStart=/usr/lib/jvm/jre-17/bin/java -Xmx1024M --add-opens=java.base/java.time=ALL-UNNAMED $JAVA_OPTS -jar /usr/share/unifi/lib/ace.jar start
ExecStop=/usr/lib/jvm/jre-17/bin/java $JAVA_OPTS -jar /usr/share/unifi/lib/ace.jar stop
SuccessExitStatus=143
Restart=on-success

[Install]
WantedBy=multi-user.target
EOF
}
restoring_db(){
    mongorestore --port 27117 --db ace --drop /opt/unifi_bkp_db_$DT
}
starting_service_unifi(){
        systemctl enable unifi.service --quiet
        systemctl start unifi.service --quiet
}
stty echo
echo -e "${RED}Iniciando Instalação${NC}"
echo -e "${RED}Fazendo BKP do UNIFI${NC}"
backup_unifi
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}Parando UniFi Antigo${NC}"
stoping_service
echo -e "${RED}Instalando UniFi${NC}"
install_unifi
echo -e "${RED}Configurando serviço${NC}"
creating_service_unifi
echo -e "${RED}Restaurando DB${NC}"
restoring_db
echo -e "${RED}Iniciando Unifi${NC}"
starting_service_unifi
echo -e "${RED}Instalação Concluida${NC}"
