#!@BOURNE_SHELL@
#
# Copyright (C) 2005-2021 ABINIT Group (Yann Pouillon)
#
# This file is part of the ABINIT software package. For license information,
# please see the COPYING file in the top-level directory of the ABINIT source
# distribution.
#

#
# IMPORTANT NOTE
#
# Do not edit this file, as it is potentially harmful (i.e. it removes
# files). YOU HAVE BEEN WARNED !
#

# Utilities
SED="@SED@"

# Fortran preprocessor
FPP="@FPP@"
FPPFLAGS="@FPPFLAGS@"
DEFS=""
INCLUDES=""
INCLUDES_NAG="@sd_netcdf_fcflags@"

# Fortran compiler
FC="@FC_NOWRAP@"
FCFLAGS=""
abi_fc_vendor="@abi_fc_vendor@"
fc_debug="@abi_source_debug_enable@"
fc_cmdline=""
fc_modsearch=""
fc_onepass="no"

# Rewrite rules
absoft_modsearch="-p"
sun_modsearch="-M"

# ---------------------------------------------------------------------------- #

# Header
echo "ABINIT WRAPPER BEGIN"

fc_cmdline="${@}"

# ---------------------------------------------------------------------------- #

# Isolate defs, includes and source file
for fc_opt in `echo "${fc_cmdline}" | sed -e 's/ -I[ ]*/ -I/g'`; do
  # Check for standard definitions
  fc_def=`echo "${fc_opt}" | grep '^-D'`
  if test "${fc_def}" != ""; then
    DEFS="${DEFS} ${fc_opt}"
  fi

  # Check for IBM definitions
  fc_ibm=`echo "${fc_opt}" | grep '^-WF,'`
  if test "${fc_ibm}" != ""; then
    fc_ibm=`echo "${fc_ibm}" | sed -e 's/-WF,//;s/,/ /g'`
    for fc_ibm_opt in ${fc_ibm}; do
      fc_def=`echo "${fc_ibm_opt}" | grep '^-D'`
      if test "${fc_def}" != ""; then
        DEFS="${DEFS} ${fc_ibm_opt}"
      fi
    done
  fi
  fc_def="${fc_ibm}"

  # Check for includes
  fc_inc=`echo "${fc_opt}" | grep '^-I'`
  if test "${fc_inc}" != ""; then
    INCLUDES="${INCLUDES} ${fc_opt}"
  fi

  # Find f90 file names
  fc_grep_src=`echo "${fc_opt}" | grep '\.[Ff]90$'`

  # Otherwise find f77 file names
  if test "${fc_grep_src}" = ""; then
    fc_grep_src=`echo "${fc_opt}" | grep '\.[Ff]$'`
  fi

  # Extract filename information and create target filenames
  if test "${fc_grep_src}" != ""; then
    fc_src="${fc_grep_src}"
    fc_ext=`echo "${fc_src}" | sed -e 's/.*\.//'`
    fc_obj=`echo "${fc_src}" | sed -e 's,.*/,,; s,\.F90$,\.o,; s,\.F$,\.o,'`
    fc_cpp=`echo "${fc_src}" | sed -e 's,.*/,,;s,\.F90$,_cpp.f90,;s,\.F$,_cpp.f,'`
    fc_cpp_obj=`echo "${fc_src}" | sed -e 's,.*/,,;s,\.F90$,_cpp.o,;s,\.F$,_cpp.o,'`

    # For upper case F90 or F files, we need to preprocess
    case "${fc_ext}" in
      F|F90)
        do_cpp="yes"
        ;;
      f|f90)
        do_cpp="no"
        ;;
    esac
  fi
  if test "${fc_def}" = "" -a "${fc_inc}" = "" -a "${fc_grep_src}" = ""; then
    tmp=`echo ${FPPFLAGS} | egrep -- "(^| )${fc_opt}"`
    if test "$tmp" == ""; then
        FCFLAGS="${FCFLAGS} ${fc_opt}"
    fi
  fi
done
if test "${abi_fc_vendor}" = "nag"; then
  INCLUDES="${INCLUDES} ${INCLUDES_NAG}"
fi

# ---------------------------------------------------------------------------- #

# Preprocess and compile separately
if test "${do_cpp}" = "yes"; then
  echo "${FPP} ${FPPFLAGS} ${DEFS} ${INCLUDES} ${fc_src} > ${fc_cpp}"
  ${SED} -e '/^!/d;/^[ ]*!/d' ${fc_src} | \
    ${FPP} ${FPPFLAGS} ${DEFS} ${INCLUDES} | \
    ${SED} -e '/^#pragma/d' > ${fc_cpp}
  if test ! -s "${fc_cpp}"; then
    echo "ABINIT WRAPPER END (exit code 10)"
    exit 10
  fi

  # Remove stray object files
  rm -f ${fc_obj}

  # DEFS should not be included anymore, since they may trigger
  # another preprocessing from the buggy compiler
  echo "${FC} ${fc_cpp} ${FCFLAGS} ${INCLUDES}"
  ${FC} ${fc_cpp} ${FCFLAGS} ${INCLUDES}
  fc_ret="${?}"
  if test "${fc_debug}" = "no" -a "${fc_ret}" = "0"; then
    rm ${fc_cpp}
  fi

  # Link object to original name, instead of _cpp.o, if not already
  # specified by -o objname
  if ! test -f ${fc_obj}; then
    ln -s ${fc_cpp_obj} ${fc_obj}
  fi
else
  fc_cmdline=`echo ${fc_cmdline} | sed -e "s/${FPPFLAGS}//"`
  echo "${FC} ${fc_cmdline}"
  ${FC} ${fc_cmdline}
  fc_ret="${?}"
fi

# Footer
echo "ABINIT WRAPPER END (exit code ${fc_ret})"
exit "${fc_ret}"
