#!/bin/bash

# configure balks if F90 is defined
# with a fatal deprecation message pointing to FC
unset F90 F77

#export FFLAGS="${FFLAGS} -fallow-argument-mismatch"

export FCFLAGS="$FFLAGS"

# avoid absolute-paths in compilers
export CC=$(basename "$CC")
export CXX=$(basename "$CXX")
export FC=$(basename "$FC")

echo "---------------------- Conditional Section ----------------------------"
echo "targeet_platform: "${target_platform}
echo "CC: "${CC}
echo "CXX: "${CXX}
echo "FC: "${FC}
echo "CPPFLAGS: "${CPPFLAGS}
echo "CXXFLAGS: "${CXXFLAGS}
echo "LDFLAGS: "${LDFLAGS}
echo "FFLAGS: "${FFLAGS}
echo "FCFLAGS: "${FCFLAGS}
echo "HOST: "${HOST}
echo "BUILD: "${BUILD}
#echo "GSSAPI: "${GSSAPI}
echo "PREFIX: "${PREFIX}
#echo "DISABLE_MACROS_FRAMEWORK: "${DISABLE_MACOS_FRAMEWORK}
#echo "INSTALL_NAME_TOOL: "${INSTALL_NAME_TOOL}
echo "----------------------------------------------------------------"

# avoid recording flags in compilers
# See Compiler Flags section of MPICH readme
# TODO: configure ignores MPICHLIB_LDFLAGS
export MPICHLIB_CPPFLAGS=$CPPFLAGS
unset CPPFLAGS
export MPICHLIB_CFLAGS=$CFLAGS
unset CFLAGS
export MPICHLIB_CXXFLAGS=$CXXFLAGS
unset CXXFLAGS
export MPICHLIB_LDFLAGS=$LDFLAGS
unset LDFLAGS
export MPICHLIB_FFLAGS=$FFLAGS
unset FFLAGS
export MPICHLIB_FCFLAGS=$FCFLAGS
unset FCFLAGS

# set some specific flags that we *do* want recorded in the compilers
# only the bare minimum of prefix-awareness here
export CPPFLAGS="-I$PREFIX/include"
export CFLAGS="-I$PREFIX/include"
export CXXFLAGS="-I$PREFIX/include"
# We included the FFLAGS and FCFLAGS because we need the parameters also for the compilers
export FFLAGS="-I$PREFIX/include -fallow-argument-mismatch"   
export FCFLAGS="-I$PREFIX/include -fallow-argument-mismatch"  # try and see 
export LDFLAGS="-L$PREFIX/lib -Wl,-rpath,$PREFIX/lib"

export LIBRARY_PATH="$PREFIX/lib"

./configure --prefix=$PREFIX \
            --disable-dependency-tracking \
            --disable-libxml2 \
            --enable-cxx \
            --enable-fortran \
            --disable-wrapper-rpath

make -j"${CPU_COUNT:-1}"
make install
