#!/bin/sh

MYDIR=`dirname $0`
GUESS="$MYDIR/config.guess"
if test ! -x "$GUESS" ; then
  echo "ERROR: Unable to find $GUESS" 1>&2
  exit 1
fi
HOST=`$GUESS`

experimental () {
  echo "****************************************************************************** " 1>&2
  echo "****************************************************************************** " 1>&2
  echo "  WARNING: the $HOST port is experimental " 1>&2
  echo "****************************************************************************** " 1>&2
  echo "****************************************************************************** " 1>&2
}

getabi () {
 if test -z "$ABI" ; then
  rm -f $MYDIR/abi
  ABI=`$CXX -o $MYDIR/abi $MYDIR/abi.cc 1>&2 && $MYDIR/abi`
  case "$ABI" in
    ILP32) echo "ABI is: $ABI"  1>&2 ;;
    LP64) echo "ABI is: $ABI"  1>&2 ;;
    *)
      echo "ERROR: Failed to find a known ABI ($ABI)." 1>&2
      echo "       Perhaps your C++ compiler is broken or creates unusable executables?" 1>&2
      exit 1
    ;;
  esac
 fi
}

checkbadabi () {
  getabi;
  if test "$ABI" != "$ABIWANT" ; then
    case "$ABI" in
      $ABIWANT) ;;
      *)  
        echo "ERROR: The $ABI ABI is not supported for building the UPC translator" 1>&2
        echo "         on $HOST." 1>&2
        echo "       Please select a C/C++ compiler that generates $ABIWANT executables." 1>&2
        echo "       Perhaps try: $ALTABIHINT" 1>&2
        exit 1
      ;;
    esac
  fi
}

requireILP32 () {
  if test -z "$ALTABIHINT" ; then
    ALTABIHINT='gmake CC="gcc -m32" CXX="g++ -m32"'
  fi
  ABIWANT="ILP32"
  checkbadabi;
}

requireLP64 () {
  if test -z "$ALTABIHINT" ; then
    ALTABIHINT='gmake CC="gcc -m64" CXX="g++ -m64"'
  fi
  ABIWANT="LP64"
  checkbadabi;
}

echo "Host is: $HOST" 1>&2
case "$HOST" in
  ia64-*-linux-* | x86_64-*-linux-*)  # Itanium/Opteron Linux host
	getabi;
	#requireLP64;
        case "$ABI" in
	  ILP32) echo "targia32_ia64_nodebug" ;; # 32-bit Opteron
	  LP64)  echo "build_ia64" ;;
      	esac
  ;;

  i686-*-linux-*) # 32 bit Linux host
  	echo "targia32_ia64_nodebug"
  ;;

  alpha*-*-osf*) # 64 bit Tru64/Alpha host
  	echo "targ_alpha_tru64"
  ;;
  
  powerpc-apple-darwin*) # PPC OSX 10.4 host
	requireILP32;
  	echo "targ_g4_osx"
  ;;

  powerpc-ibm-aix5*) # PPC AIX 5.x host
        ALTABIHINT='OBJECT_MODE=32 ; gmake CC="gcc -maix32" CXX="g++ -maix32"'
	requireILP32;
  	echo "targ_ppc_aix"
  ;;

  powerpc*-*-linux-*) # PPC Linux host
	experimental;
	getabi;
	requireLP64; # pending bug 1238
        case "$ABI" in
	  ILP32) echo "targia32_ia64_nodebug" ;;
	  LP64)  echo "build_ia64" ;;
      	esac
  ;;

  *) 
	echo "ERROR: Your platform: $HOST" 1>&2
	echo "       does not appear to be supported for hosting the UPC translator." 1>&2
	echo "       However, your platform is likely to still be supported by Berkeley UPC." 1>&2
	echo "       You should build the UPC runtime distribution and use the transparent" 1>&2 
	echo "       remote compilation feature to remote compile on a supported host." 1>&2
	echo "       See http://upc.lbl.gov for further instructions." 1>&2
	exit 1
  ;;
esac

exit 0

