#! /bin/sh
# $Id: petscarch,v 1.28 2001/06/14 23:41:02 balay Exp balay $ 
#
#   petscarch - Returns the machine's PETSc environmental variable, PETSC_ARCH.
#
#       If PETSC_DIR is set then this returns the most recently installed
#       PETSC_ARCH suitable for running on this machine (for example linux_intel).
#       If PETSC_DIR is not set or PETSc has not yet been installed it returns
#       the most basic PETSC_ARCH for the given machine (for example linux)
#
#   You can place the command
#      setenv PETSC_ARCH `${PETSC_DIR}/bin/petscarch`
#   in a .cshrc or .tcshrc file if using the csh or tcsh shell. Or
#      PETSC_ARCH=`${PETSC_DIR}/bin/petscarch` ; export PETSC_ARCH
#   in a .sh or .profile file if using the sh or bash shell 
#
#    Thus, if you have several machines of different types that share the same filesystem,
#   then when you log into any of them, the PETSC_ARCH will always be set correctly.
# 
if [ -f /bin/uname ]; then
    LARCH=`/bin/uname -s`
    MARCH=`/bin/uname -m`
    VARCH=`/bin/uname -r`
elif [ -f /usr/bin/uname ]; then
    LARCH=`/usr/bin/uname -s`
    MARCH=`/usr/bin/uname -m`
    VARCH=`/usr/bin/uname -r`
else
    echo "Unable to determine architecture"
    LARCH="unknown"
    MARCH="unknown"
    VARCH="unknown"
fi

# change B.10.01 -> 10.01
VARCH=`echo $VARCH | sed 's/[A-Z,.]*//'`
# grab 10 from 10.01
MajorVersion=`expr "$VARCH" : "\([0-9]*\)"`

if [ "$LARCH" = "AIX" ]; then
    LARCH="rs6000"
elif [ "$LARCH" = "RIOS" ]; then
    LARCH="rs6000"
elif [ "$LARCH" = "HP-UX" ]; then
    if [ "$MajorVersion" =  11 ]; then
        LARCH="hpux64"
    else
        LARCH="hpux"
    fi
elif [ "$LARCH" = "FreeBSD" ]; then
    LARCH="freebsd"
elif [ "$MARCH" = "CRAY Y-MP" ]; then  # Crays are funny - using MARCH
    LARCH="t3d"
elif [ "$MARCH" = "CRAY T3E" ]; then
    LARCH="t3e"
elif [ "$LARCH" = "i86pc" ]; then
    LARCH="solaris_x86"
elif [ "$LARCH" = "iris4d" ] || [ "$LARCH" = "IRIX" ]; then
    if [ "$MajorVersion" =  5 ]; then
        LARCH="IRIX5"
    else
        LARCH="IRIX"
    fi
elif [ "$LARCH" = "SunOS" ]; then
    if [ "$MajorVersion" =  4 ]; then
        LARCH="sun4"
    else
        LARCH="solaris"
    fi
elif [ "$LARCH" = "Linux" ]; then
    if [ "$MARCH" = "alpha" ]; then
        LARCH="linux_alpha"
    elif [ "$MARCH" = "ppc" ]; then
        LARCH="linux_ppc"
    else
        LARCH="linux"
    fi
elif [ "$LARCH" = "OSF1" ]; then
    LARCH="alpha"
elif [ "$LARCH" = "Hurd" ]; then
        LARCH="hurd"
elif [ "$LARCH" = "Rhapsody" ] || [ "$LARCH" = "Darwin" ]; then
        LARCH="macx"
elif [ "$LARCH" = "5000" ]; then
        LARCH="UXPV"
else # maybe use a case statement instead of if test for the entire script?
    case "$LARCH" in
        CYGWIN*)
        LARCH="win32"
        ;;
    esac
fi

if [ $# -gt 0 ]; then
  if [ "${PETSC_DIR}" = "" ]; then
     if [ -f `pwd`"/bin/petscarch" ]; then
        PETSC_DIR=`pwd`
        export PETSC_DIR
        echo " "
        echo "Please remember to set your environmental variable PETSC_DIR to " ${PETSC_DIR}
        echo " "
     else
        echo 'You must indicate the path of PETSc with the environmental variable PETSC_DIR to get suggestions'
        exit 0
     fi
   else
      # Test if the PETSC_DIR is a valid dir
      if ! [ -f ${PETSC_DIR}/include/petscversion.h ]; then
        echo "PETSC_DIR is currently set to $PETSC_DIR which is invalid. Please set the correct value."
        exit 0
      fi
  fi
  
  # try to get configure arch
  ${PETSC_DIR}/bin/configarch > /dev/null 2>&1
  if [ $? -eq 0 ]; then
     echo 'Automatic configuration of PETSc use the following PETSC_ARCH' 
     echo '  ' `${PETSC_DIR}/bin/configarch`
  fi
  echo 'Manual configuration of PETSc select one of the following PETSC_ARCH choices'
  ls -d ${PETSC_DIR}/bmake/${LARCH}* | sed -e "s?${PETSC_DIR}/bmake/\([_a-zA-Z0-9]*\)?   \1?g"
  exit 0
fi

# Now check if there is an installed set of libraries

if [ "${PETSC_DIR}" != "" ]; then
    ls ${PETSC_DIR}/lib/libg/${LARCH}*/libpetscdm*.* | head -1 > /dev/null 2>&1
    if [ $? -eq 0 ]; then
	LARCH=`ls -t ${PETSC_DIR}/lib/libg/${LARCH}*/libpetscdm*.* | head -1 | sed -e "s?${PETSC_DIR}/lib/lib[gO_c+omplex]*/\([_a-zA-Z0-9\-]*\)/libpetscdm.[a-zA-Z0-9]*?\1?g"`
    else 
	# check if there is a configure installed version
        ${PETSC_DIR}/bin/configarch > /dev/null 2>&1
        if [ $? -eq 0 ]; then
           TARCH=`${PETSC_DIR}/bin/configarch`
           ls ${PETSC_DIR}/lib/libg/${TARCH}*/libpetscdm*.* > /dev/null 2>&1
           if [ $? -eq 0 ]; then
	       LARCH=$TARCH
           fi
        fi
    fi
fi


echo $LARCH
exit 0
