#!/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=11

REPO="http://repo.opservices.com/rpms/latest/"

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
}

curl -s "$REPO/install/9/repos/opmon.repo" -o /etc/yum.repos.d/opmon.repo
curl -LsS "https://r.mariadb.com/downloads/mariadb_repo_setup" | bash > /dev/null 2>&1

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

if [[ $VERSION -gt 9 ]]; then
   echo "OS Version not supported. Only EL9, RHEL9 and OL9."
   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 $?

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)
INITSCRIPTS=$(dnf install initscripts -y)

if [[ $OS == "centos" || $OS == "almalinux" || $OS == "rocky" ]]; then
    enableRepo=$(dnf config-manager --set-enabled crb)
    installEpel=$(dnf install epel-release -y)
fi

if [[ $OS == "rhel" ]]; then
    enableRepo=$(subscription-manager repos --enable codeready-builder-for-rhel-9-x86_64-rpms)
    installEpel=$(dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm -y)
fi

if [[ $OS == "ol" ]]; then
    enableRepo=$(dnf config-manager --set-enabled ol9_codeready_builder)
    installEpel=$(dnf install oracle-epel-release-el9 -y)
fi

oracleBasic=$(dnf install https://download.oracle.com/otn_software/linux/instantclient/1919000/oracle-instantclient19.19-basic-19.19.0.0.0-1.el9.x86_64.rpm -y)
oracleSqlplus=$(dnf install https://download.oracle.com/otn_software/linux/instantclient/1919000/oracle-instantclient19.19-sqlplus-19.19.0.0.0-1.el9.x86_64.rpm -y)
gpgOpMon=$(rpm --import http://repo.opservices.com/rpms/latest/install/repos/RPM-GPG-KEY-OPSERVICES)
cleanAll=$(dnf clean all 2>&1)
cleanPackages=$(dnf clean packages 2>&1)
remi=$(dnf install https://rpms.remirepo.net/enterprise/remi-release-9.rpm -y)
rpmfusionFree=$(dnf install https://mirrors.rpmfusion.org/free/el/rpmfusion-free-release-9.noarch.rpm -y)
rpmfusionNonfree=$(dnf install https://mirrors.rpmfusion.org/nonfree/el/rpmfusion-nonfree-release-9.noarch.rpm -y)
phpInstall=$(dnf module install php:remi-7.4 -y)
installExtras=$(dnf install perl-DBD-MySQL -y)
code=$?
stop_spinner $code
if [[ $code != 0 ]]; then
    echo
    echo "$gpgOpMon \n"
    echo "$INITSCRIPTS \n"
    echo "$enableRepo \n"
    echo "$installEpel \n"
    echo "$cleanAll \n"
    echo "$cleanPackages \n"
    echo "$remi \n"
    echo "$rpmfusionFree \n"
    echo "$rpmfusionNonfree \n"
    echo "$phpInstall \n"
    echo "$installExtras \n"
    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
