#!/bin/bash
# Author:  EinicYeo <einicyeo AT gmail.com>
# BLOG:  https://www.infvie.com
#
# Notes: EnvoyinStack for CentOS/RedHat 6+

export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
clear
printf "
#######################################################################
#               Author:  EinicYeo <einicyeo AT gmail.com>             #
#                   EnvoyinStack for CentOS/RedHat 6+                 #
#                         Install EnvoyinStack                        #
#      For more information please visit https://www.infvie.com       #
#######################################################################
"
# Check if user is root
[ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }

envoyinstack_dir=$(dirname "`readlink -f $0`")
pushd ${envoyinstack_dir} > /dev/null
. ./versions.txt
. ./config/options.conf
. ./include/color.sh
. ./include/check_os.sh
. ./include/check_dir.sh
. ./include/download.sh
. ./include/get_char.sh

dbrootpwd=`< /dev/urandom tr -dc A-Za-z0-9 | head -c16`
dbpostgrespwd=`< /dev/urandom tr -dc A-Za-z0-9 | head -c16`
dbmongopwd=`< /dev/urandom tr -dc A-Za-z0-9 | head -c16`
rabbitmqpwd=`< /dev/urandom tr -dc A-Za-z0-9 | head -c16`
dbinstallmethod=1

version() {
  echo "version: 1.8"
  echo "updated date: 2021-11-18"
}

Show_Help() {
  version
  echo "Usage: $0  command ...[parameters]....
  --help, -h                  Show this help message, More: https://www.infvie.com
  --version, -v               Show version info
  --nginx_option [1-2]        Install Nginx server version
  --tomcat_option [1-4]       Install Tomcat version
  --jdk_option [1-3]          Install JDK version
  --db_option [1-8]           Install DB version
  --dbinstallmethod [1-2]     DB install method, default: 1 binary install
  --dbrootpwd [password]      DB super password
  --redis                     Install Redis
  --rabbitmq                  Install Rabbitmq
  --fastdfs                   Install Fastdfs and Fastdht
  --replmgr                   Install Mysql group replication and Proxysql
  --replms                    Install Mysql master slaves
  --replmsr                   Install Mysql multi source replication
  --kong                      Install Gateway kong server and konga
  --python                    Install Python (PATH: ${python_install_dir})
  --elasticstack              Install Elasticstack 
  --powerline                 Install Powerline
  --keygen                    Automatically push keys to remote servers
  --ssh_port [No.]            SSH port
  --iptables                  Enable iptables
  --reboot                    Restart the server after installation
  "
}
ARG_NUM=$#
TEMP=`getopt -o hvV --long help,version,nginx_option:,tomcat_option:,jdk_option:,db_option:,dbrootpwd:,dbinstallmethod:,redis,rabbitmq,fastdfs,replmgr,replms,replmsr,kong,python,elasticstack,powerline,keygen,ssh_port:,iptables,reboot -- "$@" 2>/dev/null`
[ $? != 0 ] && echo "${CWARNING}ERROR: unknown argument! ${CEND}" && Show_Help && exit 1
eval set -- "${TEMP}"
while :; do
  [ -z "$1" ] && break;
  case "$1" in
    -h|--help)
      Show_Help; exit 0
      ;;
    -v|-V|--version)
      version; exit 0
      ;;
    --nginx_option)
      nginx_option=$2; shift 2
      [[ ! ${nginx_option} =~ ^[1-2]$ ]] && { echo "${CWARNING}nginx_option input error! Please only input number 1~2${CEND}"; exit 1; }
      [ -e "${nginx_install_dir}/sbin/nginx" ] && { echo "${CWARNING}Nginx already installed! ${CEND}"; unset nginx_option; }
      [ -e "${openresty_install_dir}/nginx/sbin/nginx" ] && { echo "${CWARNING}OpenResty already installed! ${CEND}"; unset nginx_option; }
      ;;
    --tomcat_option)
      tomcat_option=$2; shift 2
      [[ ! ${tomcat_option} =~ ^[1-4]$ ]] && { echo "${CWARNING}tomcat_option input error! Please only input number 1~3${CEND}"; exit 1; }
      [ -e "$tomcat_install_dir/conf/server.xml" ] && { echo "${CWARNING}Tomcat already installed! ${CEND}" ; unset tomcat_option; }
      ;;
    --jdk_option)
      jdk_option=$2; shift 2
      [[ ! ${jdk_option} =~ ^[1-3]$ ]] && { echo "${CWARNING}jdk_option input error! Please only input number 1~3${CEND}"; exit 1; }
      ;;
    --db_option)
      db_option=$2; shift 2
      if [[ "${db_option}" =~ ^[1-6]$ ]]; then
        [ -d "${db_install_dir}/support-files" ] && { echo "${CWARNING}MySQL already installed! ${CEND}"; unset db_option; }
      elif [ "${db_option}" == '7' ]; then
        [ -e "${pgsql_install_dir}/bin/psql" ] && { echo "${CWARNING}PostgreSQL already installed! ${CEND}"; unset db_option; }
      elif [ "${db_option}" == '8' ]; then
        [ -e "${mongo_install_dir}/bin/mongo" ] && { echo "${CWARNING}MongoDB already installed! ${CEND}"; unset db_option; }
      else
        echo "${CWARNING}db_option input error! Please only input number 1~10${CEND}"
        exit 1
      fi
      ;;
    --dbrootpwd)
      dbrootpwd=$2; shift 2
      dbpostgrespwd="${dbrootpwd}"
      dbmongopwd="${dbrootpwd}"
      ;;
    --dbinstallmethod)
      dbinstallmethod=$2; shift 2
      [[ ! ${dbinstallmethod} =~ ^[1-2]$ ]] && { echo "${CWARNING}dbinstallmethod input error! Please only input number 1~2${CEND}"; exit 1; }
      ;;
    --redis)
      redis_flag=y; shift 1
      [ -e "${redis_install_dir}/bin/redis-server" ] && { echo "${CWARNING}redis-server already installed! ${CEND}"; unset redis_flag; }
      ;;
    --rabbitmq)
      rabbitmq_flag=y; shift 1
      [ -e "${rabbitmq_install_dir}/sbin/rabbitmq-server" ] && { echo "${CWARNING}rabbitmq-server already installed! ${CEND}"; unset rabbitmq_flag; }
      ;;
    --fastdfs)
      fastdfs_flag=y; shift 1
      [ -e "/usr/local/bin/fdhtd" ] && [ -e "/etc/init.d/fdfs_storaged"] && { echo "${CWARNING}fastdfs and fastdht already installed! ${CEND}"; unset fastdfs_flag; }
      ;;
    --replmgr)
      replmgr_flag=y; shift 1
      [ -e "/data/mysql" ] && [ -e "/data/proxysql" ] && { echo "${CWARNING} mysql mgr and proxysql already installed! ${CEND}"; unset replmgr_flag; }
      ;;
    --replms)
      replms_flag=y; shift 1
      [ -e "/data/mysql" ] && { echo "${CWARNING} mysql already installed,Master-slave replication, please manually configure! ${CEND}"; unset replms_flag; }
      ;;
    --replmsr)
      replmsr_flag=y; shift 1
      [ -e "/data/mysql" ] && { echo "${CWARNING} mysql already installed,Master-slave multi source replication, please manually configure! ${CEND}"; unset replmsr_flag; }
      ;;
    --kong)
      kong_flag=y; shift 1
      [ -e "/usr/local/pgsql" ] && [ -e "/usr/local/bin/kong" ] && { echo "${CWARNING} kong server and konga already installed! ${CEND}"; unset kong_flag; }
      ;;
    --python)
      python_flag=y; shift 1
      ;;
    --elasticstack)
      elasticstack_flag=y; shift 1
      ;;
    --powerline)
      powerline_flag=y; shift 1
      ;;
    --keygen)
      keygen_flag=y; shift 1
      ;;
    --ssh_port)
      ssh_port=$2; shift 2
      ;;
    --iptables)
      iptables_flag=y; shift 1
      ;;
    --reboot)
      reboot_flag=y; shift 1
      ;;
    --)
      shift
      ;;
    *)
      echo "${CWARNING}ERROR: unknown argument! ${CEND}" && Show_Help && exit 1
      ;;
  esac
done

# Use default SSH port 22. If you use another SSH port on your server
if [ -e "/etc/ssh/sshd_config" ]; then
  [ -z "`grep ^Port /etc/ssh/sshd_config`" ] && now_ssh_port=22 || now_ssh_port=`grep ^Port /etc/ssh/sshd_config | awk '{print $2}' | head -1`
  while :; do echo
    [ ${ARG_NUM} == 0 ] && read -e -p "Please input SSH port(Default: ${now_ssh_port}): " ssh_port
    ssh_port=${ssh_port:-${now_ssh_port}}
    if [ ${ssh_port} -eq 22 >/dev/null 2>&1 -o ${ssh_port} -gt 1024 >/dev/null 2>&1 -a ${ssh_port} -lt 65535 >/dev/null 2>&1 ]; then
      break
    else
      echo "${CWARNING}input error! Input range: 22,1025~65534${CEND}"
      exit 1
    fi
  done

  if [ -z "`grep ^Port /etc/ssh/sshd_config`" -a "${ssh_port}" != '22' ]; then
    sed -i "s@^#Port.*@&\nPort ${ssh_port}@" /etc/ssh/sshd_config
  elif [ -n "`grep ^Port /etc/ssh/sshd_config`" ]; then
    sed -i "s@^Port.*@Port ${ssh_port}@" /etc/ssh/sshd_config
  fi
fi

if [ ${ARG_NUM} == 0 ]; then
  if [ ! -e ~/.envoyinstack ]; then
    # check iptables
    while :; do echo
      read -e -p "Do you want to enable iptables? [y/n]: " iptables_flag
      if [[ ! ${iptables_flag} =~ ^[y,n]$ ]]; then
        echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
      else
        break
      fi
    done
  fi

  # check Web server
  while :; do echo
    read -e -p "Do you want to install Web server? [y/n]: " web_flag
    if [[ ! ${web_flag} =~ ^[y,n]$ ]]; then
      echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
    else
      if [ "${web_flag}" == 'y' ]; then
        # Nginx/OpenResty
        while :; do echo
          echo 'Please select Nginx server:'
          echo -e "\t${CMSG}1${CEND}. Install Nginx"
          echo -e "\t${CMSG}2${CEND}. Install OpenResty"
          echo -e "\t${CMSG}3${CEND}. Do not install"
          read -e -p "Please input a number:(Default 1 press Enter) " nginx_option
          nginx_option=${nginx_option:-1}
          if [[ ! ${nginx_option} =~ ^[1-3]$ ]]; then
            echo "${CWARNING}input error! Please only input number 1~3${CEND}"
          else
            [ "${nginx_option}" != '3' -a -e "${nginx_install_dir}/sbin/nginx" ] && { echo "${CWARNING}Nginx already installed! ${CEND}"; unset nginx_option; }
            [ "${nginx_option}" != '3' -a -e "${openresty_install_dir}/nginx/sbin/nginx" ] && { echo "${CWARNING}OpenResty already installed! ${CEND}"; unset nginx_option; }
            break
          fi
        done
        # Tomcat
        while :; do echo
          echo 'Please select tomcat server:'
          echo -e "\t${CMSG}1${CEND}. Install Tomcat-10"
          echo -e "\t${CMSG}2${CEND}. Install Tomcat-9"
          echo -e "\t${CMSG}3${CEND}. Install Tomcat-8"
          echo -e "\t${CMSG}4${CEND}. Install Tomcat-7"
          echo -e "\t${CMSG}5${CEND}. Do not install"
          read -e -p "Please input a number:(Default 5 press Enter) " tomcat_option
          tomcat_option=${tomcat_option:-5}
          if [[ ! ${tomcat_option} =~ ^[1-5]$ ]]; then
            echo "${CWARNING}input error! Please only input number 1~5${CEND}"
          else
            [ "${tomcat_option}" != '5' -a -e "$tomcat_install_dir/conf/server.xml" ] && { echo "${CWARNING}Tomcat already installed! ${CEND}" ; unset tomcat_option; }
            if [ "${tomcat_option}" == ~ ^[1-2]$ ]; then
              while :; do echo
                echo 'Please select JDK version:'
                echo -e "\t${CMSG}1${CEND}. Install JDK-11.0"
                echo -e "\t${CMSG}2${CEND}. Install JDK-1.8"
                read -e -p "Please input a number:(Default 1 press Enter) " jdk_option
                jdk_option=${jdk_option:-1}
                if [[ ! ${jdk_option} =~ ^[1-2]$ ]]; then
                  echo "${CWARNING}input error! Please only input number 1~2${CEND}"
                else
                  break
                fi
              done
            elif [ "${tomcat_option}" == '3' ]; then
              while :; do echo
                echo 'Please select JDK version:'
                echo -e "\t${CMSG}1${CEND}. Install JDK-11.0"
                echo -e "\t${CMSG}2${CEND}. Install JDK-1.8"
                echo -e "\t${CMSG}3${CEND}. Install JDK-1.7"
                read -e -p "Please input a number:(Default 2 press Enter) " jdk_option
                jdk_option=${jdk_option:-2}
                if [[ ! ${jdk_option} =~ ^[1-3]$ ]]; then
                  echo "${CWARNING}input error! Please only input number 1~3${CEND}"
                else
                  break
                fi
              done
            elif [ "${tomcat_option}" == '4' ]; then
              while :; do echo
                echo 'Please select JDK version:'
                echo -e "\t${CMSG}2${CEND}. Install JDK-1.8"
                echo -e "\t${CMSG}3${CEND}. Install JDK-1.7"
                read -e -p "Please input a number:(Default 3 press Enter) " jdk_option
                jdk_option=${jdk_option:-3}
                if [[ ! ${jdk_option} =~ ^[2-3]$ ]]; then
                  echo "${CWARNING}input error! Please only input number 2~3${CEND}"
                else
                  break
                fi
              done
             fi
            break
          fi
        done
      fi
      break
    fi
  done

  # choice database
  while :; do echo
    read -e -p "Do you want to install Database? [y/n]: " db_flag
    if [[ ! ${db_flag} =~ ^[y,n]$ ]]; then
      echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
    else
      if [ "${db_flag}" == 'y' ]; then
        while :; do echo
          echo 'Please select a version of the Database:'
          echo -e "\t${CMSG} 1${CEND}. Install MySQL-8.0"
          echo -e "\t${CMSG} 2${CEND}. Install MySQL-5.7"
          echo -e "\t${CMSG} 3${CEND}. Install MySQL-5.6"
          echo -e "\t${CMSG} 4${CEND}. Install MariaDB-10.6"
          echo -e "\t${CMSG} 5${CEND}. Install MariaDB-10.5"
          echo -e "\t${CMSG} 6${CEND}. Install MariaDB-10.4"
          echo -e "\t${CMSG} 7${CEND}. Install PostgreSQL"
          echo -e "\t${CMSG} 8${CEND}. Install MongoDB"
          read -e -p "Please input a number:(Default 2 press Enter) " db_option
          db_option=${db_option:-2}
          [[ "${db_option}" =~ ^[1,5,8]$ ]] && [ "${OS_BIT}" == '32' ] && { echo "${CWARNING}By not supporting 32-bit! ${CEND}"; continue; }
          if [[ "${db_option}" =~ ^[1-8]$ ]]; then
            if [ "${db_option}" == '7' ]; then
              [ -e "${pgsql_install_dir}/bin/psql" ] && { echo "${CWARNING}PostgreSQL already installed! ${CEND}"; unset db_option; break; }
            elif [ "${db_option}" == '8' ]; then
              [ -e "${mongo_install_dir}/bin/mongo" ] && { echo "${CWARNING}MongoDB already installed! ${CEND}"; unset db_option; break; }
            else
              [ -d "${db_install_dir}/support-files" ] && { echo "${CWARNING}MySQL already installed! ${CEND}"; unset db_option; break; }
            fi
            while :; do
              if [ "${db_option}" == '7' ]; then
                read -e -p "Please input the postgres password of PostgreSQL(default: ${dbpostgrespwd}): " dbpwd
                dbpwd=${dbpwd:-${dbpostgrespwd}}
              elif [ "${db_option}" == '8' ]; then
                read -e -p "Please input the root password of MongoDB(default: ${dbmongopwd}): " dbpwd
                dbpwd=${dbpwd:-${dbmongopwd}}
              else
                read -e -p "Please input the root password of MySQL(default: ${dbrootpwd}): " dbpwd
                dbpwd=${dbpwd:-${dbrootpwd}}
              fi
              [ -n "`echo ${dbpwd} | grep '[+|&]'`" ] && { echo "${CWARNING}input error,not contain a plus sign (+) and & ${CEND}"; continue; }
              if (( ${#dbpwd} >= 5 )); then
                if [ "${db_option}" == '7' ]; then
                  dbpostgrespwd=${dbpwd}
                elif [ "${db_option}" == '8' ]; then
                  dbmongopwd=${dbpwd}
                else
                  dbrootpwd=${dbpwd}
                fi
                break
              else
                echo "${CWARNING}password least 5 characters! ${CEND}"
              fi
            done
            # choose install methods
            if [[ "${db_option}" =~ ^[1-6]$ ]]; then
              while :; do echo
                echo "Please choose installation of the database:"
                echo -e "\t${CMSG}1${CEND}. Install database from binary package."
                echo -e "\t${CMSG}2${CEND}. Install database from source package."
                read -e -p "Please input a number:(Default 1 press Enter) " dbinstallmethod
                dbinstallmethod=${dbinstallmethod:-1}
                if [[ ! ${dbinstallmethod} =~ ^[1-2]$ ]]; then
                  echo "${CWARNING}input error! Please only input number 1~2${CEND}"
                else
                  [[ "${db_option}" =~ ^[4-5]$ ]] && [ "${LIBC_YN}" != '0' -a "${dbinstallmethod}" == '1' ] && { echo "${CWARNING}MariaDB-10.3+ binaries require GLIBC 2.14 or higher! ${CEND}"; continue; }
                  break
                fi
              done
            fi
            break
          else
            echo "${CWARNING}input error! Please only input number 1~8${CEND}"
          fi
        done
      fi
      break
    fi
  done

  # check redis
  while :; do echo
    read -e -p "Do you want to install redis-server? [y/n]: " redis_flag
    if [[ ! ${redis_flag} =~ ^[y,n]$ ]]; then
      echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
    else
      [ "${redis_flag}" == 'y' -a -e "${redis_install_dir}/bin/redis-server" ] && { echo "${CWARNING}redis-server already installed! ${CEND}"; unset redis_flag; }
      break
    fi
  done
  # check rabbitmq
  while :; do echo
    read -e -p "Do you want to install rabbitmq-server? [y/n]: " rabbitmq_flag
    if [[ ! ${rabbitmq_flag} =~ ^[y,n]$ ]]; then
      echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
    else
      [ "${rabbitmq_flag}" == 'y' -a -e "${rabbitmq_install_dir}/sbin/rabbitmq-server" ] && { echo "${CWARNING}rabbitmq-server already installed! ${CEND}"; unset rabbitmq_flag; }
      break
    fi
  done
   # check fastdfs
  while :; do echo
    read -e -p "Do you want to install fastdfs and fastdht? [y/n]: " fastdfs_flag
    if [[ ! ${fastdfs_flag} =~ ^[y,n]$ ]]; then
      echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
    else
      [ "${fastdfs_flag}" == 'y' -a -e "/usr/local/bin/fdhtd" ] && [ "${fastdfs_flag}" == 'y' -a -e "/etc/init.d/fdfs_storaged" ] && { echo "${CWARNING}fastdfs and fastdht already installed! ${CEND}"; unset fastdfs_flag; }
      break
    fi
  done
   # check replmgr
  while :; do echo
    read -e -p "Do you want to install mgr and proxysql? [y/n]: " replmgr_flag
    if [[ ! ${replmgr_flag} =~ ^[y,n]$ ]]; then
      echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
    else
      [ "${replmgr_flag}" == 'y' -a -e "/etc/my.cnf" ] && [ "${replmgr_flag}" == 'y' -a -e "/usr/bin/proxysql" ] && { echo "${CWARNING}mgr and proxysql already installed! ${CEND}"; unset replmgr_flag; }
      break
    fi
  done
   # check replms
  while :; do echo
    read -e -p "Do you want to install mysql master slave? [y/n]: " replms_flag
    if [[ ! ${replms_flag} =~ ^[y,n]$ ]]; then
      echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
    else
      [ "${replms_flag}" == 'y' -a -e "/etc/my.cnf" ] && [ "${replms_flag}" == 'y' -a -e "/usr/local/mysql/bin/mysql" ] && { echo "${CWARNING}mysql master slave already installed! ${CEND}"; unset replms_flag; }
      break
    fi
  done
   # check replmsr
  while :; do echo
    read -e -p "Do you want to install mysql multi source replication? [y/n]: " replmsr_flag
    if [[ ! ${replmsr_flag} =~ ^[y,n]$ ]]; then
      echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
    else
      [ "${replmsr_flag}" == 'y' -a -e "/etc/my.cnf" ] && [ "${replmsr_flag}" == 'y' -a -e "/usr/local/mysql/bin/mysql" ] && { echo "${CWARNING}mysql multi source replication already installed! ${CEND}"; unset replmsr_flag; }
      break
    fi
  done
   # check kong
  while :; do echo
    read -e -p "Do you want to install gateway kong server and konga? [y/n]: " kong_flag
    if [[ ! ${kong_flag} =~ ^[y,n]$ ]]; then
      echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
    else
      [ "${kong_flag}" == 'y' -a -e "/etc/kong/kong.conf" ] && [ "${kong_flag}" == 'y' -a -e "/usr/local/bin/kong" ] && { echo "${CWARNING}gateway kong server and konga already installed! ${CEND}"; unset kong_flag; }
      break
    fi
  done

fi

if [[ ${nginx_option} =~ ^[1-2]$ ]] || [[ ${tomcat_option} =~ ^[1-4]$ ]] || [[ ${kong_flag} == 'y' ]]; then
  [ ! -d ${wwwroot_dir}/default ] && mkdir -p ${wwwroot_dir}/default
  [ ! -d ${wwwlogs_dir} ] && mkdir -p ${wwwlogs_dir}
fi
[ -d /data ] && chmod 755 /data

# install wget gcc curl python
if [ ! -e ~/.envoyinstack ]; then
  downloadDepsSrc=1
  [ "${PM}" == 'yum' ] && yum clean all
  ${PM} -y install wget gcc curl python htop lrzsz tree vim lsof  
  [ "${CentOS_ver}" == '8' ] && { yum -y install python36; sudo alternatives --set python /usr/bin/python3; }
fi

# get the IP information
IPADDR=$(./include/get_ipaddr.py)
PUBLIC_IPADDR=$(./include/get_public_ipaddr.py)
IPADDR_COUNTRY=$(./include/get_ipaddr_state.py ${PUBLIC_IPADDR})

# Check download source packages
. ./include/check_download.sh
[ "${armplatform}" == "y" ] && dbinstallmethod=2
checkDownload 2>&1 | tee -a ${envoyinstack_dir}/install.log

# del openssl for jcloud
[ -e "/usr/local/bin/openssl" ] && rm -rf /usr/local/bin/openssl
[ -e "/usr/local/include/openssl" ] && rm -rf /usr/local/include/openssl

# get OS Memory
. ./include/memory.sh

if [ ! -e ~/.envoyinstack ]; then
  # Check binary dependencies packages
  . ./include/check_sw.sh
  case "${OS}" in
    "CentOS")
      installDepsCentOS 2>&1 | tee ${envoyinstack_dir}/install.log
      . include/init_CentOS.sh 2>&1 | tee -a ${envoyinstack_dir}/install.log
      ;;
  esac
  # Install dependencies from source package
  installDepsBySrc 2>&1 | tee -a ${envoyinstack_dir}/install.log
fi

# start Time
startTime=`date +%s`

# Jemalloc
if [[ ${nginx_option} =~ ^[1-2]$ ]] || [[ "${db_option}" =~ ^[1-9]$|^1[0-3]$ ]]; then
  . include/jemalloc.sh
  Install_Jemalloc | tee -a ${envoyinstack_dir}/install.log
fi

# openSSL
if [[ ${tomcat_option} =~ ^[1-3]$ ]]; then
  . include/openssl.sh
  Install_openSSL | tee -a ${envoyinstack_dir}/install.log
fi

# Database
case "${db_option}" in
  1)
    [ "${OS}" == 'CentOS' ] && [ ${CentOS_ver} -le 6 >/dev/null 2>&1 ] && dbinstallmethod=1 && checkDownload
    . include/mysql-8.0.sh
    Install_MySQL80 2>&1 | tee -a ${envoyinstack_dir}/install.log
    ;;
  2)
    . include/mysql-5.7.sh
    Install_MySQL57 2>&1 | tee -a ${envoyinstack_dir}/install.log
    ;;
  3)
    . include/mysql-5.6.sh
    Install_MySQL56 2>&1 | tee -a ${envoyinstack_dir}/install.log
    ;;
  4)
    . include/mariadb-10.6.sh
    Install_MariaDB106 2>&1 | tee -a ${envoyinstack_dir}/install.log
    ;;
  5)
    . include/mariadb-10.5.sh
    Install_MariaDB105 2>&1 | tee -a ${envoyinstack_dir}/install.log
    ;;
  6)
    . include/mariadb-10.4.sh
    Install_MariaDB104 2>&1 | tee -a ${envoyinstack_dir}/install.log
    ;;
  7)
    . include/postgresql.sh
    Install_PostgreSQL 2>&1 | tee -a ${envoyinstack_dir}/install.log
    ;;
  8)
    . include/mongodb.sh
    Install_MongoDB 2>&1 | tee -a ${envoyinstack_dir}/install.log
    ;;
esac

# Nginx server
case "${nginx_option}" in
  1)
    . include/nginx.sh
    Install_Nginx 2>&1 | tee -a ${envoyinstack_dir}/install.log
    ;;
  2)
    . include/openresty.sh
    Install_OpenResty 2>&1 | tee -a ${envoyinstack_dir}/install.log
    ;;
esac

# JDK
case "${jdk_option}" in
  1)
    . include/jdk-11.0.sh
    Install_JDK110 2>&1 | tee -a ${envoyinstack_dir}/install.log
    ;;
  2)
    . include/jdk-1.8.sh
    Install_JDK18 2>&1 | tee -a ${envoyinstack_dir}/install.log
    ;;
  3)
    . include/jdk-1.7.sh
    Install_JDK17 2>&1 | tee -a ${envoyinstack_dir}/install.log
    ;;
esac

case "${tomcat_option}" in
  1)
    . include/tomcat-9.sh
    Install_Tomcat9 2>&1 | tee -a ${envoyinstack_dir}/install.log
    ;;
  2)
    . include/tomcat-8.sh
    Install_Tomcat8 2>&1 | tee -a ${envoyinstack_dir}/install.log
    ;;
  3)
    . include/tomcat-7.sh
    Install_Tomcat7 2>&1 | tee -a ${envoyinstack_dir}/install.log
    ;;
esac

# redis
if [ "${redis_flag}" == 'y' ]; then
  . include/redis.sh
  Install_redis_server 2>&1 | tee -a ${envoyinstack_dir}/install.log
fi

# rabbitmq
if [ "${rabbitmq_flag}" == 'y' ]; then
  . include/rabbitmq.sh
  Install_rabbitmq_server 2>&1 | tee -a ${envoyinstack_dir}/install.log
fi

#fastdfs
if [ "${fastdfs_flag}" == 'y' ]; then
  . include/fastdfs.sh
  Install_fastdfs 2>&1 | tee -a ${envoyinstack_dir}/install.log
fi

#replmgr
if [ "${replmgr_flag}" == 'y' ]; then
  . include/replmgr.sh
  Install_ProxysqlMgr 2>&1 | tee -a ${envoyinstack_dir}/install.log
fi

#replms
if [ "${replms_flag}" == 'y' ]; then
  . include/replmgr.sh
  Install_master_slaves 2>&1 | tee -a ${envoyinstack_dir}/install.log
fi

#replmsr
if [ "${replmsr_flag}" == 'y' ]; then
  . include/replmgr.sh
  Install_multi_source_replication 2>&1 | tee -a ${envoyinstack_dir}/install.log
fi

#kong 
if [ "${kong_flag}" == 'y' ]; then
  . include/kong.sh
  Install_kong_server 2>&1 | tee -a ${envoyinstack_dir}/install.log
fi

# index example
if [ -d "${wwwroot_dir}/default" ]; then
  . include/demo.sh
  DEMO 2>&1 | tee -a ${envoyinstack_dir}/install.log
fi

# get web_install_dir and db_install_dir
. include/check_dir.sh

# Python
if [ "${python_flag}" == 'y' ]; then
  . include/python.sh
  Install_Python 2>&1 | tee -a ${envoyinstack_dir}/install.log
fi

# Elasticstack
if [ "${elasticstack_flag}" == 'y' ]; then
  . include/elasticstack.sh
  Install_Elasticstack 2>&1 | tee -a ${envoyinstack_dir}/install.log
fi

# Powerline
if [ "${powerline_flag}" == 'y' ]; then
  . include/powerline.sh
  Install_Powerline 2>&1 | tee -a ${envoyinstack_dir}/install.log
fi

# keygen
if [ "${keygen_flag}" == 'y' ]; then
  pushd ${envoyinstack_dir}/tools > /dev/null
  cat /dev/null > iplist.txt
  popd > /dev/null
  . keygen.sh
  Inkeygen 2>&1 | tee -a ${envoyinstack_dir}/install.log
fi


# Starting DB
[ -d "/etc/mysql" ] && /bin/mv /etc/mysql{,_bk}
[ -d "${db_install_dir}/support-files" ] && [ -z "`ps -ef | grep mysqld_safe | grep -v grep`" ] && service mysqld start

# set replmgr mysql_root_password
sed -r -i "/mysql_root_password/s/.*/mysql_root_password: ${dbrootpwd}/g" include/replmgr/config.yaml

# set dbpsqlpwd
#sed -i "s@dbpsqlpwd=.*@dbpsqlpwd='${dbrootpwd}'@g" config/options.conf

endTime=`date +%s`
((installTime=($endTime-$startTime)/60))
echo "####################Congratulations########################"
echo "Total EnvoyinStack Install Time: ${CQUESTION}${installTime}${CEND} minutes"
[[ "${nginx_option}" =~ ^[1-2]$ ]] && echo -e "\n$(printf "%-32s" "Nginx install dir":)${CMSG}${web_install_dir}${CEND}"
[[ "${tomcat_option}" =~ ^[1-3]$ ]] && echo -e "\n$(printf "%-32s" "Tomcat install dir":)${CMSG}${tomcat_install_dir}${CEND}"
[[ "${db_option}" =~ ^[1-6]$ ]] && echo -e "\n$(printf "%-32s" "Database install dir:")${CMSG}${db_install_dir}${CEND}"
[[ "${db_option}" =~ ^[1-6]$ ]] && echo "$(printf "%-32s" "Database data dir:")${CMSG}${db_data_dir}${CEND}"
[[ "${db_option}" =~ ^[1-6]$ ]] && echo "$(printf "%-32s" "Database user:")${CMSG}root${CEND}"
[[ "${db_option}" =~ ^[1-6]$ ]] && echo "$(printf "%-32s" "Database password:")${CMSG}${dbrootpwd}${CEND}"
[ "${db_option}" == '7' ] && echo -e "\n$(printf "%-32s" "PostgreSQL install dir:")${CMSG}${pgsql_install_dir}${CEND}"
[ "${db_option}" == '7' ] && echo "$(printf "%-32s" "PostgreSQL data dir:")${CMSG}${pgsql_data_dir}${CEND}"
[ "${db_option}" == '7' ] && echo "$(printf "%-32s" "PostgreSQL user:")${CMSG}postgres${CEND}"
[ "${db_option}" == '7' ] && echo "$(printf "%-32s" "postgres password:")${CMSG}${dbpostgrespwd}${CEND}"
[ "${db_option}" == '8' ] && echo -e "\n$(printf "%-32s" "MongoDB install dir:")${CMSG}${mongo_install_dir}${CEND}"
[ "${db_option}" == '8' ] && echo "$(printf "%-32s" "MongoDB data dir:")${CMSG}${mongo_data_dir}${CEND}"
[ "${db_option}" == '8' ] && echo "$(printf "%-32s" "MongoDB user:")${CMSG}root${CEND}"
[ "${db_option}" == '8' ] && echo "$(printf "%-32s" "MongoDB password:")${CMSG}${dbmongopwd}${CEND}"
[ "${redis_flag}" == 'y' ] && echo -e "\n$(printf "%-32s" "Redis install dir:")${CMSG}${redis_install_dir}${CEND}"
[ "${rabbitmq_flag}" == 'y' ] && echo -e "\n$(printf "%-32s" "Rabbitmq install dir:")${CMSG}${rabbitmq_install_dir}${CEND}"
[ "${fastdfs_flag}" == 'y' ] && echo -e "\n$(printf "%-32s" "FDFS+FDHT data install dir:")${CMSG}/data/fastDFS${CEND}"
[ "${replmgr_flag}" == 'y' ] && echo -e "\n$(printf "%-32s" "Proxysql data install dir:")${CMSG}/data/proxysql${CEND}"
[ "${replms_flag}" == 'y' ] && echo -e "\n$(printf "%-32s" "Mysql data install dir:")${CMSG}${db_data_dir}${CEND}"
[ "${replmsr_flag}" == 'y' ] && echo -e "\n$(printf "%-32s" "Mysql data install dir:")${CMSG}${db_data_dir}${CEND}"
[ "${kong_flag}" == 'y' ] && echo -e "\n$(printf "%-32s" "Kong data install dir:")${CMSG}${pgsql_data_dir}${CEND}"
[ "${elasticstack_flag}" == 'y' ] && echo -e "\n$(printf "%-32s" "Eelasticstack data install dir:")${CMSG}$/data/elasticstack${CEND}"


if [[ ${nginx_option} =~ ^[1-2]$ ]] || [[ ${tomcat_option} =~ ^[1-4]$ ]]; then
  echo -e "\n$(printf "%-32s" "Index URL:")${CMSG}http://${IPADDR}/${CEND}"
elif [[ ${rabbitmq_flag} == 'y' ]];then
  echo "$(printf "%-32s" "Rabbitmq data dir:")${CMSG}/data/rabbitmq${CEND}"
  echo "$(printf "%-32s" "Rabbitmq user:")${CMSG}root${CEND}"
  echo "$(printf "%-32s" "Rabbitmq password:")${CMSG}${rabbitmqpwd}${CEND}"
  echo -e "\n$(printf "%-32s" "Rabbitmq URL:")${CMSG}http://${IPADDR}:15672/${CEND}"
elif [[ ${fastdfs_flag} == 'y' ]];then
  echo -e "\n$(printf "%-32s" "Storage URL:")${CMSG}http://${IPADDR}:8088/${CEND}"
  echo -e "\n$(printf "%-32s" "Tracker URL:")${CMSG}http://${IPADDR}/${CEND}"
elif [[ ${kong_flag} == 'y' ]];then
 echo -e "\n$(printf "%-32s" "Konga URL:")${CMSG}http://${IPADDR}:1338/${CEND}"
 echo -e "\n$(printf "%-32s" "Kong  URL:")${CMSG}http://127.0.0.1:8001/${CEND}"
fi

if [ ${ARG_NUM} == 0 ]; then
  while :; do echo
    echo "${CMSG}Please restart the server and see if the services start up fine.${CEND}"
    read -e -p "Do you want to restart OS ? [y/n]: " reboot_flag
    if [[ ! "${reboot_flag}" =~ ^[y,n]$ ]]; then
      echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
    else
      break
    fi
  done
fi
[ "${reboot_flag}" == 'y' ] && reboot
