#!/bin/sh

# Copyright 10/15/04 Sun Microsystems, Inc. All Rights Reserved
#
# @(#)perftools_whichami.ver.sh 1.6 04/10/15 SMI
#

##########################################################################
#
# Usage
#
Usage()
{
    scriptname=`basename $0`

    echo "$scriptname: Determine the machine name and OS version, etc."
    echo " "
    echo "USAGE: $scriptname [-a] [-o] [-O] [-c] [-h] [-V]"
    echo "      default       Print extended machine name, including os"
    echo "      -a            Print all versions of the strings"
    echo "      -o            Print operating system version <os>"
    echo "      -O            Print user-friendly operating system version <os>"
    echo "      -c            Print cpu count"
    echo "      -h            Print this help message and exit"
    echo "      -V            Print a version string and exit"
    echo " Exit status:"
    echo "    0 - ok"
    echo "    1 - unrecognized OS"
}

# This string is here for the 'version' command:
# It gets sed'ed in at build-time:
# PRODVER="@(#)RELEASE VERSION Sun Performance Analyzer 7.4 122141-01 2006/07/12"

# resolve symlinks to find directory of this script
#

PRG=$0
while [ -h "$PRG" ]; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '^.*-> \(.*\)$' 2>/dev/null`
    if expr "$link" : '^/' > /dev/null 2>/dev/null; then
	PRG="$link"
    else
	PRG="`dirname $PRG`/$link"
    fi
done

progdir=`dirname $PRG`
fdhome="$progdir/.."

osmode=0
debug=0

while getopts aocOhDV option
do
    case $option in
	a) osmode=-1;;

	o) osmode=1;;

	O) osmode=2;;

	c) osmode=3;;

	h) Usage
	    exit 0;;

	D) debug=1;;

	V) echo `basename $PRG`: "Sun Performance Analyzer 7.4 122141-01 2006/07/12"; exit 0;;

	\?) echo "Unknown option"
	    exit 1;;
    esac
done

if [ $debug -eq 1 ]; then
    echo
    echo "cat /etc/release:"
    cat /etc/release 2>/dev/null
    echo
    echo "cat /etc/SuSE-release:"
    cat /etc/SuSE-release 2>/dev/null
    echo
    echo "cat /etc/sun-release:"
    cat /etc/sun-release 2>/dev/null
    echo
    echo "cat /etc/redhat-release:"
    cat /etc/redhat-release 2>/dev/null
    echo
    echo "uname -s"
    uname -s
    echo
    echo "uname -r"
    uname -r
    echo
    echo "uname -v"
    uname -v
    echo
    echo "uname -m"
    uname -m
    echo
    echo "uname -p"
    uname -p
    echo
    echo "uname -i"
    uname -i
    echo
    echo "uname -o"
    uname -o
    echo
    echo
fi

OSNAME=UNKNOWN
OSARCH=UNKNOWN
FLAVOR=`uname -a | /usr/bin/cut -d" " -f1`
BITS=32-bit

if [ "$FLAVOR" = "SunOS" ]; then
    OSARCH=SunOS
    MACHINE=`hostname`_`uname -p`_`uname -r`
    OS=`uname -r`
    if [ "`/usr/bin/isalist | grep sparcv9`" != "" ]; then
	BITS=64-bit
    elif [ "`/usr/bin/isalist | grep amd64`" != "" ];  then
	BITS=64-bit
    fi
    NCPUS=`/usr/sbin/psrinfo | wc -l | sed 's/^ *//'`
    OSNAME="SunOS-$BITS, $NCPUS CPUs, `uname -p` `uname -r`"
fi

if [ "$FLAVOR" = "Linux" ]; then
    if [ "`uname -m  | grep _64`" != "" ]; then
	BITS=64-bit
    fi
    NCPUS=`cat /proc/cpuinfo | grep processor | wc -l | sed 's/^ *//'`
fi

if [ -r /etc/SuSE-release ]; then
    OSARCH=Linux
    VERSION=`grep VERSION /etc/SuSE-release | /usr/bin/cut -d" " -f3`

    if [ -r /etc/sun-release ]; then
	# distinguish Sun Desktop and Sun Java Desktop
	J=`grep "Sun Java" /etc/sun-release`
	if [ -n "$J" ]; then
	    MACHINE=`hostname -s`_`uname -m`_JDS_$VERSION
	    OS=JDS_$VERSION
	    OSNAME="Sun Java Desktop Linux-$BITS, $NCPUS CPUs, $VERSION"
	else
	    MACHINE=`hostname -s`_`uname -m`_SDS_$VERSION
	    OS=SDS_$VERSION
	    OSNAME="Sun Desktop Linux-$BITS, $NCPUS CPUs, $VERSION"
	fi
    else
	MACHINE=`hostname -s`_`uname -m`_SuSE_$VERSION
	OS=SuSE_$VERSION
	OSNAME="SuSE Linux-$BITS, $NCPUS CPUs, $VERSION"
    fi
fi

if [ -r /etc/redhat-release ]; then
    OSARCH=Linux
    XFLAVOR=`/usr/bin/cut -d" " -f3 /etc/redhat-release`
    if [ "$XFLAVOR" = "Enterprise" ]; then
	MACHINE=`hostname -s`_`uname -m`_RH_`/usr/bin/cut -d" " -f5 /etc/redhat-release`_`/usr/bin/cut -d" " -f7 /etc/redhat-release`
	OS=RH_`/usr/bin/cut -d" " -f5 /etc/redhat-release`_`/usr/bin/cut -d" " -f7 /etc/redhat-release`
	OSNAME="RedHat Enterprise Linux-$BITS, $NCPUS CPUs, `/usr/bin/cut -d" " -f5 /etc/redhat-release` `/usr/bin/cut -d" " -f7 /etc/redhat-release`"
    elif [ "$XFLAVOR" = "Linux" ]; then
	MACHINE=`hostname -s`_`uname -m`_RH_`/usr/bin/cut -d" " -f5 /etc/redhat-release`
	OS=RH_`/usr/bin/cut -d" " -f5 /etc/redhat-release`
	OSNAME="RedHat Linux-$BITS, $NCPUS CPUs, `/usr/bin/cut -d" " -f5 /etc/redhat-release`"

    else
	MACHINE=`hostname -s`_`uname -m`_RH_UNK
	OS=RH_UNK
	OSNAME="RedHat Linux-$BITS, $NCPUS CPUs, Unrecognized Version"
    fi
fi

if [ "$OSARCH" = "UNKNOWN" ]; then
    echo "-->`hostname` has an unrecognized OS"
    exit 1
else
    if [ $osmode -eq -1 ]; then
	echo "MACHINE: $MACHINE"
	echo "OS: $OS"
	echo "OSNAME: $OSNAME"
	echo "NCPUs: $NCPUS"
    elif [ $osmode -eq 1 ]; then
	echo "$OS"
    elif [ $osmode -eq 2 ]; then
	echo "$OSNAME"
    elif [ $osmode -eq 3 ]; then
	echo "$NCPUS"
    else
	echo "$MACHINE"
    fi
fi
