#! /usr/bin/env bash
#
# default configuration options and compiler flags for different hosts
#
if test -z "$1" ; then
  HOSTNAME=`hostname`
else
  HOSTNAME=$1
fi
set -x
cwd=`pwd -P`
srcd="$(readlink -f ${0})"
srcd="${srcd%/config/default-mpi}"
if [[ "$cwd" =~ "^${srcd}/.\+" ]]; then
  echo "A build directory that is a sub-directory of the source directory is unsupported!" >&2
  exit 1
elif [[ "$srcd" = "$cwd" ]]; then
  confpath=.
else
  confpath="${srcd}"
fi
#
case "${HOSTNAME}" in
# powerpc-ibm-aix6.1.0.0
  blizzard*|passat*)
    export MP_PROCS=1
    export MP_HOSTFILE="${cwd}/hostfile"
    hostname >"$MP_HOSTFILE"
    export PKG_CONFIG_PATH="/sw/aix61/yaxt-0.1.5-64bit/lib/pkgconfig:/sw/aix61/ppm-1.0.2-64bit-xl-pe-noomp/lib/pkgconfig:${PKG_CONFIG_PATH+:$PKG_CONFIG_PATH}"
    OPTFLAGS='-O3 -qarch=pwr6 -qtune=pwr6'
    export CONFIG_SHELL=/bin/bash
    # add shared library wrappers to PATH
    case ":$PATH:" in
      *:/sw/aix61/smartmpxlf/bin:*)
        ;;
      *)
        PATH="/sw/aix61/smartmpxlf/bin:$PATH"
        ;;
    esac
    $CONFIG_SHELL "$confpath/configure" \
      --prefix="$HOME/opt/cdi-powerpc-ibm-aix6-netcdf4-pe" \
      --with-netcdf=/sw/aix61/netcdf-4.2.1.1-64bit-parallel \
      --with-hdf5=/sw/aix61/hdf5-1.8.10-64bit-parallel \
      --with-zlib=/sw/aix61/zlib-1.2.6 \
      --with-szlib=/sw/aix61/szip-2.1-64bit \
      AR='ar -X 64' \
      LDFLAGS='-g -q64 -Wl,-brtl' \
      CC='smartmpcc_r -qlanglvl=extc99' \
      CFLAGS="$OPTFLAGS -q64" \
      CPPFLAGS='-Dextname' \
      F77=smartmpxlf \
      F77FLAGS="$OPTFLAGS -q64 -qsuffix=cpp=f90 -qextname" \
      FC=smartmpxlf \
      FCFLAGS="$OPTFLAGS -q64 -qsuffix=cpp=f90 -qextname" \
      CXX=smartmpCC_r \
      LIBS='-L/sw/aix61/parallel-netcdf-1.3.0-64bit/lib -lpnetcdf -L/sw/aix61/curl-7.21.3/lib -lcurl -L/sw/aix61/openssl-1.0.0d/lib -lcrypto -lssl -L/usr/lpp/ppe.poe/lib -lgpfs' \
      ac_cv_func_mmap_fixed_mapped=yes \
      --host=powerpc-ibm-aix6.1.0.0 \
      host_alias=powerpc-ibm-aix6.1.0.0 \
      --enable-mpi=yes \
      MPI_LAUNCH="${srcd}/util/mpi_launch_poe"
    make -j8
    make -j8 check
    make install
    ;;
esac

