#!/bin/bash

#############################################
#    USAGE:
#         sudo bash installCommon.sh [otpions]
#    or make this file executable:
#         chmode +x installCommon.sh
#    and run it:
#         sudo ./installCommon.sh [otpions]
#############################################
USAGE=`cat << EOF
usage: 
installCommon.sh [otpions]   
where  otpions is:
 --provider  <database_provider>        type of database provider (MySql or MsSql) default MySql
 --logfile   <path_to_logfile>  path to log file. Default no log
EOF
`
PROVIDER="MySql"
LOGFILE="/dev/null"

while [ -n "$1" ]
do
        if [ -z "$2" ]
        then
            echo "ERROR! no values specified after "$1" !" | tee -a $LOG_FILE
            echo "$USAGE"
            exit 1
        fi
        param="$2"

        case "$1" in

                --provider)
                        PROVIDER=$param
                        #PROVIDER=${param,,}  # To lover case
                        shift ;;
                --logfile)
                        LOGFILE=$param
                        shift ;;

                --) shift
                break ;;
                *) echo "ERROR! $1 is not an option"
                   echo "$USAGE"
                   exit 1
        esac
        shift
done


if [ "$PROVIDER" != "MySql" ] && [ "$PROVIDER" != "MsSql" ] ; then
    echo echo "ERROR! $PROVIDER is not a valid database!"
    exit 1
fi


echo "PROVIDER = $PROVIDER"
MSSQL_PID="express"

####################################################################
# Centos 7
####################################################################
install_CenOS_7(){

echo updating system...
yum update -y

yum install epel-release wget git libgdiplus libicu jq gssntlmssp -y

#disabling SELinux:
sed  -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux
sed  -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config 

setenforce 0

# Firewall Configuration:
if systemctl list-unit-files | grep -Fq firewalld; then
 firewall-cmd --zone=public --permanent --add-port=443/tcp
 firewall-cmd --reload
fi

if [ "$PROVIDER" = "MySql" ] ; then
  # Adding MySQL Package Repository and Installing MySQL Server:
  rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
  rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
  yum install mysql-server -y

  #Enabling and running MySQL Service
  systemctl restart mysqld.service
  systemctl enable mysqld.service
  if systemctl list-unit-files | grep -Fq firewalld; then
    firewall-cmd --zone=public --permanent --add-port=3306/tcp
  fi  
fi  # MySql 

if [ "$PROVIDER" = "MsSql" ] ; then
  curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/7/mssql-server-2019.repo
  yum install -y mssql-server
  /opt/mssql/bin/mssql-conf setup accept-eula
  curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft.com/config/rhel/7/prod.repo
  ACCEPT_EULA=Y yum install -y mssql-tools unixODBC-devel
  echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
  source ~/.bashrc
  systemctl restart mssql-server
  systemctl enable  mssql-server
  if systemctl list-unit-files | grep -Fq firewalld; then
    firewall-cmd --zone=public --permanent --add-port=1433/tcp
  fi

fi  # MsSql 


yum install nginx -y

}
####################################################################




####################################################################
# Centos 8,9  Stream
####################################################################
install_CenOS_8_9_Stream(){

echo updating system...
dnf update -y

#install epel

if [[ $REV  == "8" ]] ; then
  dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y
  dnf install libgdiplus libicu jq gssntlmssp -y
else
  dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm -y
  dnf install libgdiplus libicu jq gssntlmssp -y
  dnf install compat-openssl11 -y
fi


#Installing git
#dnf install git -y

#Installing libicu
#dnf install libicu -y




#disabling SELinux:
sed  -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux
sed  -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config 

setenforce 0

# Firewall Configuration:
#if systemctl list-unit-files | grep -Fq firewalld; then
if systemctl list-unit-files | grep firewalld; then
 firewall-cmd --zone=public --permanent --add-port=443/tcp
fi


if [ "$PROVIDER" = "MySql" ] ; then

  # Installing MySQL Server:
  dnf install mysql-server -y

  #Enabling and running MySQL Service
  systemctl restart mysqld.service
  systemctl enable mysqld.service
  #if systemctl list-unit-files | grep -Fq firewalld; then
  if systemctl list-unit-files | grep -Fq firewalld; then
    firewall-cmd --zone=public --permanent --add-port=3306/tcp
  fi
fi  # MySql 

if [ "$PROVIDER" = "MsSql" ] ; then
  
  
  curl -o /etc/yum.repos.d/mssql-server.repo "https://packages.microsoft.com/config/rhel/8/mssql-server-2019.repo"
  
  dnf install -y mssql-server
  MSSQL_PID=$MSSQL_PID /opt/mssql/bin/mssql-conf setup accept-eula
  curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft.com/config/rhel/8/prod.repo
  ACCEPT_EULA=Y dnf install -y mssql-tools unixODBC-devel
  echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
  source ~/.bashrc
  if systemctl list-unit-files | grep -Fq firewalld; then
    firewall-cmd --zone=public --permanent --add-port=1433/tcp
  fi
fi  # MsSql 

# Installing  Nginx
dnf install nginx -y

}
####################################################################




####################################################################
# Amazon Linux 2
####################################################################
install_Amazon_2(){

echo updating system...
yum update -y

sudo amazon-linux-extras install epel -y 
sudo yum install libgdiplus jq gssntlmssp -y

if [ "$PROVIDER" = "MySql" ] ; then
  
  # adding the MySQL Repository

  yum install https://dev.mysql.com/get/mysql80-community-release-el7-5.noarch.rpm -y

#  TMPDIR=$(mktemp -d)
#  MYSQL_RPM_URL="https://repo.mysql.com//mysql80-community-release-el9-1.noarch.rpm"
#  curl -H "Cache-Control: no-cache" -o  "$TMPDIR/mysql80.rpm" $MYSQL_RPM_URL
#  dnf install "$TMPDIR/mysql80.rpm" -y
#  rm -rf "$TMPDIR"
  
  # Installing MySQL Server:
  yum install mysql-community-server -y
  #Enabling and running MySQL Service
  systemctl restart mysqld.service
  systemctl enable mysqld.service
 
fi  # MySql 

if [ "$PROVIDER" = "MsSql" ] ; then
  
  curl -o /etc/yum.repos.d/mssql-server.repo "https://packages.microsoft.com/config/rhel/8/mssql-server-2019.repo"
  
  dnf install -y mssql-server
  MSSQL_PID=$MSSQL_PID /opt/mssql/bin/mssql-conf setup accept-eula
  curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft.com/config/rhel/8/prod.repo
  ACCEPT_EULA=Y dnf install -y mssql-tools unixODBC-devel
  echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
  source ~/.bashrc
  if systemctl list-unit-files | grep -Fq firewalld; then
    firewall-cmd --zone=public --permanent --add-port=1433/tcp
  fi
fi  # MsSql 

# Installing  Nginx
yum install nginx -y

}
####################################################################





####################################################################
# Amazon Linux 2023
####################################################################
install_Amazon_2023(){

echo updating system...
dnf update -y

#dnf install libgdiplus jq gssntlmssp -y
dnf install jq libicu -y

#disabling SELinux:
sed -i 's/SELINUX=permissive/SELINUX=disabled/' /etc/sysconfig/selinux
sed  -i 's/SELINUX=permissive/SELINUX=disabled/' /etc/selinux/config 

setenforce 0

if [ "$PROVIDER" = "MySql" ] ; then

  
  # adding the MySQL Repository
  TMPDIR=$(mktemp -d)
  MYSQL_RPM_URL="https://repo.mysql.com//mysql80-community-release-el9-1.noarch.rpm"
  curl -H "Cache-Control: no-cache" -o  "$TMPDIR/mysql80.rpm" $MYSQL_RPM_URL
  dnf install "$TMPDIR/mysql80.rpm" -y
  rm -rf "$TMPDIR"
  
  # Installing MySQL Server:
  dnf install mysql-community-server -y
  #Enabling and running MySQL Service
  systemctl restart mysqld.service
  systemctl enable mysqld.service
 
fi  # MySql 

if [ "$PROVIDER" = "MsSql" ] ; then
  
  curl -o /etc/yum.repos.d/mssql-server.repo "https://packages.microsoft.com/config/rhel/8/mssql-server-2019.repo"
  
  dnf install -y mssql-server
  MSSQL_PID=$MSSQL_PID /opt/mssql/bin/mssql-conf setup accept-eula
  curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft.com/config/rhel/8/prod.repo
  ACCEPT_EULA=Y dnf install -y mssql-tools unixODBC-devel
  echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
  source ~/.bashrc
  if systemctl list-unit-files | grep -Fq firewalld; then
    firewall-cmd --zone=public --permanent --add-port=1433/tcp
  fi
fi  # MsSql 

# Installing  Nginx
dnf install nginx -y

}
####################################################################




####################################################################
# Ubuntu 18.04
####################################################################
install_Ubuntu_18_04(){

echo updating system...
apt update
apt upgrade -y

# Firewall Configuration:
ufw allow 443

sudo apt install git libgdiplus libicu60 jq gss-ntlmssp-dev -y

if [ "$PROVIDER" = "MySql" ] ; then
  # Installing MySQL Server:
  DEBPKG="mysql-apt-config_0.8.16-1_all.deb"
  wget -c https://dev.mysql.com/get/$DEBPKG
  dpkg -i $DEBPKG
  apt update0
  apt install mysql-server -y
  ufw allow 3306/tcp
fi # MySql

if [ "$PROVIDER" = "MsSql" ] ; then
    # Installing MS SQL Server:
    wget -qO- https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
    add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/18.04/mssql-server-2019.list)"
    apt-get update
    apt-get install mssql-server -y
    /opt/mssql/bin/mssql-conf setup accept-eula
    curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
    curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list | tee /etc/apt/sources.list.d/msprod.list
    apt-get update
    ACCEPT_EULA=Y apt-get install mssql-tools unixodbc-dev -y 
    echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
    source ~/.bashrc
    ufw allow 1433/tcp
fi # MsSql


# Installing  Nginx
apt install nginx -y
}
####################################################################


####################################################################
# Ubuntu 20.04, 22.04, 24.04
####################################################################
install_Ubuntu_20_04_22_04_24_04(){

echo updating system...
apt update
apt upgrade -y
#installing additional packages
sudo apt install git libgdiplus jq gss-ntlmssp-dev -y

if [[ $REV  == "20.04" ]] ; then
    sudo apt install libicu66
elif [[ $REV  == "22.04" ]] ; then
    sudo apt install libicu70  # ubuntu 22.04
else
    sudo apt install libicu74  # ubuntu 24.04
fi



# Firewall Configuration:
ufw allow 443


if [ "$PROVIDER" = "MySql" ] ; then
    # Installing MySQL Server:
    apt install mysql-server -y
    # on ubuntu 22 root pasword disabled default
    # and on 20 too.....
    ##if [[ $REV  == "22.04" ]] ; then
        read -s -p "Enter new password MySQL root : " MYSQL_ROOT_PASSWORD
        printf "\n"
        mysql -e  "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by '$MYSQL_ROOT_PASSWORD';"
    ##fi
  ufw allow 3306/tcp
fi

if [ "$PROVIDER" = "MsSql" ] ; then
    # Installing MS SQL Server:
    
    wget -qO- https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
    add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/20.04/mssql-server-2019.list)"
    apt-get update
    apt-get install mssql-server -y
    /opt/mssql/bin/mssql-conf setup accept-eula
    curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
    curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list | tee /etc/apt/sources.list.d/msprod.list
    apt-get update
    ACCEPT_EULA=Y apt-get install mssql-tools unixodbc-dev -y 
    echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
    source ~/.bashrc
    ufw allow 1433/tcp
fi

# Installing  Nginx
apt install nginx -y

}
####################################################################


# DETECT OS
# "detectOS.sh"  - this script MUST be in the same directory as the installHES.sh file

DIRNAME=$(dirname $0)
if [ ! -f $DIRNAME/detectOS.sh ] ; then
  echo "there is no detectOS.sh file in the script directory" 
  exit 1
fi

echo PROVIDER = $PROVIDER | tee -a $LOGFILE
echo LOGFAILE = $LOGFILE | tee -a $LOGFILE


. $DIRNAME/detectOS.sh --debug 0
echo Detect OS: | tee -a $LOGFILE
echo DIST = $DIST | tee -a $LOGFILE
echo REV = $REV | tee -a $LOGFILE
echo SUB_REV = $SUB_REV | tee -a $LOGFILE


##############################################################


########################### end settings zone

if [[ $DIST == "Ubuntu" ]]  && [[ $REV  == "18.04" ]]
 then install_Ubuntu_18_04
elif [[ $DIST == "Ubuntu" ]] && ([[ $REV  == "20.04" ]] || [[ $REV  == "22.04" ]] || [[ $REV  == "24.04" ]]   )
  then install_Ubuntu_20_04_22_04_24_04
elif [[ $DIST == "CentOS Linux" ]]  && [[ $SUB_REV  == "7" ]]
  then install_CenOS_7
elif [[ $DIST == "CentOS Stream" ]]  && ([[ $SUB_REV  == "8" ]] || [[ $SUB_REV  == "9" ]])
  then install_CenOS_8_9_Stream
elif [[ $DIST == "Amazon Linux" ]] && [[ $REV  == "2023" ]]
  then install_Amazon_2023
  sudo grep "A temporary password" /var/log/mysqld.log
elif [[ $DIST == "Amazon Linux" ]] && [[ $REV  == "2" ]]
  then install_Amazon_2
  sudo grep "A temporary password" /var/log/mysqld.log
else
  echo "It looks like your distribution is not supported by our script. Try installing HES manually" | tee -a $LOGFILE
  exit 1
fi 


systemctl enable nginx
systemctl restart nginx

echo "You need to restart to work on"
