#!/bin/sh
#
# Copyright (c) 1992-1998 Michael A. Cooper.
# This software may be freely distributed provided it is not sold for 
# profit and the author is credited appropriately.
#
# $Id: ostype,v 1.18 1998/04/10 01:51:03 mcooper Exp $
#
# Determine type of OS
#

#
# Find uname program.
#
if [ -f /usr/bin/uname ]; then
	unameprog=/usr/bin/uname
elif [ -f /bin/uname ]; then
	unameprog=/bin/uname
fi

#
# Determine our OS name if we can.
#
if [ "${unameprog}" ]; then
	osname="`${unameprog} -s`"
fi

#
# Check for Convex SPP special handling
#
if [ "${osname}" = "HP-UX" ]; then
	if [ -x /bin/sysinfo ]; then
		altname="`/bin/sysinfo -sv | awk '{print $1}' | sed -e 's;_.*;;' 2>/dev/null`"
		if [ ! -z "${altname}" ]; then
			osname="${altname}"
		fi
	fi
fi

#
# Try stupid file checks
#
if [ -z "${osname}" ]; then
	if [ -d /NextApps ]; then
		if [ -f /usr/bin/hostinfo ]; then
			mver="`/usr/bin/hostinfo | grep -i 'next mach' | awk '{print $3}' | sed -e 's/\..*//'`"
			osname="nextstep${mver}"
		else
			osname="nextstep2"
		fi
	elif [ -d /usr/alliant ]; then
		osname="concentrix"
	else
		echo "Unable to determine your OS type.";
		exit 1;
	fi
fi

osname="`echo ${osname} | tr '[A-Z]' '[a-z]' | sed -e 's;-;;g'`"

#
# Get OS Version
#
case "${osname}" in
irix64)
	osname="irix";
	osver="`${unameprog} -r`"
	;;
linux)
	osver="`${unameprog} -r`"
	;;
sunos)
	if [ -z "${unameprog}" ]; then
		echo "No uname program found."
		exit 1
	fi
	osver="`${unameprog} -r`"
	osvernodot=`echo $osver | awk -F. '{print $1 $2}'`
	;;
ultrix|irix)
	if [ -z "${unameprog}" ]; then
		echo "No uname program found."
		exit 1
	fi
	osver="`${unameprog} -r`"
	;;
aix)
	osver="`${unameprog} -v`"
	if [ "$osver" -eq "4" ]; then
	    aixosm="`${unameprog} -r`"
	    osver=${osver}.${aixosm}
	fi
	;;
hpux)
	osver="`${unameprog} -r| sed -e 's;^[A-Za-z]\.;;' -e 's;^[0];;'`"
	;;
sppux)
	osver="`/bin/sysinfo -sv | awk '{print $2}'`"
	;;
concentrix)
	# We don't care what the os version is
	osver=""
	;;
linux)
	osver="`${unameprog} -r`"
	;;
esac

#
# Get Application Architecture
#
case "${osname}" in
aix)
    # Hard code since we don't know how else to get this
    apparch="powerpc"
    ;;
hpux)
    # Hard code since we don't know how else to get this
    apparch="`${unameprog} -m | sed -e 's;/.*;;' -e 's;9000;pa-risc;'`"
    ;;
linux)
    apparch="`${unameprog} -m`"
    ;;
*)
    apparch="`${unameprog} -p`"
    ;;
esac

#
# Get CPU Architecture
#
case "${osname}" in
sunos)
    cpuarch="`${unameprog} -m`"
    ;;
*)
    # Doesn't matter on other platforms
    cpuarch=${apparch}
    ;;
esac

if [ ! -z "${osver}" ]; then
	osmver=`echo $osver | sed -e 's;\..*;;g'`
	if [ -z "${osvernodot}" ]; then
	    osvernodot=`echo $osver | sed -e 's;\.;;g'`
	fi    
fi

echo "${osname} ${osver} ${osvernodot} ${osname}${osmver} ${osmver} ${apparch} ${cpuarch}"
