function detect_platform() {

#		Functions to detect environment:
#		OS flavour: 		        OS_FLAVOUR
#               OS type:                        OS_TYPE
#		gLite version			GLITE_VERSION
#               VDT version                     VDT_VERSION	


yaimlog INFO "Detecting environment"

#
#	OS FLAVOUR detection based on http://www.novell.com/coolsolutions/feature/11251.html
#
#par=`ls -1 /etc | grep release | grep -v lsb | head -n1` 
#export  GLITE_OS="${par%?release}"
#yaimlog DEBUG "Detect platform: OS flavour detected is: ${GLITE_OS}" 
export OS_FLAVOUR=`lsb_release -ds`
yaimlog DEBUG "Detect platform: OS flavour detected is: ${OS_FLAVOUR}"
par=`lsb_release -is`
if (echo "$par" | grep -qi debian); then
   export GLITE_OS="debian"
else 
   export GLITE_OS="redhat"
fi


if [ "`uname -m`" = "x86_64" ]; then
 export OS_ARCH=64BIT
else
 export OS_ARCH=32BIT
fi
yaimlog DEBUG "Detected architecture is ${OS_ARCH}"
#
#	OS TYPE detection 
#

platform_os='unknown'

lsbdescr=`lsb_release -d 2>/dev/null`

if [ "x$lsbdescr" != "x" -o "x${GLITE_OS}" != "x" -a -f "/etc/${GLITE_OS}-release" ]; then
  IFS_SAVED="$IFS"
  IFS=':'
  while read pattern os cos; do
    # pattern: regexp pattern to recognize the OS
    # os: OS release abbreviation
    # cos: compatible OS release abbreviation
    # yaimlog DEBUG "Testing pattern='$pattern' for $os ($cos)"
    if [ "x$lsbdescr" != "x" ]; then
      # yaimlog DEBUG "Detect platform attempting to use lsb_release output"
      echo $lsbdescr | egrep -qi "$pattern"
    else
      # yaimlog DEBUG "Detect platform attempting to test release file content"
      egrep -qi "$pattern" /etc/${GLITE_OS}-release
    fi
    if [ $? -eq 0 ]; then
        # yaimlog DEBUG "Detect platform: recognized $os ($cos)!" 
        platform_os="$cos"
        break
    fi
  done <<EOF
cern virtual machine, version.*1:slc4:slc4
cern virtual machine, version.*2:slc5:slc5
taroon:rhel3:slc3
scientific linux.*release 3:sl3:slc3
scientific linux.*cern.*release 3:slc3:slc3
centos.*release 3:centos3:slc3
scientific linux.*release 4:sl4:slc4
scientific linux.*cern.*release 4:slc4:slc4
centos.*release 4:centos4:slc4
scientific linux.*release 5:sl5:sl5
scientific linux.*cern.*release 5:slc5:sl5
centos.*release 5:centos5:sl5
scientific linux.*release 6:sl6:sl6
scientific linux.*cern.*release 6:slc6:sl6
centos.*release 6:centos6:sl6
scientific linux.*release 7:sl7:el7
centos.*release 7:centos7:el7
almalinux( .*)? 8.*:AlmaLinux 8:el8
red hat enterprise linux( .*)? 8:RHEL 8:el8
rocky.*linux( .*)? 8.*:Rocky Linux 8:el8
almalinux( .*)? 9.*:AlmaLinux 9:el9
red hat enterprise linux( .*)? 9:RHEL 9:el9
rocky.*linux( .*)? 9.*:Rocky Linux 9:el9
debian:debian:debian
EOF
  IFS="$IFS_SAVED"
fi

if [ "$platform_os" = 'unknown' ]; then
  if [ -f /etc/debian_version ] ; then
    platform_os='debian'
  fi
fi

# do a more expensive check if we have not recognised the distribution yet
if [ "$platform_os" = 'unknown' -a -x '/bin/rpm' ]; then
    # yaimlog DEBUG "Detect platform: checking the RPM containing /etc/issue.net." 
    rpmname=$(rpm -qf /etc/issue.net)
    case $rpmname in
        sl-release-6.*)
            platform_os='sl6'
            ;;
        sl-release-5.*)
            platform_os='sl5'
            ;;
        sl-release-4.*.cern-*)
            platform_os='slc4'
            ;;
        sl-release-4.*)
            # in reality this is sl4
            platform_os='slc4'
            ;;
        sl-release-3.*.cern-*)
            platform_os='slc3'
            ;;
        sl-release-3.*)
            # in reality this is sl3
            platform_os='slc3'
            ;;
    esac
fi

if [ -n "$YAIM_OS_TYPE" ]; then
 platform_os=$YAIM_OS_TYPE
 yaimlog WARNING "The OS is overridden to be '$YAIM_OS_TYPE'"
fi

if [ "$platform_os" = 'unknown' ]; then
 yaimlog WARNING "YAIM was not able to detect the distribution name."
 yaimlog WARNING "The OS will be set to 'unknown'"
fi

export OS_TYPE="${platform_os}"
yaimlog DEBUG "Detect platform: OS type detected: ${OS_TYPE}"

}
