#!/bin/sh

# bbmachine
# Format: bbmachine {OS}
# returns machine type with optional OS arguments.
# OS args not available for all platforms

#--------------------------------------------------------
# WE REALLY SHOULD TRY TO FIGURE WHAT MACHINE WE'RE ON.
#--------------------------------------------------------
# Redhat 6.2
# Linux linus 2.2.14-5.0smp #1 SMP Tue Mar 7 21:01:40 EST 2000 i686 unknown
# Redhat 7.2
# Linux aika 2.4.7-10 #1 Thu Sep 6 17:27:27 EDT 2001 i686 unknown

# SunOS styx 5.8 Generic_108528-13 sun4u sparc SUNW,Ultra-5_10

# HP-UX saphire B.11.11 U 9000/782 2005847114 unlimited-user license
# HP-UX diamond B.11.00 U 9000/800 2012989919 unlimited-user license

# AIX 4.3 - this makes no sense...
# AIX flan 3 4 000FF76D4C00
# Release info lives in /usr/lpp/bos/aix_release.level
# 4.3.3.0

#
# DO WE WANT OS-LEVEL?
#
if test "$#" -gt 0
then
	OSLEVEL=TRUE
fi

UNAME=`uname -a`
set $UNAME

if [ "$OSARG" = "" ]
then
	MACHINE="$1"		# THIS IS WHAT THE MACHINE THINKS IT IS
	MACHINE=`echo "$MACHINE" | tr '[A-Z]' '[a-z]' `
	# echo MACHINE IS $MACHINE
else
	MACHINE=$OSARG
fi

set `echo $3 | sed "s/[A-Z.\-]/\ /g"`
VERSION="${1}.${2}"
# echo "MACHINE: $MACHINE"
# echo "VERSION: $VERSION"

case $MACHINE
in
	linux )
		shiftval=4
		# ARE WE A REDHAT MACHINE?
		grep "Red" /etc/issue >/dev/null
		if test "$?" = "0"
		then
			# OSLEVEL FOR REDHAT MACHINES RETURNS
			# redhat AND RELEASE LEVEL FOR REDHAT
			# linux AND KERNEL VERSION OF EVERYTHING ELSE
			MACHINE="redhat"
			# Format: Red Hat Linux release 7.2 (Enigma)
			VERSION=`grep Red /etc/issue`
			if test "$VERSION"
			then
				# Non-enterprise version number is in column 5
				# Enterprise version number is in column 7
				set $VERSION
				OLDARGS=$*
				OLDIFS=$IFS
				IFS="."
				set bogus $5
				shift
				IFS=$OLDIFS
				VERSION=$1
				expr "$VERSION" + 0 >/dev/null 2>/dev/null
				if [ "$?" -ne 0 ]
				then
					shiftval=6
					OLDIFS=$IFS
					IFS="."
					set bogus $5
					shift
					IFS=$OLDIFS
					VERSION=$1
					MACHINE="redhatES"
				fi
				set $OLDARGS
			fi

		fi
		grep "SuSE[ 	]" /etc/issue >/dev/null
		if test "$?" = "0"
		then
			MACHINE="suse"
			# bbconfig expects "Welcome to SuSE Linux 7.3 (i386) - Kernel ..."
			# and got at least "SuSE" in /etc/issue
			# now, go get the version.
			set bogus `grep "SuSE[ 	]" /etc/issue` 2>/dev/null
			shift		# remove bogus
		fi
		grep "Fedora[ 	]" /etc/issue >/dev/null
		if test "$?" = "0"
		then
			MACHINE="fedora"
			# bbconfig expects "Fedora Core release 4 (Stentz)"
			# and got at least "Fedora" in /etc/issue
			# now, go get the version.
			set bogus `grep "Fedora[ 	]" /etc/issue` 2>/dev/null
			shift		# remove bogus
		fi
		if [ "$MACHINE" = "redhat" -o "$MACHINE" = "redhatES" -o "$MACHINE" = "suse" ]
		then
			# We're expecting the version at the 5th argument,
			# unless it's a RedHat ES install (it's the 7th arg)
			# so make sure there's that many arguments
			if test "$#" -ge "5"
			then
				# Remove the initial arguments
				shift $shiftval
				OLDIFS=$IFS
				IFS='.'
				set bogus $1		# Split 7.3 argument
				IFS=$OLDIFS
				shift
				# Test if it's a digit/number
				expr "$1" + 0 >/dev/null 2>&1
				retc1=$?
				expr "$2" + 0 >/dev/null 2>&1
				retc2=$?
				if [ $retc1 -eq 0 ]
				then
					if [ $retc2 -eq 0 ]
					then
						# It is
						VERSION=$1.$2
						SUBVERSION=$2
					else
						VERSION=$1.0
						SUBVERSION=0
					fi
				else
					VERSION=-1
					SUBVERSION=-1
				fi
			fi
		elif [ "$MACHINE" = "fedora" ]
		then
			VERSION=${4}.0
			SUBVERSION=0
		else
			# NON-REDHAT LINUX VERSIONS
			KERNEL=$VERSION
			grep "Caldera" /etc/issue > /dev/null
			if test "$?" = "0"
			then
				MACHINE="caldera"
			fi
			echo "$3" | grep "mdk" 2>&1 >/dev/null
			if [ "$?" -eq 0 ]
			then
				MACHINE="mandrake"
			fi
		fi
		;;

	sunos )
		#echo "VERSION=$VERSION"
		# Real SunOS are 4.X and less, Solaris are 5.X and up
		# Thanks to  Bill Earle <bille@imsi.com>
		VER=`echo $VERSION | sed "s/\./ /g"`
		set bogus $VER 2>/dev/null
		shift
		if test "$1" -ge "5"
		then
			MACHINE="solaris"
		fi
		;;

	hp-ux ) # HP VERSIONS ARE OK WITHIN 
		MACHINE="hpux"
		VER=`echo $VERSION | sed "s/\./ /g"`
		set bogus $VER 2>/dev/null
		shift
		VERSION="$1"		# hpux 11
		if [ "$VERSION" -eq 11 ]
		then
			if [ "$2" -ge 11 ]
			then
				VERSION="11i"
			fi
		fi
		;;

	bsdi )
		OLDIFS=$IFS
		IFS='.'
		set $3 >/dev/null 2>&1
		IFS=$OLDIFS
		if [ "$#" -gt 1 -a "$1" -ge 4 ]
		then
			if [ "$1" -gt 4 -o "$2" -ge 1 ]
			then
				MACHINE="bsdi4"
			fi
		fi
		;;

	osf* )
		MACHINE=osf
		;;

	irix* )
		MACHINE=irix
		;;

	aix* )
		MACHINE=aix

		if test "$OSLEVEL" = "TRUE"
		then
			if test -f "/usr/lpp/bos/aix_release.level"
			then
				VER=`cat /usr/lpp/bos/aix_release.level|sed "s/\./ /g"`
				set bogus $VER
				shift
				VERSION=${1}.${2}
			fi
		fi
		;;

	unixware* )
		VER=`echo $4 | sed "s/\./ /g"`
		set bogus $VER
		shift
		if test "$1" -ge "7"
		then
			MACHINE="unixware7"
		fi
		;;
esac

if test "$OSLEVEL" = ""
then
	VERSION=""
	echo "$MACHINE"
else
	if test "$KERNEL"
	then
		echo "linux${KERNEL}"
	else
		echo "${MACHINE}${VERSION}"
	fi
fi
