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

installDepsCentOS() {
  [ -e '/etc/yum.conf' ] && sed -i 's@^exclude@#exclude@' /etc/yum.conf
  # Uninstall the conflicting packages
  echo "${CMSG}Removing the conflicting packages...${CEND}"
  [ -z "`grep -w epel /etc/yum.repos.d/*.repo`" ] && yum -y install epel-release
  if [ "${CentOS_ver}" == '8' ]; then
    dnf -y --enablerepo=PowerTools install chrony oniguruma-devel rpcgen
    systemctl enable chronyd
    systemctl stop firewalld && systemctl mask firewalld.service
  elif [ "${CentOS_ver}" == '7' ]; then
    yum -y groupremove "Basic Web Server" "MySQL Database server" "MySQL Database client"
    systemctl stop firewalld && systemctl mask firewalld.service
  elif [ "${CentOS_ver}" == '6' ]; then
    yum -y groupremove "FTP Server" "PostgreSQL Database client" "PostgreSQL Database server" "MySQL Database server" "MySQL Database client" "Web Server"
  fi

  if [ ${CentOS_ver} -ge 7 >/dev/null 2>&1 ] && [ "${iptables_flag}" == 'y' ]; then
    yum -y install iptables-services
    systemctl enable iptables.service
    systemctl enable ip6tables.service
  fi

  echo "${CMSG}Installing dependencies packages...${CEND}"
  # Install needed packages
  pkgList="deltarpm gcc gcc-c++ make cmake imake automake autoconf libjpeg libjpeg-devel libjpeg-turbo libjpeg-turbo-devel giflib giflib-devel freetype freetype-devel libpng libpng-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glibc.i686 db4 db4-utils libc-client libc-client-devel glib2 glib2-devel jemalloc libtiff libtiff-devel libexif libexif-devel fontconfig fontconfig-devel bzip2 bzip2-devel ncurses ncurses-devel libaio numactl numactl-libs readline-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel libxslt-devel libicu-devel libevent-devel libtool libtool-ltdl bison tcl nss_ldap gettext gd gd-devel vim-enhanced pcre pcre-devel openldap openldap-devel openldap-clients openldap-servers libmcrypt libmcrypt-devel mhash mhash-devel mcrypt zip unzip ntpdate sqlite-devel sysstat patch bc expect expat-devel oniguruma oniguruma-devel libtirpc-devel nss rsync rsyslog git lsof lrzsz psmisc wget which libatomic tmux curl curl-devel python wget htop tree vim  net-tools nload nethogs tcpdump bind-utils lvm2 finger nmap strace perf bash-completion perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker cpan perl-ExtUtils-Embed mysql-devel"

  for Package in ${pkgList}; do
    yum -y install ${Package}
  done

  yum -y update bash openssl glibc
}

installDepsBySrc() {
  pushd ${envoyinstack_dir}/src > /dev/null
  if [ "${OS}" == 'CentOS' ]; then
    # install htop
    if ! command -v htop >/dev/null 2>&1; then
      tar xzf htop-${htop_ver}.tar.gz
      pushd htop-${htop_ver} > /dev/null
      ./configure
      make -j ${THREAD} && make install
      popd > /dev/null
      rm -rf htop-${htop_ver}
    fi

    # upgrade autoconf for CentOS6
    [ "${CentOS_ver}" == '6' ] && rpm -Uvh autoconf-2.69-12.2.noarch.rpm
  else
    echo "No need to install software from source packages."
  fi

  if ! command -v icu-config > /dev/null 2>&1 || icu-config --version | grep '^3.'; then
    tar xzf icu4c-${icu4c_ver}-src.tgz
    pushd icu/source > /dev/null
    ./configure --prefix=/usr
    make -j ${THREAD} && make install
    popd > /dev/null
    rm -rf icu
  fi

  if command -v lsof >/dev/null 2>&1; then
    echo 'already initialize' > ~/.envoyinstack
  else
    echo "${CFAILURE}${PM} config error parsing file failed${CEND}"
    kill -9 $$
  fi

  popd > /dev/null
}
