#!/bin/sh
#
# Suss out what sort of platform I am ...
#
# whatami [-h]
#

debug=false
tmp=/var/tmp/$$
trap "rm -f $tmp.*; exit 0" 0 1 2 3 15

fmt="%-11.11s %-8.8s %-7.7s %s%s%s\n"
if [ "$1" = "-h" ]
then
    printf "$fmt" "Host" "PCP" "CPU" "Operating System and Options"
    printf "$fmt" "-----------" "--------" "------" "-----------------------------"
fi

from=''
if which uname >/dev/null 2>&1
then
    arch=`uname -m`
    [ "$arch" = unknown ] && arch=`uname -p`
    os="`uname -sr`"
    $debug && from=' [uname -sr]'
else
    arch=unknown
    os=unknown
fi
if [ -f /etc/SuSE-release ]
then
    # SUSE Linux Enterprise Server 11 (ia64)
    os=`sed </etc/SuSE-release -n -e '1{
s/ (.*//
s/Linux Enterprise Server /SLES/
p
}'`
    os="$os`sed </etc/SuSE-release -n -e '/^PATCHLEVEL /{
s/.*=[ 	]*/ SP/
p
}'`"
    os="$os`sed </etc/SuSE-release -n -e '/^CODENAME /{
s/.*=[ 	]*/ (/
s/$/)/
p
}'`"
    $debug && from=' [/etc/SuSE-release]'
elif [ -f /etc/centos-release ]
then
    # CentOS release 6.5 (Final)
    os=`sed </etc/centos-release -n -e '1{
s/ release //
p
}'`
    $debug && from=' [/etc/centos-release]'
elif [ -f /etc/redhat-release ]
then
    # CentOS release 5.6 (Final)
    # Red Hat Enterprise Linux Server release 6.6 (Santiago)
    # Red Hat Enterprise Linux Server release 7.1 (Maipo)
    #
    os=`sed </etc/redhat-release \
	-e 's/ release / /' \
	-e 's/Red Hat /RH/' \
	-e 's/Enterprise Linux /EL /'`
    $debug && from=' [/etc/redhat-release]'
elif [ -f /etc/lsb-release ]
then
    # Debian-based distros tend to use this way
    # DISTRIB_ID=LinuxMint
    # DISTRIB_RELEASE=12
    # DISTRIB_CODENAME=lisa
    #
    id=`sed </etc/lsb-release -n -e '/^DISTRIB_ID *= */s///p'`
    release=`sed </etc/lsb-release -n -e '/^DISTRIB_RELEASE *= */s///p'`
    codename=`sed </etc/lsb-release -n -e '/^DISTRIB_CODENAME *= */s///p'`
    os="$id $release ($codename)"
    $debug && from=' [/etc/lsb-release]'
elif [ -f /etc/debian_version ]
then
    # 6.0.1
    #
    os="Debian `cat /etc/debian_version`"
    $debug && from=' [/etc/debian_version]'
elif [ -f /etc/mandriva-release ]
then
    # Mandriva Linux release 2011.0 (Official) for x86_64
    #
    os=`sed </etc/mandriva-release -e 's/ release / /' -e 's/ (Official)/ /' -e 's/ for .*//'`
    $debug && from=' [/etc/mandriva-release]'
elif [ -f /etc/gentoo-release ]
then
    # Gentoo Base System release 2.0.3
    #
    os=`sed </etc/gentoo-release -e 's/ release / /' -e 's/ Base System / /'`
    $debug && from=' [/etc/gentoo-release]'
elif [ -f /etc/system-release ]
then
    # Fedora release 15 (Lovelock)
    #
    os=`sed </etc/system-release -e 's/ release / /'`
    $debug && from=' [/etc/system-release]'
elif [ -f /etc/os-release ]
then
    # NAME=openSUSE
    # VERSION = 12.1 (Asparagus)
    # or
    # NAME="Arch Linux"
    #
    name=`sed </etc/os-release -n -e '/^NAME *= */s///p'`
    name=`echo "$name" | sed -e 's/"//g'`
    ver=`sed </etc/os-release -n -e '/^VERSION *= */s///p'`
    os="$name $ver"
    $debug && from=' [/etc/os-release]'
elif [ -f /etc/release ]
then
    #    OpenSolaris 2010.05 snv_134b X86
    #    OpenIndiana Development oi_148 X86
    #
    try=`sed </etc/release -n -e '/Open[A-Z]/{
s/.*Open/Open/
s/ /~/
s/ /~/
s/ .*//
s/~/ /g
p
}'`
    if [ -n "$try" ]
    then
	os="$try"
	$debug && from=' [/etc/release]'
    fi
elif [ -f /etc/slackware-version ]
then
    os=`cat /etc/slackware-version`
fi
# pmcd.version 1 "3.6.2"
#
if which pmprobe >/dev/null 2>&1
then
    pcpver=`pmprobe -v pmcd.version 2>/dev/null | sed -e 's/"$//' -e 's/.*"//'`
    [ -z "$pcpver" ] && pcpver=unknown
else
    pcpver=unknown
fi

opts=''
if which pmpython >/dev/null 2>&1
then
    if pmpython --version >$tmp.out 2>&1
    then
	opts="$opts `sed -e 's/Python /py=/' <$tmp.out`"
    else
	opts="$opts py=none"
    fi
else
    opts="$opts py=none"
fi
if which getenforce >/dev/null 2>&1
then
    opts="$opts se=`getenforce | tr '[A-Z]' '[a-z]'`"
else
    opts="$opts se=none"
fi
opts=" [`echo $opts | sed -e 's/^ //'`]"

printf "$fmt" "`hostname | sed -e 's/\..*//'`" "$pcpver" "$arch" "$os" "$from" "$opts"

exit 0
