#!/bin/sh

# $Id: getPlatform,v 1.5 1995/11/03 10:35:43 svein Exp $

# Determine XITE hosttype/platform

# INPUTS:
#   1 : Suggested XITE hosttype/platform (if any)
#   2 : Directory for utility scripts echonl, testPlatform and typePlatform,
#	default: `pwd`
#   3 : Non-empty if dialog is to appear on stderr. This makes it possible
#       to have a dialog and return a value when the script is called
#	within backquotes (``)
#   4 : Non-empty if no dialog should take place. The exit value will
#       signal success or failure.

if test "$#" -lt 1
then
    XITE_HOSTTYPE=""
else
    # Skip leading blanks and return first word.
    XITE_HOSTTYPE=`expr "$1" : '[ ]*\([^ ]*\)'`
fi

utildir=`expr "$2" : '[ ]*\(.*\)'`
dest=`expr "$3" : '[ ]*\(.*\)'`
diag=`expr "$4" : '[ ]*\(.*\)'`

if test -z "$utildir"
then
    utildir="."
fi
if test -n "$dest"
then
    dest="1>&2"
fi

ECHONL=$utildir/echonl
TESTPLATFORM=$utildir/testPlatform
TYPEPLATFORM=$utildir/typePlatform

fine=0; ok=0
while test $fine -ne 1 
do
    if test -z "$XITE_HOSTTYPE"
    then

	XITE_HARDWARE=`uname -m`
	XITE_HOSTTYPE=`uname -s`
	RELEASE=`uname -r`

	if test "$XITE_HOSTTYPE" = "SunOS"
	then
	    if test `expr $XITE_HARDWARE : 'sun3.*'` -ge 4
	    then
		XITE_HOSTTYPE="SunOSSun3"
	    elif test "$RELEASE" -eq 5
	    then
		XITE_HOSTTYPE="SunOS5"
	    fi
	    fine=1; ok=1
	elif test "$XITE_HOSTTYPE" = "Linux"
	then
	    if test "$XITE_HARDWARE" = "i486" -o "$XITE_HARDWARE" = "i586"
	    then
		XITE_HOSTTYPE=`expr $XITE_HARDWARE : 'i\(.*\)'`
		XITE_HOSTTYPE="Linux$XITE_HOSTTYPE"
	    else
		XITE_HOSTTYPE="Linux"
	    fi
	    fine=1; ok=1
	fi
    fi

    $TESTPLATFORM $XITE_HOSTTYPE
    if test "$?" -eq 0
    then
	fine=1; ok=1
    elif test -z "$diag"
    then
	eval "echo 'Unknown platform $XITE_HOSTTYPE.'" $dest
	eval "echo 'Indicate type of platform to install on, choose from'" \
		$dest
	$TYPEPLATFORM $dest

	eval "$ECHONL 'Give platform: '" $dest
	read XITE_HOSTTYPE
    else
	fine=1; ok=0
    fi
done

if test "$ok" -eq 1
then
    echo $XITE_HOSTTYPE
    exit 0
else
    exit 1
fi
