#!/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-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-sharedlibs=osx-gcc'
fi

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

case "$compiler" in

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

    ;;

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

    ;;

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

    ;;

#################### 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
#--------------------------------------------------------------------------

if [ -d mpich2-1.2.1p1 ]; then
    MPIDIR=mpich2-1.2.1p1
else
    echo "You must download MPICH2 and extract it here"
    echo "   (for example, type 'tar xvfz mpich2-1.2.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

cd $MPIDIR && \
./configure --prefix=$AMBERHOME --enable-f77 --enable-f90 \
    $static \
    F77="$fc" FFLAGS="$fflags" \
    F90="$fc" F90FLAGS="$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



