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:centos5:sl6
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}"

#       
#	gLite version 3.0 , 3.1
#
# not supported by EMI
#glite=`/opt/glite/bin/glite-version 2>/dev/null`
#if [ $? -eq 0 ]; then
# export  GLITE_VERSION=`echo ${glite%.*} | sed -e 's/\.//g'`
# yaimlog DEBUG "Detect platform: The gLite version detected is: ${GLITE_VERSION}"
#else
# unset GLITE_VERSION
# yaimlog WARNING "No gLite version has been detected. The variable GLITE_VERSION used by yaim can't be defined."
# yaimlog WARNING "If you are configuring gLite, please make sure the command glite-version is installed or YAIM may fail to configure things properly."
#fi

#
#	vdt version # this has to be fixed in order to work for TAR UI, debian, etc
#
# no VDT in EMI, to clarify later
#if [ "x${OS_TYPE}" != "xdebian" ]; then
#  if [ -x /bin/rpm ]; then
#    VDT=`rpm -q --qf %{V} vdt_globus_essentials`
#  else
#    VDT="not installed"
#  fi
#  if ! ( echo "x${VDT}" | egrep -q 'not installed') ; then
#    export VDT_VERSION=`echo ${VDT} | sed -e 's/^VDT\([0-9]*\)\.\([0-9]*\).*/\1.\2/'`
#    yaimlog DEBUG "Detect platform: VDT version detected is: ${VDT_VERSION}"
#  else
#    yaimlog DEBUG "Detect platform: No VDT version has been detected."
#  fi
#fi

}
