#!/bin/bash

# pFUnit tests/MakeDependenciesInclude

# Generates makefile dependencies from "use" statements in
# Fortran files. Specifically built to pFUnit conventions.
#
# M. Rilee, 2013-0815

# TODO:  Move this and its cousins to a single "$(TOP)/bin/MakeDependenciesInclude".

# Dependency injection...
# Intermediate & target files...
export tmpFile=./DependencyCheck-1.tmp; 
export outFile=./dependencies.inc

# We do not automatically check to see if references are satisfied
# within the using file, so we can IGNORE those references by naming
# them here.  For example, SUT and MockSUT are provided in the file in
# which they are "used."
#
IGNORE="MockSUT|SUT"
# May need to consider case, however if we stick to our current coding
# style, we should be okay.

# Have trouble with commented use statements... see grep -v "\!" below.
# Need to watch out for use statements that refer to tokens with use or used.
# Also may have some trouble with files/modules with "use" in their names.

echo "# pFUnit: tests/MakeDependenciesInclude..." > ${tmpFile}; 
echo -e "# tests/${outFile} automatically generated: " `date "+%Y-%m%d-%H%M-%S"` "\n" >> ${tmpFile}; 

for i in *.F90; do 
   echo `echo $i | cut -f 1 -d. - `".o: " | tr -d "\n" >> ${tmpFile}; 
   grep -i use $i | grep -v used | cut -d, -f1 - | grep -v "\!" | sort | tr -s " \t\v" | uniq - | \
      egrep -v ${IGNORE} | \
      sed 's/use//g' | sed 's/_mod/.o/g' | egrep "\.o" | grep -v "\!\!" | tr -d "\n" | tr -s " \t\v" >> ${tmpFile};  
   echo -e "\n" >> ${tmpFile};
done; 

cat ${tmpFile} | grep -iv selfTests.o > ${outFile}; 
   echo -e "\nifeq (\$(USEMPI),YES)\n" >> ${outFile}; 
   cat ${tmpFile} | grep -i selfTests.o >> ${outFile}; 
   echo -e "\nelse\n" >> ${outFile}; 

cat ${tmpFile} | grep -i selfTests.o | sed 's/[_a-zA-Z0-9]*[m|M]pi[._a-zA-Z0-9]*//g' >> ${outFile}; 
   echo -e "\nendif\n" >> ${outFile}

# The following converts the occurrences of .o in ${outFile} to $(OBJ_EXT)
sed -i -e 's&\.o&$(OBJ_EXT)&g' ${outFile}
\rm -f ${outFile}-e
