#!/usr/bin/env bash

# ##############################################################################
#
# Amanzi MOAB Configuration 
#
# ##############################################################################
print_usage()
{
    echo ""
    echo "Usage: $0 [-h|--help] [-p|--prefix path] -5|--hdf5-dir hdf5_path -n|--netcdf-dir netcdf_path [ -- additional configure args]"
    echo ""
    echo "FLAGS:"
    echo "-h|--help                           Print this message and exit"
    echo "-p|--prefix path                    Define the installation prefix path. Default ${HOME}/netcdf"
    echo "-5|--hdf5-dir hdf5_path             HDF5 installation directory (REQUIRED)"
    echo "-n|--netcdf-dir netcdf_path         NetCDF installation directory (REQUIRED)"
    echo ""
    echo ""
    echo "Additional flags may be passed to configure"
    echo "The '--' informs the parser that the remaining arguments are configure arguments" 
}

# Need the new version of getopt to process the long style command options
getopt_ok() {
    getopts --test
    stat=$?

    if [ stat -eq 4 ]; then
        return 1
    else
        echo "Your version of getopt failed the version test"
        echo "long option names are disabled and will be ignored"
        echo -n "getopt --test returned:"
        echo stat
        echo "Can not parse command line arguments"
        return 0
    fi    
}

# Parse command line opts
INSTALL_PATH=${HOME}/moab
HDF5_PATH=
ZLIB_PATH=
CURL_PATH=
EXTRA_ARGS=

short_opts='hp:5:n:'
if [ getopt_ok ]; then
    long_opts='help,prefix:,hdf5-dir:,netcdf-dir:'
    args=`getopt -o ${short_opts} -l ${long_opts} -u -- $*`
    stat=$?
    if [ ${stat} -ne 0 ]; then
        print_usage
        exit 1
    fi
    echo "Before set ${args}"
    set -- $args
    for arg 
    do
        case "$arg" in
            -h|--help) print_usage; exit 0;;
            -p|--prefix) shift; INSTALL_PATH="$1"; shift;; 
            -5|--hdf5-dir) shift; HDF5_PATH="$1"; shift;; 
            -n|--netcdf-dir) shift; NETCDF_PATH="$1"; shift;; 
            --) shift; EXTRA_ARGS=$@;break;;
        esac
    done
else
    echo "Need to put code here for the bash bultin getopts"
    exit 1
fi

# Check the arguments
if [ -z "${HDF5_PATH}" ]; then
    echo "Please define the HDF5 installation directory"
    echo "HDF5 is required to build MOAB"
    print_usage
    exit 1
fi

if [ -z "${NETCDF_PATH}" ]; then
    echo "Please define the NetCDF installation directory"
    echo "NetCDF is required to build MOAB"
    print_usage
    exit 1
fi

# Simple configure 

echo "-------------------------------------------------------------------"
echo ""
echo "Configuring MOAB"
echo ""
echo "PREFIX=${INSTALL_PATH}"
echo "NetCDF Directory=${NETCDF_PATH}"
echo "HDF5 Directory=${HDF5_PATH}"
echo ""

# Set CC and CXX environment variables
export CC=mpicc
export CXX=mpic++
NetCDF_config="${NETCDF_PATH}/lib/pkgconfig/netcdf.pc"
if [ -e ${NetCDF_config} ]; then
    echo ""
    echo " Using NetCDF package config file to set CFLAGS and LDFLAGS"
    echo ""
    old_LDFAGS=${LDFLAGS}
    NETCDF_LDFLAGS=`pkg-config --libs ${NetCDF_config}`
    export LDFLAGS="${old_LDFLAGS} ${NETCDF_LDFLAGS}"
    old_CFLAGS=${CFLAGS}
    NETCDF_CFLAGS=`pkg-config --cflags ${NETCDF_config}`
    export CFLAGS="${old_CFLAGS} ${NETCDF_CFLAGS}"
fi   

echo ""
echo "Environment Variables"
echo "CC=${CC}"
echo "CXX=${CC}"
echo "CFLAGS=${CFLAGS}"
echo "LDFLAGS=${LDFLAGS}"
echo ""
echo "-------------------------------------------------------------------"


# We are using the latest MOAB version and must build the
#  configure script 
#autoreconf -fi



# Configure 
#  * Enable (MPI) Parallel
#  * Enable NetCDF
#  * Enable hdf5
./configure --prefix=${INSTALL_PATH} \
            --with-mpi \
            --with-hdf5=${HDF5_PATH} \
            --with-netcdf=${NETCDF_PATH} \
            ${EXTRA_ARGS}

echo ${QTDIR}            
