#!/bin/bash
#
# This script is for configuring adios on the authors' machines
# You can study it to figure out how to configure adios on your system
#

SRCDIR=`dirname ${BASH_SOURCE[0]}`
BUILD_FULL=true
if [ x"$1" == x"lean" ]; then
    BUILD_FULL=false
    echo "Build ADIOS without extra packages"
fi

if [ `hostname | cut -c 1-4` == "rhea" ]; then

    ########
    # Rhea #
    ########
    source /etc/profile.d/modules.sh
    TARGET=`module list 2>&1 | grep "PE"- | sed "s/^.*PE-\([a-z]*\).*/\1/"`

    if [ -z "$TARGET" ]; then
        echo "Cannot determine Programming environment. Exit"
        exit 1
    fi
    echo "Configure on Rhea for $TARGET env. for user ${USER}"
    module unload hdf5
    module unload netcdf
    module unload PE-gnu
    module unload PE-pgi
    module unload PE-intel
    module unload PE-pathscale
    module unload pgi gcc intel pathscale
    module unload python
    module load PE-$TARGET
    #module load python
    # Use both seq hdf5 (for utils) and 
    #   parallel hdf5 (for PHDF5 method)

    if [ $BUILD_FULL == "true" ]; then
        echo "Load modules for full build"
        module load hdf5/1.8.11
        SEQ_HDF5_DIR=$HDF5_DIR
        SEQ_HDF5_CLIB=$HDF5_CLIB
        module unload hdf5
        module load hdf5-parallel/1.8.11
        PAR_HDF5_DIR=$HDF5_DIR
        PAR_HDF5_CLIB=$HDF5_CLIB
        module unload hdf5
        # Seq. and Parallel NetCDF 4 
        module load netcdf/4.1.3
        SEQ_NC_DIR=$NETCDF_DIR
        SEQ_NC_CLIB=$NETCDF_CLIB
        module unload netcdf
        #module load netcdf/4.1.3_par
        #PAR_NC_DIR=$NETCDF_DIR
        #PAR_NC_CLIB=$NETCDF_CLIB
        #module unload netcdf
        #module load szip
        #module load bzip2
        module load dataspaces/1.6.2
        module load flexpath/1.12
        module load alacrity/1.0.0
        module load fastbit/svn
    fi
    export MPICC=mpicc
    export MPICXX=mpiCC
    export MPIFC=mpif90
    unset EXTRA_CFLAGS
    if [ "$TARGET" == "pgi" ]; then
        export CC=pgcc
        export CXX=pgCC
        export FC=pgf90
    elif [ "$TARGET" == "gnu" ]; then
        export CC=gcc
        export CXX=g++
        export FC=gfortran
    elif [ "$TARGET" == "intel" ]; then
        export CC=icc
        export CXX=icpc
        export FC=ifort
        export EXTRA_CFLAGS="-std=c99"
        WITHFLEX=""
    else
        echo "TARGET must be pgi or gnu or intel"
        exit 1
    fi
        
    export CPPFLAGS="-DMPICH_IGNORE_CXX_SEEK" 
    export CFLAGS="-g -fPIC ${EXTRA_CFLAGS}" 
    if [ $BUILD_FULL == "true" ]; then
        echo "Configure for full build"
        $SRCDIR/configure --prefix=/ccs/proj/e2e/${USER}/ADIOS/rhea.$TARGET \
        --disable-maintainer-mode \
        --enable-dependency-tracking \
        --enable-research-transports \
        --with-lustre \
        --with-dataspaces=$DATASPACES_DIR \
        --with-flexpath=$FLEXPATH_DIR \
        --with-alacrity=$ALACRITY_DIR \
        --with-zlib \
        --with-bzip2 \
        --with-fastbit=$FASTBIT_DIR \
        --with-hdf5=${SEQ_HDF5_DIR} \
        --with-hdf5-libs="${SEQ_HDF5_CLIB}" \
        #--with-phdf5=${PAR_HDF5_DIR} \
        #--with-phdf5-libs="${PAR_HDF5_CLIB}" \
        #--with-netcdf=${SEQ_NC_DIR} \
        #--with-netcdf-libs="${SEQ_NC_CLIB}" \
        #--with-nc4par=${PAR_NC_DIR} \
        #--with-nc4par-libs="${PAR_NC_CLIB}" \
        #--with-aplod=/ccs/proj/e2e/ncsu/sith.gnu \
        #--with-isobar=/ccs/proj/e2e/ncsu/sith.gnu \
        #--with-fgr=/ccs/proj/e2e/qliu/tap \
        #--with-glib=/ccs/proj/e2e/qliu/glib
        #--with-szip=$SZIP_DIR

    else
        echo "Configure for lean build"
        $SRCDIR/configure --prefix=/ccs/proj/e2e/${USER}/ADIOS/rhea.$TARGET \
        --disable-maintainer-mode \
        --enable-dependency-tracking \
        --with-lustre \
        --disable-timers --disable-timer-events

    fi


elif [ `hostname | cut -c 1-5` == "titan" ]; then

    #source /opt/modules/default/etc/modules.sh
    TARGET=`module list 2>&1 | grep "PrgEnv"- | sed "s/^.*PrgEnv-\([a-z]*\).*/\1/"`
    
    #########
    # Titan #
    #########
    #TARGET=pgi
    echo "Configure on TITAN (XK7) including staging methods for $TARGET env."
    module unload szip
    module unload hdf5
    module unload netcdf
    module unload hdf5-parallel
    module unload netcdf-hdf5parallel
    module unload fastbit
    module unload craype-interlagos craype-interlagos-cu 
    module unload craype-barcelona craype-abudhabi craype-abudhabi-cu 
    module unload craype-ivybridge craype-sandybridge craype-haswell 
    module unload craype-mc8 craype-mc12
    module load craype-istanbul
    if [ $BUILD_FULL == "true" ]; then
        module load dataspaces # /1.6.1
        module load flexpath # /1.12
        # NOTE hdf5-parallel module does not work with C++ compiler
        #module load hdf5-parallel
        #module load netcdf-hdf5parallel
        #module load szip
        #module load papi
    fi
    unset EXTRA_CFLAGS
    unset EXTRA_LIBS
    unset LDFLAGS
    unset WITHFLEX
    unset WITHFASTBIT
    unset WITHSZ
    export CC=cc
    export FC=ftn
    export CXX=CC
    if [ "$TARGET" == "pgi" ]; then
        if [ $BUILD_FULL == "true" ]; then
            # FASTBIT needs libstdc++ and -pgcpplibs flag
            export LDFLAGS="-pgc++libs" 
            #DATASPACES_DIR="/ccs/proj/e2e/dataspaces/titan/$target-cpu"
            WITHFLEX="--with-flexpath=$FLEXPATH_DIR"
            #export EXTRA_LIBS="/opt/gcc/4.9.0/snos/lib64/libstdc++.a"
            export EXTRA_LIBS="${GCC_PATH}/snos/lib64/libstdc++.a"
            WITHSZ="--with-sz=/sw/xk6/adios/SZ/1.4.10.0/pgi16.10.0"
        fi
        export EXTRA_CFLAGS="-fast"
    elif [ "$TARGET" == "gnu" ]; then
        #export CC=gcc
        #export FC=gfortran
        #export CXX=g++
        #export MPICC=cc
        #export MPIFC=ftn
        #export MPICXX=CC
        # NSSI/FLEXPATH/FASTBIT needs libstdc++
        export LDFLAGS="" 
        export EXTRA_CFLAGS="-Ofast"
        if [ $BUILD_FULL == "true" ]; then
            module load fastbit
            WITHFLEX="--with-flexpath=$FLEXPATH_DIR"
            #WITHFLEX="--with-flexpath=/ccs/proj/e2e/chaos/titan/$TARGET"
            # FASTBIT needs libstdc++
            #WITHFASTBIT="--with-fastbit=$FASTBIT_DIR"
            #export EXTRA_LIBS="${GCC_PATH}/snos/lib64/libstdc++.a"
            #DATASPACES_DIR="/ccs/proj/e2e/dataspaces/titan/$TARGET"
            WITHSZ="--with-sz=/sw/xk6/adios/SZ/1.4.10.0/gnu4.9.3"
        fi
    elif [ "$TARGET" == "cray" ]; then
        export EXTRA_CFLAGS="-h gnu"
    fi
 
    # use the two lines below for openmpi
    #export CC=mpicc
    #export FC=mpif90
    export CPPFLAGS="-DMPICH_IGNORE_CXX_SEEK -DDART_DO_VERSIONING" 
    export CFLAGS="-fPIC -g ${EXTRA_CFLAGS}" 
    if [ $BUILD_FULL == "true" ]; then
        LIBS="$EXTRA_LIBS" ${SRCDIR}/configure --prefix=/ccs/proj/e2e/${USER}/ADIOS/xk6.$TARGET \
        --enable-dependency-tracking \
        --disable-maintainer-mode \
        --with-cray-pmi=/opt/cray/pmi/default \
        --with-cray-ugni-incdir=/opt/cray/gni-headers/default/include \
        --with-cray-ugni-libdir=/opt/cray/ugni/default/lib64 \
        --with-dataspaces=$DATASPACES_DIR \
        --with-dimes=$DATASPACES_DIR \
        --without-infiniband \
        $WITHFLEX \
        $WITHFASTBIT \
        $WITHSZ \
        --with-lustre \
        --disable-timers --disable-timer-events \
        --with-zlib --with-bzip2 

        #--enable-skel-timing
        #--with-fgr=/ccs/proj/e2e/qliu/tap \
        #--with-glib=/ccs/proj/e2e/qliu/glib \
        #--with-netcdf=/opt/cray/netcdf/3.6.2/netcdf-${TARGET} \
        #--with-hdf5=/sw/xt5/hdf5/1.8.2/cnl2.1_gnu4.2.0 \
        #--with-nc4par=/opt/cray/netcdf-hdf5parallel/4.0.1.3/netcdf-hdf5parallel-$TARGET \
        #--with-phdf5=/opt/cray/hdf5-parallel/1.8.4.1/hdf5-parallel-$TARGET \
        #--with-dimes=/ccs/proj/e2e/pnorbert/spaces/$TARGET
        #--with-nssi=/ccs/proj/e2e/pnorbert/nssi/xt5/$TARGET \
        #--with-datatap=/ccs/home/zf2/work/pe.$TARGET \
        #--with-datatap=/ccs/proj/e2e/pnorbert/datatap/xt5/$TARGET \

    else
        LIBS="$EXTRA_LIBS" ${SRCDIR}/configure --prefix=/ccs/proj/e2e/${USER}/ADIOS/xk6.$TARGET \
        --enable-dependency-tracking \
        --disable-maintainer-mode \
        --with-lustre \
        --disable-timers --disable-timer-events \
        --without-infiniband 

    fi




elif [ `hostname | cut -c 1-4` == "eos-" ]; then

      #################
      # EOS Cray XC30 #
      #################
      #source /opt/modules/default/etc/modules.sh
      TARGET=`module list 2>&1 | grep "PrgEnv"- | sed "s/^.*PrgEnv-\([a-z]*\).*/\1/"`
      echo "Configure on EOS (Cray XC30) for $TARGET env."
      export CC=cc
      #export CFLAGS="-Wall -g" 
      export FC=ftn
      export CXX=CC
      module unload szip
      module unload hdf5
      module unload netcdf
      module unload netcdf-hdf5parallel
      module unload hdf5-parallel
      module unload papi
      module unload pmi
      if [ "$TARGET" == "pgi" ]; then
          # NSSI needs -pgc++libs flag 
          export LDFLAGS="-pgc++libs" 
          unset EXTRA_LIBS 
      elif [ "$TARGET" == "gnu" ]; then
          # NSSI needs libstdc++
          unset LDFLAGS 
          export EXTRA_LIBS="/opt/gcc/4.8.1/snos/lib64/libstdc++.a"
          module swap gcc gcc/4.8.1
      elif [ "$TARGET" == "cray" ]; then
          export EXTRA_CFLAGS="-h gnu"
      else
          unset LDFLAGS 
          unset EXTRA_LIBS 
          unset EXTRA_CFLAGS 
      fi
      module load pmi
      module load craype-hugepages2M
      CFLAGS="-g -fPIC ${EXTRA_CFLAGS}" ${SRCDIR}/configure --prefix=/ccs/proj/e2e/pnorbert/ADIOS/eos.$TARGET \
          --disable-maintainer-mode \
          --enable-dependency-tracking \
          --with-cray-pmi=/opt/cray/pmi/default \
          --with-cray-ugni-incdir=/opt/cray/gni-headers/default/include \
          --with-cray-ugni-libdir=/opt/cray/ugni/default/lib64 \
          --with-dataspaces=/ccs/proj/e2e/dataspaces/eos/1.6.1/$TARGET \
          --with-dimes=/ccs/proj/e2e/dataspaces/eos/1.6.1/$TARGET \
            

elif [ `hostname | cut -c 1-7` == "chester" ]; then

      ###############
      # Chester XK6 #
      ###############
      #source /opt/modules/default/etc/modules.sh
      TARGET=`module list 2>&1 | grep "PrgEnv"- | sed "s/^.*PrgEnv-\([a-z]*\).*/\1/"`
      echo "Configure on Chester (XK6) for $TARGET env."
      export CC=cc
      export FC=ftn
      export CXX=CC
      module unload szip
      module unload hdf5
      module unload netcdf
      module unload netcdf-hdf5parallel
      module unload hdf5-parallel
      module swap craype-interlagos craype-istanbul
      module load dataspaces # /1.6.2
      module load flexpath # /1.12
      module load fastbit
      WITHFASTBIT=""
      WITHALACRITY=""
      if [ "$TARGET" == "pgi" ]; then
          export CC=pgcc
          export FC=pgf90
          export CXX=pgc++
          export MPICC=cc
          export MPIFC=ftn
          export MPICXX=CC
          # FASTBIT, ALACRITY need -pgcpplibs flag 
          WITHALACRITY="--with-alacrity=/ccs/proj/e2e/ncsu/alacrity/xk6/pgi"
          export FLEXPATH="--with-flexpath=$FLEXPATH_DIR"
          export LDFLAGS="-pgc++libs" 
          unset EXTRA_LIBS 
      elif [ "$TARGET" == "gnu" ]; then
          export CC=gcc
          export FC=gfortran
          export CXX=g++
          export MPICC=cc
          export MPIFC=ftn
          export MPICXX=CC
          # FASTBIT, ALACRITY needs libstdc++
          # PROBLEM: there is libstdc++.so and libtool uses that in any case leading to link error 
          #WITHALACRITY="--with-alacrity=/ccs/proj/e2e/ncsu/alacrity/xk6/gnu"
          #WITHFASTBIT="--with-fastbit=${FASTBIT_DIR}"
          #export LDFLAGS="-static -static-libstdc++"
          #export EXTRA_LIBS="${GCC_PATH}/snos/lib64/libstdc++.a"
          #export FLEXPATH="--with-flexpath=/ccs/proj/e2e/chaos/titan/gnu"
          export FLEXPATH="--with-flexpath=$FLEXPATH_DIR"
          #module swap gcc gcc/4.4.4
      elif [ "$TARGET" == "cray" ]; then
          export EXTRA_CFLAGS="-h gnu"
          WITHALACRITY="--with-alacrity=/ccs/proj/e2e/ncsu/alacrity/xk6/cray"
      else
          unset LDFLAGS 
          unset EXTRA_LIBS 
          unset EXTRA_CFLAGS 
      fi
      CFLAGS="-g -fPIC ${EXTRA_CFLAGS}" LDFLAGS="$LDFLAGS $CRAY_RCA_POST_LINK_OPTS" \
      ${SRCDIR}/configure --prefix=/ccs/proj/e2e/pnorbert/ADIOS/chester.$TARGET \
          --disable-maintainer-mode \
          --enable-dependency-tracking \
          --with-cray-pmi=/opt/cray/pmi/default \
          --with-cray-ugni-incdir=/opt/cray/gni-headers/default/include \
          --with-cray-ugni-libdir=/opt/cray/ugni/default/lib64 \
          --with-dataspaces=$DATASPACES_DIR \
          --with-dimes=$DATASPACES_DIR \
          $FLEXPATH \
          $WITHALACRITY \
          $WITHFASTBIT \
          --without-infiniband 
#            --with-lustre=/opt/xt-lustre-ss/2.2_1.6.5/usr \
#            --with-dmalloc=/ccs/proj/e2e/qliu/dmalloc.$TARGET \
#            --enable-research-transports \
#            --with-netcdf=/opt/cray/netcdf/3.6.2/netcdf-${TARGET} \
#            --with-nc4par=/opt/cray/netcdf-hdf5parallel/4.0.1.3/netcdf-hdf5parallel-$TARGET \
#            --with-phdf5=/opt/cray/hdf5-parallel/1.8.4.1/hdf5-parallel-$TARGET \
#            --with-hdf5=/sw/xt5/hdf5/1.8.2/cnl2.1_gnu7.2.3 \
#            --with-hdf5=/sw/xt5/hdf5/1.8.2/cnl2.1_gnu4.2.0 \
#          --with-cray-ugni-incdir=/opt/cray/gni-headers/2.1-1.0400.4351.3.1.gem/include \
            

elif [ `hostname -f | cut -c 1-9` == "summitdev" ]; then
    #####################
    #  SUMMITDEV IBM    #
    #####################
    MLIST=`module list 2>&1 ` #| grep "PrgEnv"- | sed "s/^.*PrgEnv-\([a-z]*\).*/\1/"`
    TARGET=`echo $MLIST | grep " xl\/" | sed "s/^.* xl\/.*/xl/"`
    if [ -z $TARGET ]; then
        TARGET=`echo $MLIST | grep " pgi\/" | sed "s/^.* pgi\/.*/pgi/"`
        if [ -z $TARGET ]; then
            TARGET=`echo $MLIST | grep " gcc\/" | sed "s/^.* gcc\/.*/gcc/"`
            if [ -z $TARGET ]; then
                TARGET=`echo $MLIST | grep " clang\/" | sed "s/^.* clang\/.*/clang/"`
            fi
        fi
    fi
    echo "Configure on Summitdev for $TARGET env."
    
    unset WITH_DATASPACES
    FORTRAN_BUILD="--enable-fortran"
    if [ "$TARGET" == "xl" ]; then
        export MPICC=mpicc
        export MPCXX=mpic++
        export MPIFC=mpif90
        export CFLAGS="-g -O0"
        export FC=xlf90
        export CC=xlc
        export CXX=xlC
        #export LIBS="-lrdmacm -libverbs"
        DATASPACES_DIR=/ccs/proj/e2e/dataspaces/summitdev/1.6.1/xl
        #WITH_DATASPACES="--with-dataspaces=$DATASPACES_DIR --with-dimes=$DATASPACES_DIR"
    elif [ "$TARGET" == "pgi" ]; then
        export CC=pgcc
        export FC=pgf90
        export CXX=pgc++
        export MPICC=mpicc
        export MPCXX=mpic++
        export MPIFC=mpif90
        export CFLAGS="-g -O0"
        #DATASPACES_DIR=/ccs/proj/e2e/dataspaces/summitdev/1.6.1/pgi
        #WITH_DATASPACES="--with-dataspaces=$DATASPACES_DIR --with-dimes=$DATASPACES_DIR"
    elif [ "$TARGET" == "gcc" ]; then
        export CC=gcc
        export FC=gfortran
        export CXX=g++
        export MPICC=mpicc
        export MPCXX=mpic++
        export MPIFC=mpif90
        export CFLAGS="-g -O0"
        DATASPACES_DIR=/ccs/proj/e2e/dataspaces/summitdev/1.6.1/gcc
        #WITH_DATASPACES="--with-dataspaces=$DATASPACES_DIR --with-dimes=$DATASPACES_DIR"
    elif [ "$TARGET" == "clang" ]; then
        export CC=clang
        export CXX=clang++
        export MPICC=mpicc
        export MPCXX=mpic++
        export CFLAGS="-g -O0"
        DATASPACES_DIR=/ccs/proj/e2e/dataspaces/summitdev/1.6.1/clang
        FORTRAN_BUILD="--disable-fortran"
        #WITH_DATASPACES="--with-dataspaces=$DATASPACES_DIR --with-dimes=$DATASPACES_DIR"
    fi

    ${SRCDIR}/configure --prefix=/ccs/proj/e2e/pnorbert/ADIOS/summitdev.$TARGET  \
            --disable-maintainer-mode \
            --enable-dependency-tracking \
            $FORTRAN_BUILD \
            --without-datatap  \
            $WITH_DATASPACES

elif [ `hostname | cut -c 1-4` == "sith" ]; then

    ########
    # Sith #
    ########
    #source /etc/profile.d/modules.sh
    TARGET=`module list 2>&1 | grep "PE"- | sed "s/^.*PE-\([a-z]*\).*/\1/"`

    if [ -z "$TARGET" ]; then
        echo "Cannot determine Programming environment. Exit"
        exit 1
    fi
    echo "Configure on SITH for $TARGET env. for user ${USER}"
    module unload hdf5
    module unload netcdf
    module unload python
    #module unload PE-gnu
    #module unload PE-pgi
    #module unload PE-intel
    #module unload PE-pathscale
    #module unload pgi gcc intel pathscale
    #module load PE-$TARGET
    module load python
    # Use both seq hdf5 (for utils) and 
    #   parallel hdf5 (for PHDF5 method)
    module load hdf5/1.8.10
    SEQ_HDF5_DIR=$HDF5_DIR
    SEQ_HDF5_CLIB=$HDF5_CLIB
    module unload hdf5
    module load hdf5/1.8.10_par
    PAR_HDF5_DIR=$HDF5_DIR
    PAR_HDF5_CLIB=$HDF5_CLIB
    module unload hdf5
    # Seq. and Parallel NetCDF 4 
    module load netcdf/4.1.3
    SEQ_NC_DIR=$NETCDF_DIR
    SEQ_NC_CLIB=$NETCDF_CLIB
    module unload netcdf
    module load netcdf/4.1.3_par
    PAR_NC_DIR=$NETCDF_DIR
    PAR_NC_CLIB=$NETCDF_CLIB
    module unload netcdf
    #module load szip
    module load bzip2
    module unload dataspaces
    unset DATASPACES_DIR
    module load dataspaces/1.6.0
    export MPICC=mpicc
    export MPICXX=mpiCC
    export MPIFC=mpif90
    export CPPFLAGS="-DMPICH_IGNORE_CXX_SEEK" 
    export CFLAGS="-g -O0 -fPIC" 
    export CXXFLAGS="-g -O0 -fPIC" 
    export FCFLAGS="-g -O0 -fPIC" 
    WITHFLEX=""
    WITHFGR=""
    WITHFASTBIT=""
    WITHALACRITY=""
    WITHSZ=""
    if [ "$TARGET" == "pgi" ]; then
        export CC=pgcc
        export CXX=pgc++
        export FC=pgf90
        WITHFLEX="--with-flexpath=/ccs/proj/e2e/chaos/sith/$TARGET"
        WITHFGR="--with-fgr=/ccs/proj/e2e/qliu/tap" 
        WITHALACRITY="--with-alacrity=/ccs/proj/e2e/ncsu/alacrity/sith/pgi"
        WITHFASTBIT="--with-fastbit=/sw/redhat6/fastbit/svn/rhel6_gnu4.4.7"
        WITHSZ="--with-sz=/ccs/proj/e2e/SZ/1.4.10.0/sith/pgi16.9"
        #DATASPACES_DIR="/ccs/proj/e2e/dataspaces/sith-tcp/1.6.pgi"
        #DATASPACES_DIR="/ccs/home/hbui/usr/software/dataspaces-dev"
    elif [ "$TARGET" == "gnu" ]; then
        export CC=gcc
        export CXX=g++
        export FC=gfortran
        #export CFLAGS="$CFLAGS -gdwarf-3" 
        #export FCFLAGS="$FCFLAGS -gdwarf-3" 
        WITHFLEX="--with-flexpath=/ccs/proj/e2e/chaos/sith/$TARGET"
        WITHALACRITY="--with-alacrity=/ccs/proj/e2e/ncsu/alacrity/sith/gnu"
        WITHFASTBIT="--with-fastbit=/sw/redhat6/fastbit/svn/rhel6_gnu4.8.2"
        #DATASPACES_DIR="/ccs/proj/e2e/dataspaces/sith-tcp/1.6.gnu"
    elif [ "$TARGET" == "intel" ]; then
        export CC=icc
        export CXX=icpc
        export FC=ifort
        WITHALACRITY="--with-alacrity=/ccs/proj/e2e/ncsu/alacrity/sith/intel"
        WITHFASTBIT="--with-fastbit=/sw/redhat6/fastbit/svn/rhel6_gnu4.8.2"
    else
        echo "TARGET must be pgi or gnu or intel"
        exit 1
    fi
    
    $SRCDIR/configure --prefix=/ccs/proj/e2e/${USER}/ADIOS/sith.$TARGET \
        --config-cache \
        --disable-maintainer-mode \
        --enable-dependency-tracking \
        --enable-research-transports \
        --with-lustre \
        $WITHSZ  
        #--with-zlib \
        #--with-bzip2=$BZIP2_DIR \
        #--with-dataspaces=$DATASPACES_DIR \
        #$WITHFLEX \
        #$WITHFASTBIT \
        #$WITHALACRITY \
        #--with-aplod=/ccs/proj/e2e/ncsu/sith.gnu \
        #--with-isobar=/ccs/proj/e2e/ncsu/sith.gnu \
        #--with-hdf5=${SEQ_HDF5_DIR} \
        #--with-hdf5-libs="${SEQ_HDF5_CLIB}" \
        #--with-netcdf=${SEQ_NC_DIR} \
        #--with-netcdf-libs="${SEQ_NC_CLIB}" \
        #--with-dimes=$DATASPACES_DIR \
        #--with-glib=/ccs/proj/e2e/qliu/glib
        #$WITHFGR \
        #--with-szip=$SZIP_DIR \
        #--with-phdf5=${PAR_HDF5_DIR} \
        #--with-phdf5-libs="${PAR_HDF5_CLIB}" \
        #--with-nc4par=${PAR_NC_DIR} \
        #--with-nc4par-libs="${PAR_NC_CLIB}" \
        #--with-dmalloc=/ccs/proj/e2e/qliu/dmalloc.sith.$TARGET 
        #--with-dmalloc=/ccs/proj/e2e/pnorbert/dmalloc.$TARGET 
        #--enable-shared --disable-static
        #--without-datatap #--without-infiniband
        #--with-dmalloc=/ccs/proj/e2e/pnorbert/dmalloc.$TARGET 
        #--with-datatap=/ccs/home/habbasi/work/ewok/
        #--enable-datatap=ib

elif [ `hostname | cut -c 1-4` == "yona" ]; then

    ########
    # Yona #
    ########
    source /etc/profile.d/modules.sh
    TARGET=`module list 2>&1 | grep "PE"- | sed "s/^.*PE-\([a-z]*\).*/\1/"`
    echo "Configure on Yona for $TARGET env."
    module unload hdf5
    module unload netcdf
    module unload PE-gnu
    module unload PE-pgi
    module unload PE-intel
    module unload PE-pathscale
    module unload pgi gcc intel pathscale
    #module unload python
    module load PE-$TARGET
    #module load python
    # Use both seq hdf5 (for utils) and 
    #   parallel hdf5 (for PHDF5 method)
    module load hdf5/1.8.5
    SEQ_HDF5_DIR=$HDF5_DIR
    SEQ_HDF5_CLIB=$HDF5_CLIB
    module unload hdf5
    #module load hdf5/1.8.5_par
    #PAR_HDF5_DIR=$HDF5_DIR
    #PAR_HDF5_CLIB=$HDF5_CLIB
    #module unload hdf5
    # Seq. and Parallel NetCDF 4 
    module load netcdf/3.6.2
    SEQ_NC_DIR=$NETCDF_DIR
    SEQ_NC_CLIB=$NETCDF_CLIB
    module unload netcdf
    #module load netcdf/4.1.1_par
    #PAR_NC_DIR=$NETCDF_DIR
    #PAR_NC_CLIB=$NETCDF_CLIB
    #module unload netcdf
    export MPICC=mpicc
    export MPICXX=mpiCC
    export MPIFC=mpif90
    if [ "$TARGET" == "pgi" ]; then
        export CC=pgcc
        export CXX=pgCC
        export FC=pgf90
    elif [ "$TARGET" == "gnu" ]; then
        export CC=gcc
        export CXX=g++
        export FC=gfortran
    elif [ "$TARGET" == "intel" ]; then
        export CC=pgcc
        export CXX=pgCC
        export FC=pgf90
    elif [ "$TARGET" == "gnu" ]; then
        export CC=gcc
        export CXX=g++
        export FC=gfortran
    elif [ "$TARGET" == "intel" ]; then
        export CC=icc
        export CXX=icpc
        export FC=ifort
    else
        echo "TARGET must be pgi or gnu or intel"
        exit 1
    fi
        
    export CFLAGS="-g -fPIC" 
    ${SRCDIR}/configure --prefix=/ccs/proj/e2e/pnorbert/ADIOS/yona.$TARGET \
        --config-cache \
        --disable-maintainer-mode \
        --enable-dependency-tracking \
        --enable-research-transports  \
        --with-hdf5=${SEQ_HDF5_DIR} \
        --with-hdf5-libs="${SEQ_HDF5_CLIB}" \
        --with-netcdf=${SEQ_NC_DIR} \
        --with-netcdf-libs="${SEQ_NC_CLIB}" \
        --with-lustre=/usr \
        --without-datatap --without-infiniband
        #--with-phdf5=${PAR_HDF5_DIR} \
        #--with-phdf5-libs="${PAR_HDF5_CLIB}" \
        #--with-nc4par=${PAR_NC_DIR} \
        #--with-nc4par-libs="${PAR_NC_CLIB}" \
        #--with-dmalloc=/ccs/proj/e2e/pnorbert/dmalloc.$TARGET 
        #--with-datatap=/ccs/home/habbasi/work/ewok/
        #--enable-datatap=ib



elif [ `hostname | cut -c 1-5` == "theta" ]; then

    ##########
    # THETA  #
    ##########
    TARGET=`module list 2>&1 | grep "PrgEnv"- | sed "s/^.*PrgEnv-\([a-z]*\).*/\1/"`
    CPUARCH=`module list 2>&1 | grep -e craype-broadwell -e craype-haswell -e craype-mic-knl -e craype-ivybridge -e craype-sandybridge -e craype-intel-knc | sed -e "s/.*craype-//"`
    echo "Configure on THETA for $TARGET env. for $CPUARCH CPU"
    export MPICC=cc
    export MPICXX=CC
    export MPIFC=ftn
    module unload darshan
    module unload hdf5
    module unload netcdf
    module unload hdf5-parallel
    module unload netcdf-hdf5parallel
    WITHDART=""
    if [ "$TARGET" == "pgi" ]; then
        export CC=pgcc
        export CXX=pgCC
        export FC=pgf90
        export LDFLAGS="-pgc++libs" 
    elif [ "$TARGET" == "gnu" ]; then
        export CC=gcc
        export CXX=g++
        export FC=gfortran
        export LDFLAGS=""
    elif [ "$TARGET" == "intel" ]; then
        export CC=icc
        export CXX=icpc
        export FC=ifort
        export LDFLAGS=""
    elif [ "$TARGET" == "cray" ]; then
        export EXTRA_CFLAGS="-h gnu"
        export CC=cc
        export CXX=CC
        export FC=ftn
        export LDFLAGS=""
        export EXTRA_CFLAGS="-h gnu"
    else
        echo "TARGET must be one of [ pgi | gnu | intel | cray ]"
        exit 1
    fi

    export CPPFLAGS="-DMPICH_IGNORE_CXX_SEEK"
    export CFLAGS="-fPIC ${EXTRA_CFLAGS}"
    ${SRCDIR}/configure --prefix=/home/pnorbert/sw/adios/1.12/theta/$TARGET.$CPUARCH \
        --disable-maintainer-mode \
        --enable-dependency-tracking \
        --with-cray-pmi-incdir=/opt/cray/pmi/default/include \
        --with-cray-pmi-libdir=/opt/cray/pmi/default/lib64 \
        --with-cray-ugni-incdir=/opt/cray/gni-headers/default/include \
        --with-cray-ugni-libdir=/opt/cray/ugni/default/lib64 \
        --without-infiniband \
        --without-hdf5 --without-netcdf


elif [ `hostname -f | cut -c 1-4` == "mira" -o `hostname -f | cut -c 1-5` == "cetus" ]; then
    #####################
    #  MIRA   BlueGene  #
    #####################
    echo "Configure on Mira (BlueGene/Q)"
    #export MPICC=mpicc
    #export MPIFC=mpif90
    #export CFLAGS=""
    #export FC=gfortran
    #export CC=gcc
    #export FCFLAGS="-funderscoring"
    export MPICC=mpixlc_r
    export MPIFC=mpixlf90_r
    export CFLAGS=""
    export FC=xlf90_r
    export CC=xlc_r

    ${SRCDIR}/configure --prefix=/home/pnorbert/sw/adios/xl \
            --disable-maintainer-mode \
            --disable-timers \
            --enable-dependency-tracking \
            --without-datatap --without-infiniband --with-bgq

elif [ `hostname -f | cut -c 1-5` == "vesta" ]; then
    ######################
    #  VESTA   BlueGene  #
    ######################
    echo "Configure on Vesta (BlueGene/Q)"
    export MPICC=mpixlc_r
    export MPIFC=mpixlf90
    export CFLAGS=""
    export FC=bgxlf90
    export CC=bgxlc

    ${SRCDIR}/configure --prefix=/home/qliu/ADIOS \
            --disable-maintainer-mode \
            --enable-dependency-tracking \
            --without-datatap --without-infiniband --with-bgq


elif [ `hostname | cut -c 1-4` == "cori" ]; then

    ##########
    # CORI #
    ##########
    TARGET=`module list 2>&1 | grep "PrgEnv"- | sed "s/^.*PrgEnv-\([a-z]*\).*/\1/"`
    CPUARCH=`module list 2>&1 | grep -e craype-broadwell -e craype-haswell -e craype-mic-knl -e craype-ivybridge -e craype-intel-knc | sed -e "s/.*craype-//"`
    echo "Configure on CORI for $TARGET env. for $CPUARCH CPU"
    export MPICC=cc
    export MPICXX=CC
    export MPIFC=ftn
    module unload darshan
    module unload hdf5
    module unload netcdf
    module unload hdf5-parallel
    module unload netcdf-hdf5parallel
    module load python
    WITHDART=""
    if [ "$TARGET" == "pgi" ]; then
        export CC=pgcc
        export CXX=pgCC
        export FC=pgf90
        export LDFLAGS="-pgcpplibs"
    elif [ "$TARGET" == "gnu" ]; then
        export CC=gcc
        export CXX=g++
        export FC=gfortran
        export LDFLAGS=""
    elif [ "$TARGET" == "intel" ]; then
        export CC=icc
        export CXX=icpc
        export FC=ifort
        export LDFLAGS=""
    elif [ "$TARGET" == "cray" ]; then
        export EXTRA_CFLAGS="-h gnu"
        export CC=cc
        export CXX=CC
        export FC=ftn
        export LDFLAGS=""
        export EXTRA_CFLAGS="-h gnu"
    else
        echo "TARGET must be one of [ pgi | gnu | intel | cray ]"
        exit 1
    fi

    export CPPFLAGS="-DMPICH_IGNORE_CXX_SEEK"
    export CFLAGS="-fPIC ${EXTRA_CFLAGS}"
    ${SRCDIR}/configure --prefix=/global/homes/p/pnorbert/adios/1.12/cori/$TARGET.$CPUARCH \
        --disable-maintainer-mode \
        --enable-dependency-tracking \
        --with-lustre=/usr \
        --with-cray-pmi-incdir=/opt/cray/pmi/default/include \
        --with-cray-pmi-libdir=/opt/cray/pmi/default/lib64 \
        --with-cray-ugni-incdir=/opt/cray/gni-headers/default/include \
        --with-cray-ugni-libdir=/opt/cray/ugni/default/lib64 \
        --without-infiniband \
        --without-hdf5 --without-netcdf

elif [ `hostname | cut -c 1-6` == "edison" ]; then
    ##########
    # EDISON #
    ##########
    TARGET=`module list 2>&1 | grep "PrgEnv"- | sed "s/^.*PrgEnv-\([a-z]*\).*/\1/"`
    echo "Configure on EDISON for $TARGET env."
    export MPICC=cc
    export MPICXX=CC
    export MPIFC=ftn 
    module unload darshan
    module unload automake
    module unload autoconf
    module unload hdf5
    module unload netcdf
    module unload hdf5-parallel
    module unload netcdf-hdf5parallel
    module unload PrgEnv-gnu
    module unload PrgEnv-pgi
    module unload PrgEnv-pathscale
    module unload PrgEnv-cray
    module load PrgEnv-$TARGET
    module swap cray-libsci
    module unload darshan
    module load automake/1.15
    module load autoconf
    module load python
    WITHDART=""
    if [ "$TARGET" == "pgi" ]; then
        export CC=pgcc
        export CXX=pgCC
        export FC=pgf90
        export LDFLAGS="-pgcpplibs"
    elif [ "$TARGET" == "gnu" ]; then
        export CC=gcc
        export CXX=g++
        export FC=gfortran
        export LDFLAGS=""
    elif [ "$TARGET" == "intel" ]; then
        export CC=icc
        export CXX=icpc
        export FC=ifort
        export LDFLAGS=""
    elif [ "$TARGET" == "cray" ]; then
        export EXTRA_CFLAGS="-h gnu"
        export CC=cc
        export CXX=CC
        export FC=ftn
        export LDFLAGS=""
        export EXTRA_CFLAGS="-h gnu"
    else
        echo "TARGET must be pgi or gnu or intel"
        exit 1
    fi
    export CPPFLAGS="-DMPICH_IGNORE_CXX_SEEK"
    export CFLAGS="-fPIC ${EXTRA_CFLAGS}"
    ${SRCDIR}/configure --prefix=/global/homes/p/pnorbert/adios/master/edison/$TARGET \
        --disable-maintainer-mode \
        --enable-dependency-tracking \
        --with-lustre=/usr \
        --with-cray-pmi-incdir=/opt/cray/pmi/default/include \
        --with-cray-pmi-libdir=/opt/cray/pmi/default/lib64 \
        --with-cray-ugni-incdir=/opt/cray/gni-headers/default/include \
        --with-cray-ugni-libdir=/opt/cray/ugni/default/lib64 \
    	--without-infiniband \
        --without-hdf5 --without-netcdf

elif [ `hostname | cut -c 1-4` == "dyn9" -o `hostname | cut -c 1-3` == "pnb" ]; then

    #######
    # Mac #
    #######
    echo "Configure on Mac"
    #   but using Apple's default gcc compiler
#    export OMPI_CC=$CC
#    export OMPI_FC=$FC
#    export OMPI_CXX=$CXX
    # Installed Homebrew, gcc/gfortran 5.3.0 and OpenMPI 1.10.2 in /usr/local
    # But should work with default mpicc and gcc, using --disable-fortran or with Homebrew gfortran
    #
    unset WITHZFP # build from the included zfp source
    USE_GCC5=false
    if [ $USE_GCC5 == "true" ]; then
        # If want to use gcc/gfortran 5.3.0
        export CC=/usr/local/bin/gcc-5
        export CXX=/usr/local/bin/g++-5
        export FC=/usr/local/bin/gfortran
        export OMPI_CC=/usr/local/bin/gcc-5
        export OMPI_CXX=/usr/local/bin/g++-5
        export OMPI_FC=/usr/local/bin/gfortran
        export WITHALACRITY="--with-alacrity=/opt/alacrity/gcc5"
        export WITHFASTBIT="--without-fastbit"
        export WITHFLEXPATH="--without-flexpath"
        export WITHDATASPACES="--without-dataspaces --without-dimes"
        #export WITHZFP="--without-zfp"
        # ALACRITY, FASTBIT are C++ lib so we need to link with the actual libstdc++
        # if the automatically added -lstdc++ does not work
        #export LIBS="/usr/local/Cellar/gcc/5.3.0/lib/gcc/5/libstdc++.dylib" 
        echo "Configure using gcc-5"
    else
        # If want to use clang C/C++ and gfortran 5.3.0
        export CC=/usr/bin/gcc
        export CXX=/usr/bin/g++
        export FC=/usr/local/bin/gfortran
        unset OMPI_CC
        unset OMPI_CXX
        unset OMPI_FC
#        export WITHALACRITY="--with-alacrity=/opt/alacrity/clang"
#        export WITHFASTBIT="--with-fastbit=/opt/fastbit"
        export WITHDATASPACES="--with-dataspaces=/opt/dataspaces --without-dimes"
        export WITHFLEXPATH="--with-flexpath=/opt/chaos"
        #export WITHZFP="--with-zfp=/Users/pnb/scratch/zfp-0.5.0"
        echo "Configure using clang"
        # Problem: test/test_src/selection_api.F90 link will fail because
        #   gfortran is used which finds the gcc-5 libstdc++ library instead of the system libstdc++
        # Manual fix: use mpif90 for that build command, or use "gfortran ... -Wl,-flat_namespace" command
    fi
    # The rest is common for all
    export MPICC=/usr/local/bin/mpicc
    export MPIFC=/usr/local/bin/mpif90
    export MPICXX=/usr/local/bin/mpicxx

    export CFLAGS="-g -O0 -DO_LARGEFILE=0 -fno-common -Wall" 
    export CXXFLAGS="-g -O0"
    export FCFLAGS="-g -O0"
    ${SRCDIR}/configure --prefix=/opt/adios --disable-shared \
        --with-zlib \
        ${WITHALACRITY} \
        ${WITHFASTBIT} \
        ${WITHZFP} \
        ${WITHDATASPACES} \
        ${WITHFLEXPATH} \
        --with-phdf5=/usr/local 
    #    --disable-mpi --disable-write --disable-fortran \
    #        --with-netcdf=/opt/netcdf \
    #        --with-hdf5=/opt/hdf5.seq


elif [ `hostname | cut -c 1-7` == "ubuntu" ]; then

    #########################
    # Scott's ubuntu laptop #
    #########################
    echo "Configure on UBUNTU"
    export CC=mpicc
    ${SRCDIR}/configure --prefix=/usr/local/adios \
        --enable-dependency-tracking \
        #--with-hdf5=/usr/local/hdf5-serial\
        #--with-phdf5=/usr/local \
        #--with-netcdf=/usr 

elif [ `hostname | cut -c 1-13` == "whoopingcough" ]; then

    #########################
    # Dave's workstation    #
    #########################
    echo "Configure on Whoopingcough"
    export CC=mpicc
    export CFLAGS="-fPIC -g"
    ${SRCDIR}/configure --prefix=/apps/adios/install \
        --enable-dependency-tracking \
        --without-netcdf --without-nc4par --without-hdf5 --without-phdf5 \
        --with-dataspaces=/apps/dataspaces
        #--with-hdf5=/usr/local/hdf5-serial\
        #--with-phdf5=/usr/local \

elif [ `hostname | cut -c 1-6` == "tomato" ]; then

    #########################
    # Todd's ubuntu laptop #
    #########################
    echo "Configure on UBUNTU"
    export CC=mpicc
    ${SRCDIR}/configure --prefix=/home/thkorde/local \
        --enable-dependency-tracking \
        --with-phdf5=/home/thkorde/local \
        --with-hdf5=/home/thkorde/local \
        --with-netcdf=/home/thkorde/local


elif [ `hostname | cut -c 1-4` == "qliu" ]; then

    #########################
    # Gary's ubuntu laptop #
    #########################
    echo "Configure on UBUNTU"
    export CC=mpicc
    export CFLAGS="-g -O0 -fPIC"
    ${SRCDIR}/configure --prefix=/home/qliu/ADIOS \
        --enable-dependency-tracking \
        --with-phdf5=/home/thkorde/local \
        #--with-hdf5=/home/thkorde/local \
        #--with-netcdf=/home/thkorde/local


elif [ `hostname | cut -c 1-2` == "ln" ]; then
    #######################
    # Tianhe-1A           #
    #######################
    echo "Configure on Tianhe-1A."
    #export MPICC=
    #export MPICXX=
    #export MPIFC=
    export CC=icc
    export CXX=icpc
    export FC=ifort

    ${SRCDIR}/configure --prefix=/vol6/home/Jeremy/adios-install \
        --enable-dependency-tracking \
        --enable-skel-timing \
        --disable-maintainer-mode 


        #--with-phdf5=/opt/hdf5-1.8.12-parallel \
        #--with-hdf5=/opt/hdf5-1.8.12 \
        #--with-netcdf=/opt/netcdf-3.6.3 \
        #--with-zlib=/opt/zlib  \
        #--with-szip=/opt/szip-2.1 \
        #--with-bzip2=/usr/lib/i386-linux-gnu \
        #--with-isobar=/opt/isobar \
        #--with-flexpath=/opt/chaos

elif [ `hostname | cut -c 1-9` == "PC0098504" ]; then

    #######################
    #      Linux PC       # 
    #######################
    echo "Configure on Linux PC."
    export MPICC=mpicc
    export MPICXX=mpicxx
    export MPIFC=mpif90
    export CC=gcc
    export CXX=g++
    export FC=gfortran
    export CPPFLAGS="-DMPICH_IGNORE_CXX_SEEK"
    export LDFLAGS="-lpthread"
    export CFLAGS="-g -O0 -fPIC -Wno-int-to-pointer-cast -Wno-pointer-to-int-cast"
    ${SRCDIR}/configure --prefix=/opt/adios \
        --enable-dependency-tracking \
        --enable-timers \
        --disable-timer-events 

elif [ `hostname | cut -c 1-7` == "adiosVM" ]; then

    #######################
    # ADIOS Virtual Box #
    #######################
    echo "Configure on ADIOS VirtualBox."
    export MPICC=mpicc
    export MPICXX=mpicxx
    export MPIFC=mpif90
    export CC=gcc
    export CXX=g++
    export FC=gfortran

    export CFLAGS="-g -O0 -fPIC -Wno-int-to-pointer-cast -Wno-pointer-to-int-cast"
    if [ $BUILD_FULL == "true" ]; then
        ${SRCDIR}/configure --prefix=/opt/adios \
        --disable-maintainer-mode \
        --enable-dependency-tracking \
        --enable-timers \
        --disable-timer-events \
        --with-zlib \
        --with-bzip2 \
        --with-lz4 \
        --with-sz=/opt/SZ \
        --with-blosc=/opt/blosc \
        --without-szip \
        --with-isobar=/opt/isobar \
        --with-flexpath=/opt/chaos \
        --with-dataspaces=/opt/dataspaces --without-infiniband \
        --with-fastbit=/opt/fastbit \
        --with-alacrity=/opt/alacrity \
        --with-hdf5=/opt/hdf5-1.8.17 \
        --with-phdf5=/opt/hdf5-1.8.17-parallel \
        --with-netcdf=/opt/netcdf-4.4.0 

    else
        ${SRCDIR}/configure --prefix=/opt/adios/lean \
        --disable-maintainer-mode \
        --enable-dependency-tracking \
        --with-lustre \
        --disable-timers --disable-timer-events

    fi


else
    echo "Could not determine what machine is this."
    echo "This script is for configuring adios on the authors' machines."
    echo "You can study it to figure out how to configure adios on your system."
fi
    
    
    
