#!/bin/bash
# $Id: sofa_configure_ftn_ivybridge_linux 5714 2019-02-13 15:51:08Z idculv $
#
# SOFA shell script for a local 'configure' for ROPP
# USERS SHOULD MODIFY THIS SCRIPT AS REQUIRED FOR LOCAL INSTALL
#
# Compiler: ftn / Linux
#
PREFIX=${1-$ROPP_ROOT/ftn}
echo
echo "Platform:"
uname -a
echo
echo "Fortran compiler:"
ftn -V
echo
echo "Installation target:"
echo "$PREFIX"
echo
#
FC=ftn
FCFLAGS="-O0 -ef -f fixed -target-cpu=ivybridge"

# If ROPP_BUILD_DEPS_PATH set, we want to build outside ROPP_SRC
# (nfs versus local mount).
if test -n "$ROPP_BUILD_DEPS_PATH"; then
    deps_src=$ROPP_BUILD_DEPS_PATH
    test -d $deps_src || { echo "$ROPP_BUILD_DEPS_PATH no such directory"; exit 1; }
else
    deps_src=$ROPP_SRC
fi

mfile=$(find ${deps_src}/sofa -name makefile)
test -f $mfile || { echo "No makefile found in $dep_src"; exit 1; }

if [[ ! -f ${mfile}.original ]] ; then # make one
  cp $mfile ${mfile}.original
else # work on a copy of the original
  cp ${mfile}.original $mfile
fi

for file in $(find ${deps_src}/sofa -name "*.for") ; do
  echo cp $file $(echo $file |sed -es/'\.for'/'\.f'/)
       cp $file $(echo $file |sed -es/'\.for'/'\.f'/)
done
cat $mfile |sed -es/'\.for'/'\.f'/ > $HOME/makefile.temp
mv $HOME/makefile.temp $mfile

# Tailor the makefile to this compiler/platform.  Must compile without optimisation,
# otherwise the test will detect differences and fail.
sed -i "s:INSTALL_DIR = \$(HOME):INSTALL_DIR = ${PREFIX}:g"  $mfile
sed -i "s:FC = gfortran:FC = ${FC}:g"                        $mfile
sed -i "s:FX = -O -Wall:FX = ${FCFLAGS}:g"                   $mfile

