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

# install offline yum createrepo
pushd ${envoyinstack_dir}/src/yum-custom/packages/ &> /dev/null

if [ -e "/usr/bin/createrepo" ];then
 if [ $? -ne 0 ];then
     rpm -ivh deltarpm-3.6-3.el7.x86_64.rpm libxml2-python-2.9.1-6.el7.4.x86_64.rpm libxml2-2.9.1-6.el7.4.x86_64.rpm python-deltarpm-3.6-3.el7.x86_64.rpm createrepo-0.9.9-28.el7.noarch.rpm &> /dev/null
 fi
fi

/usr/bin/createrepo -pdo ${envoyinstack_dir}/src/yum-custom/ ${envoyinstack_dir}/src/yum-custom/ >/dev/null 2>&1

mkdir -p /etc/yum.repos.d/backup && mv -f /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/

cp -f ${envoyinstack_dir}/config/*.repo /etc/yum.repos.d/

cat > /etc/yum.repos.d/CentOS-Media.repo <<EOF
[Envoyin-yum-media]
name=CentOS- - Media
baseurl=file:///opt/giten/envoyinstack/src/yum-custom/
gpgcheck=0
enabled=1
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
EOF


yum clean all >/dev/null 2>&1
#yum makecache >/dev/null 2>&1

if [ -e '/usr/sbin/lvs' ];then
 if [ $? -ne 0 ];then 
   yum -y install lvm2
 fi
fi
popd > /dev/null
		

if [ -e "/usr/bin/yum" ]; then
  if [ $? -eq 0 ]; then
    PM=yum
    command -v lsb_release >/dev/null 2>&1 || { [ -e "/etc/euleros-release" ] && yum -y install euleros-lsb || yum -y install redhat-lsb-core; clear; }
 fi
fi

command -v lsb_release >/dev/null 2>&1 || { echo "${CFAILURE}${PM} source failed! ${CEND}"; kill -9 $$; }

# Get OS Version
if [ -e /etc/redhat-release ]; then
  OS=CentOS
  CentOS_ver=$(lsb_release -sr | awk -F. '{print $1}')
  [[ "$(lsb_release -is)" =~ ^Aliyun$|^AlibabaCloudEnterpriseServer$ ]] && { CentOS_ver=7; Aliyun_ver=$(lsb_release -rs); }
  [[ "$(lsb_release -is)" =~ ^EulerOS$ ]] && { CentOS_ver=7; EulerOS_ver=$(lsb_release -rs); }
  [ "$(lsb_release -is)" == 'Fedora' ] && [ ${CentOS_ver} -ge 19 >/dev/null 2>&1 ] && { CentOS_ver=7; Fedora_ver=$(lsb_release -rs); }
elif [ -n "$(grep 'Amazon Linux' /etc/issue)" -o -n "$(grep 'Amazon Linux' /etc/os-release)" ]; then
  OS=CentOS
  CentOS_ver=7
fi

# Check OS Version
if [ ${CentOS_ver} -lt 6 >/dev/null 2>&1 ]; then
  echo "${CFAILURE}Does not support this OS, Please install CentOS 6+ ${CEND}"
  kill -9 $$
fi

LIBC_YN=$(awk -v A=$(getconf -a | grep GNU_LIBC_VERSION | awk '{print $NF}') -v B=2.14 'BEGIN{print(A>=B)?"0":"1"}')
[ ${LIBC_YN} == '0' ] && GLIBC_FLAG=linux-glibc_214 || GLIBC_FLAG=linux

if uname -m | grep -Eqi "arm|aarch64"; then
  armplatform="y"
  if uname -m | grep -Eqi "armv7"; then
    TARGET_ARCH="armv7"
  elif uname -m | grep -Eqi "armv8"; then
    TARGET_ARCH="arm64"
  elif uname -m | grep -Eqi "aarch64"; then
    TARGET_ARCH="aarch64"
  else
    TARGET_ARCH="unknown"
  fi
fi

if [ "$(uname -r | awk -F- '{print $3}' 2>/dev/null)" == "Microsoft" ]; then
  Wsl=true
fi

if [ "$(getconf WORD_BIT)" == "32" ] && [ "$(getconf LONG_BIT)" == "64" ]; then
  OS_BIT=64
  SYS_BIT_j=x64 #jdk
  SYS_BIT_a=x86_64 #mariadb
  SYS_BIT_b=x86_64 #mariadb
  [ "${TARGET_ARCH}" == 'aarch64' ]
else
  OS_BIT=32
  SYS_BIT_j=i586
  SYS_BIT_a=x86
  SYS_BIT_b=i686
  SYS_BIT_c=i386
  SYS_BIT_d=x86
  [ "${TARGET_ARCH}" == 'armv7' ] && { SYS_BIT_c=armhf; SYS_BIT_d=armv7l; }
fi

THREAD=$(grep 'processor' /proc/cpuinfo | sort -u | wc -l)

# Percona binary: https://www.percona.com/doc/percona-server/5.7/installation.html#installing-percona-server-from-a-binary-tarball
if [[ "${CentOS_ver}" =~ ^[6-7]$ ]]; then
  sslLibVer=ssl101
elif [ "${CentOS_ver}" == '8' ]; then 
  sslLibVer=ssl1:111
else
  sslLibVer=unknown
fi
