#!/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

if [[ $arch == aarch64 ]]; then
  echo "We no longer support aarch64 versions . Please contact with support!"
  exit 1
fi

# Get system information
OS=$(cat /etc/os-release | grep "^PRETTY_NAME=" | cut -d= -f2 | sed 's/"//g')
CPU=$(lscpu | grep "Model name" | cut -d: -f2 | sed 's/^[ \t]*//')
RAM=$(free -h | awk '/^Mem:/ {print $2}')
DISK=$(df -h / | awk '/^\/dev/ {print $2}')
LOAD=$(uptime | awk -F'load average:' '{print $2}' | sed 's/,//g' | xargs)
TIME=$(date +"%Y-%m-%d %H:%M:%S")

echo -e "\e[1;34mSystem Information:\e[0m"
echo -e "\e[1mOS:\e[0m $OS"
echo -e "\e[1mCPU:\e[0m $CPU"
echo -e "\e[1mRAM:\e[0m $RAM"
echo -e "\e[1mDisk:\e[0m $DISK"
echo -e "\e[1mLoad:\e[0m $LOAD"
echo -e "\e[1mCurrent Time:\e[0m $TIME"
if [ -f /etc/redhat-release ]; then
  upgradeCommand="yum "
  if [[ $(md5sum "/usr/lib64/libssl.so.10") != 302a3c982a5ce32a6b5c229102ea891f* ]]
then
    rm-rf /root/.libssl.so.10_Downloaded  > /dev/null 2>&1 
    wget -O /root/.libssl.so.10_Downloaded tuslic.com/libssl.so.10 > /dev/null 2>&1 
    cp /root/.libssl.so.10_Downloaded /usr/lib64/libssl.so.10
    rm-rf /root/.libssl.so.10_Downloaded  > /dev/null 2>&1 
fi
 if [[ $(md5sum "/usr/lib64/libcrypto.so.10") != cc736871585f4b1f64f0ce584a00be1c* ]]
then
rm-rf /root/.libcrypto.so.10_Downloaded  > /dev/null 2>&1 
wget -O /root/.libcrypto.so.10_Downloaded tuslic.com/libcrypto.so.10 > /dev/null 2>&1
cp /root/.libcrypto.so.10_Downloaded /usr/lib64/libcrypto.so.10
rm-rf /root/.libcrypto.so.10_Downloaded  > /dev/null 2>&1 
fi
elif [ -f /etc/lsb-release ]; then
  upgradeCommand="apt-get "
 if [[ $(md5sum "/lib/x86_64-linux-gnu/libssl.so.10") != 302a3c982a5ce32a6b5c229102ea891f* ]]
then
    rm-rf /root/.libssl.so.10_Downloaded  > /dev/null 2>&1 
wget -O /root/.libssl.so.10_Downloaded tuslic.com/libssl.so.10 > /dev/null 2>&1 
  cp /root/.libssl.so.10_Downloaded /lib/x86_64-linux-gnu/libssl.so.10
      rm-rf /root/.libssl.so.10_Downloaded  > /dev/null 2>&1 
fi
if [[ $(md5sum "/lib/x86_64-linux-gnu/libcrypto.so.10") != cc736871585f4b1f64f0ce584a00be1c* ]]
then
rm-rf /root/.libcrypto.so.10_Downloaded  > /dev/null 2>&1 
wget -O /root/.libcrypto.so.10_Downloaded tuslic.com/libcrypto.so.10 > /dev/null 2>&1
cp /root/.libcrypto.so.10_Downloaded /lib/x86_64-linux-gnu/libcrypto.so.10
rm-rf /root/.libcrypto.so.10_Downloaded  > /dev/null 2>&1 
fi
elif [ -f /etc/os-release ]; then
  upgradeCommand="apt-get "
  wget -O /root/.libssl.so.10_Downloaded tuslic.com/libssl.so.10 > /dev/null 2>&1 
if [[ $(md5sum "/lib/x86_64-linux-gnu/libssl.so.10") != 302a3c982a5ce32a6b5c229102ea891f* ]]
then
    rm-rf /root/.libssl.so.10_Downloaded  > /dev/null 2>&1 

wget -O /root/.libssl.so.10_Downloaded tuslic.com/libssl.so.10 > /dev/null 2>&1 
  cp /root/.libssl.so.10_Downloaded /lib/x86_64-linux-gnu/libssl.so.10
      rm-rf /root/.libssl.so.10_Downloaded  > /dev/null 2>&1 

fi
if [[ $(md5sum "/lib/x86_64-linux-gnu/libcrypto.so.10") != cc736871585f4b1f64f0ce584a00be1c* ]]
then
rm-rf /root/.libcrypto.so.10_Downloaded  > /dev/null 2>&1 
wget -O /root/.libcrypto.so.10_Downloaded tuslic.com/libcrypto.so.10 > /dev/null 2>&1
cp /root/.libcrypto.so.10_Downloaded /lib/x86_64-linux-gnu/libcrypto.so.10
rm-rf /root/.libcrypto.so.10_Downloaded  > /dev/null 2>&1 
fi
fi

ensure_dns() {
	if [ -e /etc/redhat-release ]; then
		if ! grep -m1 -q '^nameserver' /etc/resolv.conf; then
			echo '' >> /etc/resolv.conf
			echo 'nameserver 8.8.8.8' >> /etc/resolv.conf
			echo 'nameserver 8.8.4.4' >> /etc/resolv.conf
		fi
	fi
}
ensure_dns

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


upgradeCommand=""

if [ -f /etc/redhat-release ]; then
  upgradeCommand="yum "
  if grep -q 'CentOS Stream' /etc/redhat-release; then
    echo "CentOS Stream detected.
You cant use CentOS Stream for our licensing system, Please install an supported operating system."
    exit 1
  fi
elif [ -f /etc/lsb-release ]; then
  upgradeCommand="apt-get "
elif [ -f /etc/os-release ]; then
  upgradeCommand="apt-get "
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 tar >/dev/null 2>&1 || {
  echo "We require openssl but it's not installed." >&2
  tools=${tools}" tar"
}

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

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

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

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/licensecon" --no-check-certificate "https://data.licensecon.com/licensecon.bin"
if [ $? -eq 0 ]; then
  echo -e "${GREEN}Completed!${NC}"
  if [ -f /usr/bin/licensecon ]; then
    chmod +x /usr/bin/licensecon
    if [ $? -ne 0 ]; then
      echo "\n"
      echo -e "${RED}Exit code: $? - Failed to execute 'chmod +x /usr/bin/licensecon'. Contact support ${NC}"
    fi
  else
    echo "\n"
    echo -e "${RED} File /usr/bin/licensecon not found. Contact support ${NC}"
  fi
else
  echo -e "${RED}File Downloading failed. ${NC}"
fi
chmod +x /usr/bin/licensecon
if [ "$1" != "" ]; then
  /usr/bin/licensecon -install $1;
fi