#! /bin/sh
# set -x
#
if [ "$1" = "-echo" ] ; then 
    set -x
    shift
fi
#  Returns the arch of the machine. This file is from MPICH.
#
# First, try some special cases:
if [ -d "/dev/elan" ] ; then
  FARCH="meiko"
elif [ -f /usr/bin/uxpm ] && /usr/bin/uxpm ; then
  FARCH="UXPM"
elif [ -f /usr/bin/uxpv ] && /usr/bin/uxpv ; then
  FARCH="uxpv"
fi
if [ -n "$FARCH" ] ; then
    echo $FARCH
    exit 0
fi
#
# Try to find uname
for dir in /bin /usr/bin /usr/local/bin ; do
    if [ -x $dir/uname ] ; then
	UNAME="$dir/uname"
        break
    fi
done
#
# Get uname -s, uname -m, and arch values
#
if [ -n "$UNAME" ] ; then
    ARCHLIST="`uname -s`"
    ARCHLIST="$ARCHLIST `uname -m`"
fi
#
# Get ARCH variable name
if [ -n "$ARCH" ] ; then
    ARCHLIST="$ARCHLIST $ARCH"
fi
#
# Get arch command
if [ -x /bin/arch ] ; then
    ARCHLIST="$ARCHLIST `/bin/arch`"
elif [ -x /usr/local/bin/arch ] ; then
    ARCHLIST="$ARCHLIST `/usr/local/bin/arch`"
fi
#
# GARCH is a guess if we don't find something better
GARCH=
# Now, weed through all of these values until we find something useful.
for LARCH in $ARCHLIST ; do
   # Remove blanks
   LARCH=`echo $LARCH | sed 's/ //g'`
   # Get the first 4 characters (you'd be surprised)
   #   LARCH4=`expr "$LARCH" : "\(....\)"`
   #   LARCH6=`expr "$LARCH" : "\(......\)"`
   case $LARCH in
        SUPER-UX) FARCH=SX4; break ;;
	AIX|RIOS) FARCH=rs6000; break ;;
	HP-UX) 
 	if [ -a /dev/kmem ] ; then
 	    FARCH=hpux ;
 	else
 	    FARCH=sppux ;
 	fi
        break ;;

	IRIX64|IRIX) FARCH=$LARCH ; break ;;
 
        Linux)  
	    # Pick between i86, alpha, and PowerPC LINUX
            LINUXARCH=`$UNAME -m`
	    case $LINUXARCH in 
		*alpha*)
		FARCH=LINUX_ALPHA ; break ;;
		*Power*|*power*)
		FARCH=LINUX_PPC ; break ;;
		*86*)
		FARCH=LINUX ; break ;;
		*)
		# Hope for the best
		FARCH=LINUX ; break ;;
	    esac
            break ;;
	i586|i486|i686|i86pc)
	    GARCH=$LARCH 
	    if [ -n "$UNAME" ] ; then
  	        SysName=`$UNAME -s`
  	        if [ "$SysName" = "SunOS" ] ; then
	            FARCH="solaris86"
                else
		    # Try to get a short name (eliminate any numbers)
		    # (include the hyphen for any subsequent tests.
		    ShortSysName=`expr "$SysName" : "\([A-Za-z_-]*\)"`
		    if [ "$ShortSysName" = "CYGWIN_NT" -o \
			 "$ShortSysName" = "CYGWIN_NT-" ] ; then
			FARCH="CYGWIN_NT"
		    fi
		fi
	    fi
	;;
	sun4*|SunOS)
	Version=`$UNAME -r`
        # In "improving" SunOS, the useful feature of "substr" was withdrawn 
        # from expr.  Can't let the users have life too easy, can we?  This 
        # means that we can't just use 
        #   set MajorVersion = `expr substr $Version 1 1`
        # because it won't work on Solaris systems.  The following should work
        # on both:
        MajorVersion=`expr "$Version" : "\(.\)"`
        if [ "$MajorVersion" -ge 5 ] ; then
	    # Check for solaris86
            if [ -n "$UNAME" ] ; then
                hardware=`$UNAME -i`
		case $hardware in
		*86*) FARCH="solaris86" ;;
		*)    FARCH="solaris"   ;;
                esac
            else
                FARCH=solaris
            fi
	else
	    FARCH=sun4
	fi
	break ;;
	hp9000*|hp7000*)
 	if [ -a /dev/kmem ] ; then
 	    FARCH=hpux ;
 	else
 	    FARCH=sppux ;
 	fi
 	break ;;
	mips|dec-5000)   FARCH=dec5000 ; break ;;
        IP12|iris-4d)    GARCH=IRIX ;;
        cray|CRAY*)      GARCH=CRAY ;;
        next)            FARCH=NeXT ; break ;;	
	KSR1|KSR2)       FARCH=ksr ; break ;;	
        FreeBSD)         FARCH=freebsd ; break ;;
	NetBSD)          FARCH=netbsd ; break ;;
	i386)            GARCH=ipsc2 ;;
	ULTRIX|RISC)     GARCH=dec5000 ;;
	OSF*)            
	    # Guess that this is a Compaq Alpha running some version of
            # Tru64; 
            machtype=`uname -m`
	    if [ "$machtype" = "alpha" ] ; then
                GARCH=ALPHA
            fi
        break;;
	Darwin|Macintosh) 
	    # We must distinquish the processor architecture
            FARCH=freebsd_ppc ; break ;;
   esac
   LLARCH=$LARCH
done
if [ -z "$FARCH" ] ; then
    FARCH=$GARCH
    if [ -z "$FARCH" ] ; then
	FARCH=$LLARCH
    fi
    if [ -z "$FARCH" ] ; then
	FARCH=unknown
    fi
fi
echo $FARCH
exit 0
