#!/bin/sh 
#set -xv

# configure script for MPICH2

#------------------------------------------------------------------------------
#  set up usage statement:
#------------------------------------------------------------------------------
usage(){
cat<<EOD

Usage: ./configure_mpich2 [flags] compiler

    where compiler is one of:

        gnu (=gcc/gfortran), intel (=icc/ifort), pgi (=pgcc/pgf90),
        solaris (=cc/f90)

    Option flags:
      -static     Create statically linked executables (not recommended for
                    MacOSX)
      -cygwin     Modifications for cygwin/windows

    Note: See http://www.mcs.anl.gov/research/projects/mpich2/downloads/index.php?s=downloads
          for information on how to obtain MPICH2.
EOD

exit 1;
}

static='--enable-dynamiclibs'
libkind='--enable-shared --enable-sharedlibs=gcc'
while [ $# -gt 0 ]; do
    case "$1" in

        -static)       static='' ;;
        -cygwin)       libkind='--enable-sharedlibs=cygwin-gcc';;
        -*) usage ;;

        *) if [ $# -gt 1 ]; then
             usage
           fi
           compiler=$1 ;;
    esac
    shift
done

if [ `uname -s|awk '{print $1}'` = "Darwin" ]; then
    libkind='--enable-shared --enable-sharedlibs=osx-gcc LDFLAGS=-L/usr/X11/lib'
fi

workdir=`pwd`
AMBERHOME=`dirname \`dirname $workdir\``
echo "Setting AMBERHOME to $AMBERHOME"
echo " "

case "$compiler" in

#################### gcc #######
gnu)
    cc=gcc
    cflags=-fPIC
    cplusplus=g++
    ocflags=-O3
    fc=gfortran
    fflags="-O0 -fPIC"

    ;;

#################### icc #######
intel)
    cc=icc
    cflags=-fpic
    cplusplus=icpc
    ocflags=-O2
    fc=ifort
    fflags="-O0 -fpic"

    ;;

#################### Portland Group #######
pgi)
    cc=pgcc
    cflags=-fpic
    cplusplus=pgCC
    ocflags=-O2
    fc=pgf90
    fflags="-O1 -fpic"

    ;;

#################### solaris #######
solaris)

    cc="cc -fast"
    cflags="-DSYSV"
    ocflags="-DSUN -DSYSV"
    fc="f90 -fast"
    libkind='--enable-sharedlibs=solaris-cc'
    ;;

#################### unknown choice #######
*)
    echo "Architecture/compiler $compiler is not supported" 1>&2
    usage
    exit 1
    ;;

esac

if [ -n "$static"  ]; then
#    static="$static $libkind"
    static="$libkind"
fi

#--------------------------------------------------------------------------
#  Configure MPICH2
#--------------------------------------------------------------------------

# Get the list of mpich2 directories
mpidirs=`/bin/ls -d mpich2*/ 2>/dev/null | tail -n 1`

if [ -z $mpidirs ]; then
    echo "You must download MPICH2 and extract it here"
    echo "   (for example, type 'tar xvfz mpich2-1.4.1p1.tar.gz')"
    echo "See http://www.mcs.anl.gov/research/projects/mpich2/downloads/index.php?s=downloads for more info;"
    echo "Then, re-run this script."
    exit 1
fi

# Take the first directory
MPIDIR=`echo $mpidirs | tail -n 1`

if [ ! -d $MPIDIR ]; then
   echo "Strange, $MPIDIR does not appear to be a directory... Try extracting"
   echo "MPICH2 again and re-running this script"
   exit 1
fi

cd $MPIDIR && \
./configure --prefix=$AMBERHOME --enable-f77 --enable-fc -enable-cxx \
    $static \
    F77="$fc" FFLAGS="$fflags" \
    FC="$fc" FCFLAGS="$fflags" \
    CC="$cc" CFLAGS="$cflags" CXX="$cplusplus"
cerror=$?
if [ "$cerror" -gt 0 ]; then
    echo "    MPICH2 configure failed, returning $cerror"
    exit 1
else
    echo "    MPICH2 configure succeeded."
    echo "    (You may need to add $AMBERHOME/lib to your LD_LIBRARY_PATH)"
fi
make clean
make
make install
cd ..

exit


