#!/bin/sh 

# converted to sh, (12/14/2007)

# Based on parts of tcsh's tc.vers.c && PVM's aimk

# Originally hacked by Bernd Mohr
# Modified by Pete Beckman (3/2/95)
# Modified by Lars Thomas Hansen (2/27/95); very minor fixes for Solaris

# Command line parameters:
# -x provide cross-development architecture name (for cm?, rs6k, etc)
# -l provide long name
# -s SPECIAL.  Used to differentiate two very similar architectures

#echo foo
ARCHTESTFILE=$0.c
ARCHLISTFILE=$0.txt
ARCH=
XDEV=
SPECIAL=

# TAU_CCOM may be defined by the caller to the name of the c compiler
# for this user and system, as given on the command line.
#
# Not everyone uses gcc.

if [ "x$TAU_CCOM" = "x" ]; then
    TAU_CCOM=cc
fi

if [ `uname -s ` = "Darwin" ];  then
    TAU_CCOM=c++
    ARCH=apple
fi

# check for ARM linux so it covers uname -m output such as armv7l or arm*
uname_output=`uname -m`
match_string="aarch64"
test_arg=${uname_output#$match_string}
if [ "y$test_arg" != "y$uname_output" ]
then
    TAU_CCOM=gcc
    ARCH=arm64_linux
fi
# if you are on a Cray XC system, use craycnl as the architecture
if  [ "x$CRAY_CPU_TARGET" != "x" ]; then
  ARCH=craycnl
fi

match_string="arm"
test_arg=${uname_output#$match_string}
if [ "y$test_arg" != "y$uname_output" ]
then
    TAU_CCOM=gcc
    ARCH=arm_linux
    if [ `uname -s ` = "Darwin" ]; then 
      ARCH=arm64_apple
    fi
fi

match_string="ppc64le"
test_arg=${uname_output#$match_string}
if [ "y$test_arg" != "y$uname_output" ]
then
    TAU_CCOM=gcc
    ARCH=ibm64linux
fi

#if [ -d /usr/linux-k1om-* ]
#then
#    ARCH=mic_linux
#fi

if [ `uname -s ` = "HI-UX/MPP" ]; then 
      ARCH=`$TAU_CCOM $MSG_EQ_E -E $ARCHTESTFILE | sed -e '/^#/d' -e '/^[ ]*$/d' -e 's/^ARCH//'` 
else
  if [ "x$ARCH" = "x" ]; then
# ARCH has not been assigned yet.
    if [ "x$PE_ENV" = "xCRAY" ]; then
      TAU_W_FLAG="" 
    else
      TAU_W_FLAG=-w
    fi
    if [ "x$TAU_CCOM" = "xmpicc" ]; then  # fix for PGI/18 mpicc 
      ARCH=`$TAU_CCOM $TAU_W_FLAG -E $ARCHTESTFILE | grep ARCH | sed -e '/^#/d' -e '/^[ ]*$/d' -e 's/^ARCH//'` 
    else # Redirect warnings to standard out.
      ARCH=`$TAU_CCOM $TAU_W_FLAG -E $ARCHTESTFILE 2>&1 | grep ARCH | sed -e '/^#/d' -e '/^[ ]*$/d' -e 's/^ARCH//'` 
    fi

    if [ "$TAU_CCOM" = "mcc" ] ; then
      ARCH=`$TAU_CCOM $TAU_W_FLAG -E $ARCHTESTFILE | sed -e '/^#/d' -e '/^[ ]*$/d' -e 's/^ARCH//' | tail -1` 
    fi

    if [ "x$ARCH" = "x" ] ; then 
	  exit
    fi
  fi
fi

# Check for brain-dead solaris compiler
if [ "$ARCH" = "sun4" ]; then
    VER=`uname -r | cut -c0-2`
    if [ $VER = "5." ]; then
	ARCH=solaris2
    fi
fi


# Check for SGI Symmetric Multiprocessing engine
if [ "$ARCH" = "sgi4k" -o "$ARCH" = "sgi8k" ]; then
  # Run "hinv" and check for the number of processors
    /bin/hinv 2>&1 | /usr/bsd/head -1 2>&1 | /bin/grep "^1 " &> /dev/null
    if [ $? = 1 ]; then
	XDEV="sgimp $XDEV"
    fi
fi      

# Check for Meiko CS2
if [ "$ARCH" = "solaris2" -a -d "/opt/MEIKOcs2" ]; then
    XDEV="cs2 $XDEV"
fi

# Check for cray-t3d xdev environment for Cray C90
if [ "$ARCH" = "c90" -a -d "/mpp/bin" ]; then
    XDEV="t3d $XDEV"
fi

# Check for Convex SPP engine
if [ "$ARCH" = "hp9000s800" ]; then
    if [ -d /usr/convex ]; then
	XDEV="cnxspp $XDEV"
    fi
fi

# Check for RS6000 based IBM SPx
if [ "$ARCH" = "rs6000" ]; then
    if [ -f /bin/mpcc ]; then
	XDEV="sp1 $XDEV"
    fi
fi

if [ "$ARCH" = "unknown" ]; then
#See if users path finds an 'arch' command, if so, use it! (a little sloppy)
    arch &> /dev/null
    if [ $? != 0 ]; then
#This machine does not have an 'arch' command
#Or at least one that correctly sets the arch

#Try another guess....
	if [ -e /usr/bin/getcube ]; then
	    ARCH=i860 
	fi
    else
# 'arch' command found, use it!
	ARCH=`arch`
    fi

    if [ "$ARCH" = "unknown" ]; then
	if [ `uname -s` = "FreeBSD" ]; then
	    ARCH=freebsd
	fi
    fi

    if [ $# = 1 ]; then
	if [ "$1" = "-x" ]; then
	    if [ "x$XDEV" = "x" ]; then
		echo none
		exit 1
	    else
		echo $XDEV
		exit 0
	    fi
	else
	    if [ "$argv[1]" = "-l" ]; then
		grep $ARCH $ARCHLISTFILE
		if [ $ARCH = "unknown" ]; then
		    exit 1
		else
		    exit 0
		fi
	    else
		if [ "$1" = "-s" ]; then
		    if [ "x$SPECIAL" = "x" ]; then
			echo none
			exit 1
		    else
			echo $SPECIAL
			exit 0
		    fi
		fi
	    fi
	fi
    fi
fi

echo $ARCH
if [ "$ARCH" = "unknown" ]; then
    exit 1
else
    exit 0
fi


