compare_versions() {
  version="${1}.0.0"
  major_base=$2
  minor_base=$3
  MAJOR_VERSION=$(echo ${version} | cut -d '.' -f 1)
  MINOR_VERSION=$(echo ${version} | cut -d '.' -f 2)

  echo "       ${DSY_OS_Release_Name} ${1}"

  if [ "${MAJOR_VERSION}" -lt "${major_base}" ]; then
    echo "ERROR: ${DSY_OS_Release_Name} ${major_base}.${minor_base} is the minimum required level" >&2
    return 1
  elif [ "${MAJOR_VERSION}" -eq "${major_base}" ] && [ "${MINOR_VERSION}" -lt "${minor_base}" ]; then
    echo "ERROR: ${DSY_OS_Release_Name} ${major_base}.${minor_base} is the minimum required level" >&2
    return 1
  fi
  
  return 0
}

if [ -n "${DSY_Force_OS}" ]; then
  echo " INFO: DSY_Force_OS=\"${DSY_Force_OS}\", no check prerequisites for Linux"
  return 0
fi

if [ ! -e "/etc/os-release" ]; then
  echo "ERROR: /etc/os-release not found. Cannot proceed"
  exit 81
fi

DSY_OS_Release=$(cat /etc/os-release | grep "^ID=" | cut -d = -f 2 | sed -e 's/^"//' -e 's/"$//')
DSY_OS_Release_Name=$(cat /etc/os-release | grep "^NAME=" | cut -d = -f 2 | sed -e 's/^"//' -e 's/"$//')
echo " INFO: DSY_OS_Release=\"${DSY_OS_Release}\""
export DSY_OS_Release

echo " INFO: Check Prerequisites for Linux"

# Set variables
set +o nounset
RC=0

case ${DSY_OS_Release} in
  #---- SLES ----
  "sles")
    VERSION=$(cat /etc/os-release | grep "^VERSION_ID=" | cut -d = -f 2 | sed -e 's/^"//' -e 's/"$//')
    compare_versions "${VERSION}" 15 4
    RC=$?
  ;;

  #---- RHEL / Oracle Linux / Rocky Linux ----
  "rhel"|"ol"|"rocky")
    VERSION=$(cat /etc/os-release | grep "^VERSION_ID=" | cut -d = -f 2 | sed -e 's/^"//' -e 's/"$//')
    compare_versions "${VERSION}" 8 10
    RC=$?
  ;;

  #---- Kylin ----
  "kylin")
    VERSION=$(cat /etc/os-release | grep "^VERSION_ID=" | cut -d = -f 2 | sed -e 's/^"//' -e 's/"$//' -e 's/^[vV]//')
    compare_versions "${VERSION}" 10 0
    RC=$?
  ;;

  #---- Unknown ----
  *)
    echo "ERROR: Unsupported operating system: ${DSY_OS_Release_Name}" >&2
    RC=1
  ;;
esac

if [ "$RC" = 0 ]; then
  echo "       Prerequisites OK"
else
  echo >&2
  echo "ERROR: Prerequisites KO" >&2
  echo >&2
fi
return $RC
