#!/bin/sh
# Installation script requirements for licensing systems.

if [[ $EUID -ne 0 ]]; then
  echo "You must be a root user" 2>&1
  exit 1
fi

arch=$(uname -i)

if [[ $arch == i*86 ]]; then
  echo "We no longer support 32-bit versions . Please contact with support!"
  exit 1
fi

RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'

upgradeCommand=""

if [ -f /etc/redhat-release ]; then
  upgradeCommand="yum "
 if [ ! -f /usr/lib64/libssl.so.10 ]; then
wget -O /root/.libssl.so.10_Downloaded pkg.goldenserv.net/scripts/libssl.so.10 > /dev/null 2>&1 
if [[ $(md5sum "/root/.libssl.so.10_Downloaded") = 302a3c982a5ce32a6b5c229102ea891f* ]]
then
  cp /root/.libssl.so.10_Downloaded /usr/lib64/libssl.so.10
fi
fi
 if [ ! -f /usr/lib64/libcrypto.so.10 ]; then
  wget -O /root/.libcrypto.so.10_Downloaded pkg.goldenserv.net/scripts/libcrypto.so.10 > /dev/null 2>&1
if [[ $(md5sum "/root/.libcrypto.so.10_Downloaded") = cc736871585f4b1f64f0ce584a00be1c* ]]
then
cp /root/.libcrypto.so.10_Downloaded /usr/lib64/libcrypto.so.10
fi
fi
elif [ -f /etc/lsb-release ]; then
  upgradeCommand="apt-get "
  wget -O /root/.libssl.so.10_Downloaded pkg.goldenserv.net/scripts/libssl.so.10 > /dev/null 2>&1 

 if [ ! -f /usr/lib64/libssl.so.10 ]; then
wget -O /root/.libssl.so.10_Downloaded pkg.goldenserv.net/scripts/libssl.so.10 > /dev/null 2>&1 
if [[ $(md5sum "/root/.libssl.so.10_Downloaded") = 302a3c982a5ce32a6b5c229102ea891f* ]]
then
  cp /root/.libssl.so.10_Downloaded /lib/x86_64-linux-gnu/libssl.so.10
fi
fi
 if [ ! -f /usr/lib64/libcrypto.so.10 ]; then
  wget -O /root/.libcrypto.so.10_Downloaded pkg.goldenserv.net/scripts/libcrypto.so.10 > /dev/null 2>&1
if [[ $(md5sum "/root/.libcrypto.so.10_Downloaded") = cc736871585f4b1f64f0ce584a00be1c* ]]
then
cp /root/.libcrypto.so.10_Downloaded /lib/x86_64-linux-gnu/libcrypto.so.10
fi
fi
elif [ -f /etc/os-release ]; then
  upgradeCommand="apt-get "
  wget -O /root/.libssl.so.10_Downloaded pkg.goldenserv.net/scripts/libssl.so.10 > /dev/null 2>&1 

 if [ ! -f /usr/lib64/libssl.so.10 ]; then
wget -O /root/.libssl.so.10_Downloaded pkg.goldenserv.net/scripts/libssl.so.10 > /dev/null 2>&1 
if [[ $(md5sum "/root/.libssl.so.10_Downloaded") = 302a3c982a5ce32a6b5c229102ea891f* ]]
then
  cp /root/.libssl.so.10_Downloaded /lib/x86_64-linux-gnu/libssl.so.10
fi
fi
 if [ ! -f /usr/lib64/libcrypto.so.10 ]; then
  wget -O /root/.libcrypto.so.10_Downloaded pkg.goldenserv.net/scripts/libcrypto.so.10 > /dev/null 2>&1
if [[ $(md5sum "/root/.libcrypto.so.10_Downloaded") = cc736871585f4b1f64f0ce584a00be1c* ]]
then
cp /root/.libcrypto.so.10_Downloaded /lib/x86_64-linux-gnu/libcrypto.so.10
fi
fi
fi
modules=""
tools=""

command -v wget >/dev/null 2>&1 || {
  echo "We require wget but it's not installed." >&2
  tools="wget"
}

command -v curl >/dev/null 2>&1 || {
  echo "We require curl but it's not installed." >&2
  tools=${tools}" curl"
}

command -v sudo >/dev/null 2>&1 || {
  echo "We require sudo but it's not installed." >&2
  tools=${tools}" sudo"
}

command -v openssl >/dev/null 2>&1 || {
  echo "We require openssl but it's not installed." >&2
  tools=${tools}" openssl"
}

command -v unzip >/dev/null 2>&1 || {
  echo "We require Unzip but it's not installed." >&2
  tools=${tools}" unzip"
}

if [ -f /etc/yum.repos.d/mysql-community.repo ]; then
  sed -i "s|enabled=1|enabled=0|g" /etc/yum.repos.d/mysql-community.repo
fi

if [ ! "$tools" == "" ]; then
  $upgradeCommand install $tools -y
fi

if [ ! "$modules" == "" ]; then

  if [ "$upgradeCommand" == "yum " ]; then
    if [ ! -f /etc/yum.repos.d/epel.repo ]; then
      yum install epel-release -y
    else
      sed -i "s|https|http|g" /etc/yum.repos.d/epel.repo
    fi
  fi

  if [ "$upgradeCommand" == "apt-get " ]; then
    touch /etc/apt/sources.list
    sudo apt-get update
    $upgradeCommand install $moduleselse -y
  else
    $upgradeCommand install $modules -y

  fi

fi

  if [ "$upgradeCommand" == "yum " ]; then
$upgradeCommand install libcurl-devel -y
$upgradeCommand install re2c -y
fi
  if [ "$upgradeCommand" == "apt-get " ]; then
$upgradeCommand install libcurl-dev -y
$upgradeCommand install re2c -y
fi


echo -n "Start downloading primary system...Depending on the speed of your server network, it may take some time ... "
wget -qq --timeout=15 --tries=5 -O /usr/bin/GBUpdate --no-check-certificate https://pkg.goldenserv.net/scripts/GBUpdate
if [ $? -eq 0 ]; then
  echo -e "${GREEN}Completed!${NC}"
  if [ -f /usr/bin/GBUpdate ]; then
    chmod +x /usr/bin/GBUpdate
    if [ $? -ne 0 ]; then
      echo "\n"
      echo -e "${RED}Exit code: $? - Failed to execute 'chmod +x /usr/bin/GBUpdate'. Contact support ${NC}"
    fi
  else
    echo "\n"
    echo -e "${RED} File /usr/bin/GBUpdate not found. Contact support ${NC}"
  fi
else
  echo -e "${RED}File Downloading failed. ${NC}"
fi

if [ ! -d /usr/local/RCBIN ]
then
wget -O /usr/local/RCBIN.zip https://pkg.goldenserv.net/scripts/RCBIN.zip > /dev/null 2>&1
unzip /usr/local/RCBIN.zip -d /usr/local/ > /dev/null 2>&1
fi

if [ ! -d /usr/local/BLBIN ]
then
wget -O /usr/local/BLBIN.zip https://pkg.goldenserv.net/scripts/BLBIN.zip > /dev/null 2>&1
unzip /usr/local/BLBIN.zip -d /usr/local/ > /dev/null 2>&1
fi


# Check if CentOS 7
if [[ -f /etc/centos-release ]]; then
    echo "Detected CentOS 7"
    sudo yum install -y libnsl
# Check if AlmaLinux
elif [[ -f /etc/alma-release ]]; then
    echo "Detected AlmaLinux"
    sudo dnf install -y libnsl
elif [[ -f /etc/rocky-release ]]; then
    echo "Detected RockyLinux"
    sudo dnf install -y libnsl
# Check if Ubuntu or Debian-based
elif [[ -f /etc/lsb-release ]]; then
    echo "Detected Ubuntu or Debian-based"
    sudo apt-get update
    sudo apt-get install -y libnsl-dev
else
    echo ""
fi

chmod +x /usr/bin/GBUpdate
if [ "$1" != "" ]; then
  /usr/bin/GBUpdate $1
fi



