#!/bin/sh

# set DEBUG to print some debug information!
if [ -n "$DEBUG" ]; then
    d_p=$(basename $0)" DEBUG:"
fi

platform_os='unknown'
set +e

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
    if [ -n "$DEBUG" ]; then echo "$d_p Testing pattern='$pattern' for $os ($cos)" >&2; fi
    egrep -qi "$pattern" /etc/issue.net
    if [ $? -eq 0 ]; then
        if [ -n "$DEBUG" ]; then echo "$d_p recognized $os ($cos)!" >&2; fi
        platform_os="$cos"
    fi
done <<EOF
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
scientific linux.*release 6:sl6:sl6
scientific linux.*cern.*release 6:slc6:sl6
centos.*release 5:centos5:sl5
debian:debian:debian
ubuntu 7.10:ubuntu710:sl5
EOF
IFS="$IFS_SAVED"

# do a more expensive check if /etc/issue.net is unrecognizable
if [ "$platform_os" = 'unknown' -a -x '/bin/rpm' ]; then
    if [ -n "$DEBUG" ]; then echo "$d_p not recognized /etc/issue.net, checking the RPM." >&2; fi
    rpmname=$(rpm -qf /etc/issue.net)
    case $rpmname in 
        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

set -e

echo ${platform_os}
