#!/usr/bin/env bash

info () {
    echo -e "\033[1m info:   \033[0m $@ ";
}

ok () {
    echo -e "\033[1m done:  \033[0m \033[32m $@ \033[0m";
}

error () {
    echo -e  "\033[1m ERROR:\033[0m \033[31m $@\033[0m";
}

#Check OS version
OsRelease=`cat /etc/system-release`

OsVersion=6
IsOsStream=0
IsOsOracle=0
IsOsAlmaLinux=0
IsVEOS=0

function TestVersion()
{
	local versionStr=$1

	for versionNum in {6..20}
	do
		if [[ $OsRelease == *"${versionStr} ${versionNum}"* ]]; then
			OsVersion=$versionNum
		fi
	done

	if [[ $OsRelease == *"Stream"* ]]; then
    IsOsStream=1
  fi

  if [[ $OsRelease == *"Oracle"* ]]; then
    IsOsOracle=1
  fi

  if [[ $OsRelease == *"AlmaLinux"* ]]; then
    IsOsAlmaLinux=1
  fi

  if [[ $OsRelease == *"VEOS"* ]]; then
      IsVEOS=1
  fi
}

TestVersion "CentOS Linux release"
TestVersion "CentOS Stream release"
TestVersion "Oracle Linux Server release"
TestVersion "Red Hat Enterprise Linux release"
TestVersion "AlmaLinux release"
TestVersion "VEOS release"

if [ "$OsVersion" == 6 ]
then
  error "OS version is not compatible."
  exit
fi

if [ "$OsVersion" == 8 ] || [ "$OsVersion" == 7 ]
then
  if [ "$IsOsOracle" == 0 ] && [ "$IsOsAlmaLinux" == 0 ] && [ "$IsVEOS" == 0 ]
  then
    info "Switching repos for CentOs ${OsVersion}..."
    sed -i -e '/^mirrorlist=http:\/\//d' -e 's/^# *baseurl=http:\/\/mirror.centos.org/baseurl=https:\/\/vault.centos.org/' /etc/yum.repos.d/CentOS-*.repo
  fi
fi

sed -i -e '/^failovermethod=/d' /etc/yum.repos.d/*.repo

info "Disabling Selinux!"
sed -i 's/^\(SELINUX\s*=\s*\).*$/\1disabled/' /etc/selinux/config

#Configure repos
info "Configuring repos..."

rpm --import http://vasexperts.ru/centos/RPM-GPG-KEY-vasexperts.ru
rpm -Uvh http://vasexperts.ru/centos/6/x86_64/vasexperts-repo-1-0.noarch.rpm

yum install -y wget

MARIADB_REPO=/etc/yum.repos.d/mariadb.repo

if [ "$OsVersion" == 8 ]
then

    rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

    rpm --import http://rpms.remirepo.net/RPM-GPG-KEY-remi
    rpm -Uvh --nodeps https://rpms.remirepo.net/enterprise/remi-release-8.rpm
    #yum install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm

	  #yum config-manager --set-enabled PowerTools

    if [[ $IsOsOracle == 1 ]]; then
      yum config-manager --set-enabled ol8_codeready_builder
    else
      yum config-manager --set-enabled powertools
    fi

    yum module reset -y php
    yum module enable -y php:remi-7.2

	  echo "[mariadb-main]
name = MariaDB Server
baseurl = https://downloads.mariadb.com/MariaDB/mariadb-10.4/yum/rhel/\$releasever/\$basearch
gpgkey = https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck = 1
enabled = 1
module_hotfixes = 1" > $MARIADB_REPO

    yum install -y --disableexcludes=all kernel-headers

elif [ "$OsVersion" == 7 ]
then
	rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
	rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
	
	  echo "[mariadb-main]
name = MariaDB Server
baseurl = https://downloads.mariadb.com/MariaDB/mariadb-10.4/yum/rhel/\$releasever/\$basearch
gpgkey = https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck = 1
enabled = 1
module_hotfixes = 1" > $MARIADB_REPO

else

	rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
	rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm
	
	MACHINE_TYPE=`uname -m`
	if [ ${MACHINE_TYPE} == 'x86_64' ] 
	then
	  
	  echo "[mariadb]
name=MariaDB
baseurl=http://yum.mariadb.org/10.2/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1" > $MARIADB_REPO
	  
	else
	  echo "[mariadb]
name=MariaDB
baseurl=http://yum.mariadb.org/10.2/centos6-x86
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1" > $MARIADB_REPO
	fi


fi

ok "Finished Configuring repos."

#For web socket
info "Installing Node..."

yum install -y gcc-c++ make

if [ "$OsVersion" == 8 ]
then
#  yum install https://rpm.nodesource.com/pub_20.x/nodistro/repo/nodesource-release-nodistro-1.noarch.rpm -y

  if [ ! -f "/etc/redhat-release" ]; then
      ln -s "/etc/system-release" "/etc/redhat-release"
  fi

  curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -
else
  curl -sL https://rpm.nodesource.com/setup_14.x | sudo -E bash -
fi
sed -i -e '/^failovermethod=/d' /etc/yum.repos.d/*.repo

#For indexed search
info "Installing Sphinx..."
yum install -y postgresql-libs unixODBC
if [ "$OsVersion" == 8 ]
then
  #sphinx now in epel and remi dosen`t need anymore
  #wget http://rpms.remirepo.net/enterprise/8/remi/x86_64/
	#rpm -Uvh remi-release*rpm

	sudo yum install -y sphinx
#    wget https://vasexperts.ru/install/sphinx-2.3.2-1.rhel8.x86_64.rpm
#    sudo yum install -y sphinx-2.3.2-1.rhel8.x86_64.rpm
else
    wget --no-check-certificate https://vasexperts.ru/install/sphinx-2.3.2-1.rhel7.x86_64.rpm
    sudo yum install -y sphinx-2.3.2-1.rhel7.x86_64.rpm
fi

#add nginx user because it will not be autocreated
if [[ $IsVEOS == 1 ]]; then
  useradd nginx -s /bin/false
fi


#dpiui2 install
info "DPIUI2 package installing..."

if [ "$OsVersion" == 8 ]
then
    yum install -y dpiui2
else
    yum install -y dpiui2 --exclude=php-common*
fi

info "Finished DPIUI2 package installing."

rm -- "$0"
