#!/bin/bash
export SCRATCHDIR=Compile
export SRCDIR=../../src
MPI=1
Test=0 
if [[ $MPI == 1 ]]; then
  CFT=mpifort
  MPIINC="-I/usr/local/include"
  LFLAGS="-L/usr/local/lib -lmpi"
else
  CFT=ifort
  MPIINC=""
  LFLAGS=""
fi

rm -rf ${SCRATCHDIR}
mkdir ${SCRATCHDIR}

/bin/cp -f  ${SRCDIR}/*.f90    $SCRATCHDIR
/bin/cp -f  ${SRCDIR}/*.f      $SCRATCHDIR
/bin/cp -f  ${SRCDIR}/Makefile ${SCRATCHDIR}/Makefile1

#Replace global setting with local setting
/bin/cp -f params.f90 ${SCRATCHDIR}/


if [[ $CFT == ifort ]]; then
  if [[ $Test == 1 ]]; then
     echo 'Run model with debug options...'
     FFLAGS="-O0 -r8 -i4 -g -check all -fpe0 -warn -traceback -debug extended -nogen-interface"
  else
     FFLAGS="-fast -r8 -i4"
     echo 'Run model without debug options...'
  fi
elif [[ $CFT == mpifort ]]; then
  if [[ $Test == 1 ]]; then
     echo 'Using mpi with debug options...'
     FFLAGS="-O0 -r8 -i4 -g -check all -fpe0 -warn -traceback -debug extended -nogen-interface"
  else
     FFLAGS="-fast -r8 -i4"
     echo 'Using mpi without debug options...'
  fi
elif [[ $CFT == pgfortran ]]; then
  if [[ $Test == 1 ]]; then
     echo 'Run model with debug options...'
     FFLAGS="-Mbounds -Minfo=all -traceback -Mchkfpstk -Mchkstk -Mdalign 
-Mdclchk -Mdepchk -Miomutex -Mrecursive -Msave -Ktrap=fp -O0 -g -byteswapio"
  else
     FFLAGS="-fast -Mipa=fast,inline"
     echo 'Run model without debug options...'
  fi
elif [[ $CFT == gfortran ]]; then
  if [[ $Test == 1 ]]; then
     echo 'Run model with debug options...'
     FFLAGS="-O0 -g -fdefault-real-8 -fdefault-double-8 -fimplicit-none -Wall -Wline-truncation  -Wcharacter-truncation  -Wsurprising  -Waliasing  -Wimplicit-interface  -Wunused-parameter  -fwhole-file  -fcheck=all  -std=f2008  -pedantic  -fbacktrace"
  else
     FFLAGS="-O3  -fdefault-real-8 -fdefault-double-8 -march=native -fimplicit-none -Wall  -Wline-truncation  -fwhole-file  -std=f2008"
     echo 'Run model without debug options...'
  fi
else
  
  echo "Unknown Fortran compiler"
	exit
fi


FFLAGS="$FFLAGS $MPIINC"
cd ${SCRATCHDIR}
echo 's?$(FFLAGS)?'$FFLAGS'?g' >> flags.tmp
export MAKEFILE=Makefile1
sed -f flags.tmp $MAKEFILE > Makefile2
rm -rf flags.tmp

echo 's?$(CFT)?'$CFT'?g' >> flags.tmp
sed -f flags.tmp Makefile2 > Makefile3
rm -rf flags.tmp
echo 's?$(LFLAGS)?'$LFLAGS'?g' >> flags.tmp
sed -f flags.tmp Makefile3 > Makefile
make
rm -rf flags.tmp

mv DRAM ../citrate
cd ../
