#!/bin/sh
#   hostype [-<shell>[e]]
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# This is a script that tries to figure out what sort of machine  and  OS  we #
# have  under  us.   It  should  output  a  series of commands to set several #
# variables.  This is not an easy problem, and this script will probably have #
# to  be  extended  for  various other Unix systems that we haven't seen yet. #
# Note that the default output is the "NAME=VALUE;" pattern, which should  be #
# useful for many random applications. The -make option lacks the ';', and is #
# the other simple choice.                                                    #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# The options say what sort of commands to generate:                          #
#    -sh      Bourne-shell variable assignments [default].                    #
#    -she     Bourne-shell environment variables.                             #
#    -csh     C-shell "set"    commands.                                      #
#    -cshe    C-shell "setenv" commands.                                      #
#    -fsh     JC's form-shell commands.                                       #
#    -perl    perl definitions.                                               #
#    -perle   perl environment definitions.                                   #
#    -make    make definitions.                                               #
#    -maked   make command-line definitions.                                  #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# The output is commands in the correct syntax to produce:                    #
#    SYSNAME is the machine's name, stripped of any "domain" stuff.           #
#            It may be `uname -s` or `uname -n` or `hostname`.                #
#    CPUTYPE says what sort of processor we are running on.                   #
#            It should be the name used in the system-build directories.      #
#    HOSTYPE is a code for the vendor type and version.                       #
#            It is intended for use in file names, and is short.              #
#    SYSTYPE is a code for the OS type and release                            #
#            It is intended for use in messages, and may be long.             #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Here's what a csh user might put in .login to invoke this script:           #
#    eval `.hostype -cshe`                                                    #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

PATH="$PATH:/sbin:/usr/sbin:/usr/ucb:/usr/local:/usr/bin/local"

# It seems almost everyone has hostname now:

sname=`hostname|sed -e "/\..*/s///"`

S=/tmp/$$.sys
opt='sh'
case "$1" in
	-[vx])	DBG='1'; shift;;
esac

if [ -f /bin/uname ];then
	t=`uname -s`
	htype=`echo $t | tr '[A-Z]' '[a-z]' `
	stype=`echo $t | tr '[a-z]' '[A-Z]' `
elif [ -f /usr/bin/uname ];then
	t=`uname -s`
	htype=`echo $t | tr '[A-Z]' '[a-z]' `
	stype=`echo $t | tr '[a-z]' '[A-Z]' `
else
	echo /bin/uname not found on `hostname`
fi

# If the "arch" command is present, it should tell us about the OS.

if [ -f /bin/arch ];then
	htype=`/bin/arch`
	stype=`echo $htype | tr 'a-z' 'A-Z'`
#else
#	echo /bin/arch not found on `hostname`
fi
# So far, all machines with "arch" also have "mach",  but  who  knows?
# Note  that  we  capitalize  its  output,  because the main use is in
# accessing the cpu-specific directories in the system-build area, and
# they usually have upper-case names.
if [ -f /usr/bin/mach ];then
	ctype=`/usr/bin/mach | tr 'a-z' 'A-Z'`
fi

# A useful clue is the kernel's name:

if	[ ! -n "$htype" -a ! -n "$ctype" ];then
	if [ -f /vmunix ];then				# "vmunix" kernel.
		if [ -f /usr/bin/uname ];then	# With uname, we are really in luck.
			if [ -n "$DBG" ];then echo '#-- /usr/bin/uname exists.';fi
			# Is is a SPARC processor?
			if	[ "$htype" = "" ];then
				file /usr/bin/uname | grep 'sparc .* executable' >/dev/null 2>&1 
				if [ $? = 0 ];then
					if [ -n "$DBG" ];then echo "#-- SPARC + Ultrix release 4.";fi
					# We don't yet know how to distinguish Sun4 from Solaris.
					ctype=SPARC
					htype=sun4
					stype=SUN4
				fi
			fi
			# Is is a VAX or MIPS processor?
			if	[ "$htype" = "" ];then
				file /usr/bin/uname | grep 'mipsel .* executable' >/dev/null 2>&1 
				if [ $? = 0 ];then
					if [ -n "$DBG" ];then echo "#-- MIPS + Ultrix release 4.";fi
					ctype=MIPS
					htype=mips4
					stype=ULTRIX4
				fi
			fi
			if	[ "$htype" = "" ];then
				file /usr/bin/uname |grep 'VAX .* executable' >/dev/null 2>&1 
				if [ $? = 0 ];then
					if [ -n "$DBG" ];then echo "#-- VAX + Ultrix release 4.";fi
					ctype=VAX
					htype=vax4
					stype=ULTRIX4
				fi
			fi
		fi
		if	test ! -n "$htype" \
				& (strings /vmunix |grep -s ULTRIX-32)
		then
			if [ -n "$DBG" ];then echo "#-- VAX + Ultrix release 3.";fi
			ctype=VAX
			htype=vax3
			stype=ULTRIX3
		fi
	fi
	if	[ ! -n "$htype" -a -f /unix ];then
		strings /unix >/tmp/kernelstrings
		if grep ESIX /tmp/kernelstrings >/dev/null
		then
			if [ -n "$DBG" ];then echo "#-- ESIX Sys/V.";fi
			rel=`uname -r`
			r=`expr $rel : '\([0-9]**\)`
			ctype=i386
			stype=sys5.$rel
			htype=esix$r
			rm /tmp/kernelstrings
		fi
	fi
	if	[ ! -n "$htype" -a -f /hp-ux ];then
		ctype=`file /hp-ux|sed -e 's".*:[ 	]*\([a-z0-9]*\)[ 	].*"\1"'`
		htype=hp
		stype=HP-UX
	fi
fi

# Almost all other systems call the kernel "/unix".  We now assume that  this
# name  isn't  helpful, and proceed with a series of other tests.  One way to
# tell is to look for our own installed execables

if	[ ! -n "$htype" ];then
	if	(test -f misc/vax3/hosttype )>/dev/null 2>&1 \
		&	(file misc/vax3/hosttype |grep 'pure executable')>/dev/null 2>&1  \
		&	(misc/vax3/hosttype )>/dev/null 2>&1
	then	
		if [ -n "$DBG" ];then echo "#-- VAX + Ultrix 3.";fi
		ctype=VAX
		htype=vax3
		stype=ULTRIX3
	elif	[ -f /vmunix ];then
		if strings /vmunix | grep '^SunOS Release' >$S
		then
			if [ -n "$DBG" ];then echo "#-- SunOS.";fi
			stype=SunOS`sed <$S -e 's"SunOS Release \([0-9.a-zA-Z]*\) .*"\1"'`
			rm -f $S
			ctype=`file /vmunix|sed -e 's".*:[ 	]*\([a-z0-9]*\)[ 	].*"\1"'`
			case "$ctype" in
				sparc) htype=sun4;;
				68010) htype=sun2;;
				68020) htype=sun3;;
				i386 ) htype=sun386;;
			esac
		fi
	fi
fi

# Now we generate the commands as requested.  There are a lot of syntaxes...

if [ -n "$htype" ];then
	if [ $# -lt 1 ];then set 'sh';fi
	for opt do
		case "$opt" in
			-[vx])	
				break;;
			-sh|sh)		# Bourne shell variables.
				echo "SYSNAME=$sname;"
				echo "CPUTYPE=$ctype;"
				echo "HOSTYPE=$htype;"
				echo "SYSTYPE=$stype;"
				break;;
			-she|she)	# Bourne shell environment.
				echo "SYSNAME=$sname;"
				echo "CPUTYPE=$ctype;"
				echo "HOSTYPE=$htype;"
				echo "SYSTYPE=$stype;"
				echo "export CPUTYPE HOSTYPE SYSTYPE;"
				break;;
			-csh|csh)	# C-shell variables.
				echo "set SYSNAME=$sname;"
				echo "set CPUTYPE=$ctype;"
				echo "set HOSTYPE=$htype;"
				echo "set SYSTYPE=$stype;"
				break;;
			-cshe|cshe)	# C-shell environment.
				echo "setenv SYSNAME $sname;"
				echo "setenv CPUTYPE $ctype;"
				echo "setenv HOSTYPE $htype;"
				echo "setenv SYSTYPE $stype;"
				break;;
			-fsh|fsh)	# JC's form-shell.
				echo "~(set SYSNAME $sname)"
				echo "~(set CPUTYPE $ctype)"
				echo "~(set HOSTYPE $htype)"
				echo "~(set SYSTYPE $stype)"
				break;;
			-make|make)	# Make symbols.
				echo "SYSNAME="$sname
				echo "CPUTYPE="$ctype
				echo "HOSTYPE="$htype
				echo "SYSTYPE="$stype
				break;;
			-maked|maked|-dmake|dmake)	# Make command-line definitions.
				echo "\-DSYSNAME="$sname
				echo "\-DCPUTYPE="$ctype
				echo "\-DHOSTYPE="$htype
				echo "\-DSYSTYPE="$stype
				break;;
			-perl|perl)	# Perl symbols.
				echo '$SYSNAME="'$sname'";'
				echo '$CPUTYPE="'$ctype'";'
				echo '$HOSTYPE="'$htype'";'
				echo '$SYSTYPE="'$stype'";'
				break;;
			-perle|perle)	# Perl environment.
				echo '$ENV{"SYSNAME"}="'$sname'";'
				echo '$ENV{"CPUTYPE"}="'$ctype'";'
				echo '$ENV{"HOSTYPE"}="'$htype'";'
				echo '$ENV{"SYSTYPE"}="'$stype'";'
				break;;
		esac
	done
fi
