#!/bin/bash
# Black        0;30     Dark Gray     1;30
# Red          0;31     Light Red     1;31
# Green        0;32     Light Green   1;32
# Brown/Orange 0;33     Yellow        1;33
# Blue         0;34     Light Blue    1;34
# Purple       0;35     Light Purple  1;35
# Cyan         0;36     Light Cyan    1;36
# Light Gray   0;37     White         1;37

RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'
BROWN='\033[0;33m'

OPMONVERSION=10

REPO="http://repo.opservices.com/rpms/latest/install/8/opmon-release.noarch.rpm"
LOCALREPO="http://localrepo.opservices.com/rpms/latest/install/8/opmon-localrepo.noarch.rpm"

function _spinner() {
    local on_success="DONE"
    local on_fail="FAIL"
    local white="\e[1;37m"
    local green="\e[1;32m"
    local red="\e[1;31m"
    local nc="\e[0m"

    case $1 in
        start)
            let column=${#2}-${3}
            echo -ne ${2}
            printf "%${column}s"

            i=1
            sp='\|/-'
            delay=${SPINNER_DELAY:-0.15}

            while :
            do
                printf "\b${sp:i++%${#sp}:1}"
                sleep $delay
            done
            ;;
        stop)
            if [[ -z ${3} ]]; then
                echo "spinner is not running.."
                exit 1
            fi

            kill $3 > /dev/null 2>&1

            echo -en "\b["
            if [[ $2 -eq 0 ]]; then
                echo -en "${green}${on_success}${nc}"
            else
                echo -en "${red}${on_fail}${nc}"
            fi
            echo -e "]"
            ;;
        *)
            echo "invalid argument, try {start/stop}"
            exit 1
            ;;
    esac
}

function start_spinner {
    _spinner "start" "${1}" "${2}"&
    _sp_pid=$!
    disown
}

function stop_spinner {
    _spinner "stop" $1 $_sp_pid
    unset _sp_pid
}

VERSIONID=$(sed -nE 's/^VERSION_ID="([^"]+)".*/\1/p' /etc/os-release)
VERSION=$(echo $VERSIONID | cut -d'.' -f1)

if [[ $VERSION -gt 8 ]]; then
   echo "OS Version not supported. Only EL8, RHEL8 and OL8."
   exit;
fi

setenforce 0
sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

if [[ $(getenforce) == "Enforcing" ]]; then
    echo -e "   SELINUX is ${RED}ENABLE${NC}. To proceed with the installation it is necessary to be ${GREEN}DISABLED${NC}."
    exit
fi

`systemctl stop firewalld 2>&1`
`systemctl disable firewalld 2>&1`

export DNF0=$OPMONVERSION

echo
echo -e "   Welcome to the ${BLUE}OpMon${NC} platform installer."

cat << "EOF"

      Wait for the        ( ( (
         installation      ) ) )
        process           ........_
         to finish        |      |_]
        and for now        \    /
          I have a          '--'
EOF

echo -e "          ${BROWN}coffee${NC}."
echo

if ! [ -L "/usr/local" ] && [ -e "/usr/local" ]; then
    mv /usr/local/* /var/local/
    rm -rf /usr/local
    ln -s --force /var/local/ /usr/local
fi

start_spinner "*\tSetting version ${BLUE}$OPMONVERSION${NC}." 7
if [[ ! $(grep "export DNF0=$OPMONVERSION" /etc/profile) ]]; then
    dnf=$(echo "export DNF0=$OPMONVERSION" >> /etc/profile)
fi
stop_spinner $?

if [[ $(rpm -qa | grep opmon-localrepo | wc -l) == 1 ]]; then
    cmd=$(rpm -e opmon-localrepo 2>&1)
    code=$?
    if [[ $code != 0 ]]; then
        echo
        echo $cmd
        echo
    fi
fi

if [[ ! $(rpm -qa | grep opmon-release | wc -l) == 1 ]]; then
    start_spinner "*\tInstalling repositories." 0
    installRepo=$(rpm -ivh $REPO 2>&1)
    code=$?
    stop_spinner $code
    if [[ $code != 0 ]]; then
        echo
        echo $installRepo
        echo
    fi
else
    start_spinner "*\tInstalling repositories." 0
    installRepo=$(rpm -Uvh --replacepkgs $REPO 2>&1)
    code=$?
    stop_spinner $code
    if [[ $code != 0 ]]; then
        echo
        echo $installRepo
        echo
    fi
fi

start_spinner "*\tInstalling dependencies." 0
OS=$(sed -nE 's/^ID="([^"]+)".*/\1/p' /etc/os-release)

NODEJSD=$(dnf module disable nodejs:10 -y)
NODEJSE=$(dnf module enable nodejs:18 -y)

if [[ $OS == "centos" ]]; then
    enablePt=$(dnf config-manager --set-enabled powertools 2>&1)
fi

if [[ $OS == "rhel" ]]; then
    enablePt=$(subscription-manager repos --enable codeready-builder-for-rhel-8-x86_64-rpms 2>&1)
fi

if [[ $OS == "ol" ]]; then
    installOL=$(dnf install dnf-plugins-core oracle-epel-release-el8 -y 2>&1)
    enablePt=$(yum-config-manager --set-enabled ol8_codeready_builder 2>&1)
fi

gpgMariadb=$(rpm --import https://yum.mariadb.org/RPM-GPG-KEY-MariaDB 2>&1)
cleanAll=$(dnf clean all 2>&1)
cleanPackages=$(dnf clean packages 2>&1)
#InstallDepsRepos=$(dnf install remi-release epel-release rpmfusion-free-release okay-release rpmfusion-nonfree-release -y 2>&1)
InstallDepsRepos=$(dnf install remi-release epel-release rpmfusion-free-release rpmfusion-nonfree-release -y 2>&1)
phpInstall=$(dnf module install php:remi-7.4 -y 2>&1)
code=$?
stop_spinner $code
if [[ $code != 0 ]]; then
    echo
    echo $gpgMariadb
    echo $enablePt
    echo $installOL
    echo $cleanAll
    echo $cleanPackages
    echo $InstallDepsRepos
    echo $phpInstall
    echo
    exit
fi

start_spinner "*\tInstalling ${BLUE}OpMon${NC}." 3
InstallOpMon=$(dnf groupinstall opmon -y 2>&1)
code=$?
stop_spinner $code
if [[ $code != 0 ]]; then
    echo
    echo -e "   Check if there is access to the internet and the repositories or if the ${BLUE}OpMon${NC} platform is not already installed."
    echo
    echo $InstallOpMon
    exit
fi

echo
echo -n -e "   You can now manage ${BLUE}OpMon${NC} from the address of this server where it was installed, ${GREEN}http://"
echo -n $(ip addr | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1')
echo -e "${NC}."
echo -e "   In case of doubts, you can access our documentation, ${GREEN}https://kb.opservices.com${NC}."
echo "   See you later."
echo
