#! /bin/sh
# 
# Used by bmake/common rule html to replace #includes in the source files with links when possible
#
#
filename=$1
ROOT=$2
PETSC_DIR=$3
#
# remove any ../ from the ROOT that are also in the filename
#
root=`echo $filename | sed 's?/include/[a-zA-A]*.h??g'`
TROOT=`echo $ROOT | sed "s?$root??g"`

while [ 1 -ne 0 ]; do
  read -r line 
  if [ $? -ne 0 ] ; then break; fi
#
#  To slightly reduce size of .html file change line# labels to l#
#
#  line=`echo $line | sed -e 's?name="line\([0-9]*\)"?name="l\1"?g'`
#
#  look for include and replace them 
#
  r=`echo $line | egrep include`
  if [ "$?" = 0 ]; then \
    includename=`echo $line | sed -e 's?#include [ ]*</font><font color=\"#666666\">\"\([a-z./]*\)\"</font>?\1?g' -e 's?<a name="line[0-9]*">[ 0-9]*: </a><font color="#A020F0">??g' -e 's?<font color="#A020F0">[ ]*</font><font color="#B22222">[/*a-z.I "]*</font><font color="#A020F0"></font>??g' -e 's?<font color="#A020F0">[ ]*</font>??g'` 
    linenumber=`echo $line | sed 's?\(<a name="line[0-9]*">[ 0-9]*: </a>\)[.# =0-9><"a-z/A-Z:*]*?\1?g'` 
    if [ -f "$includename" ] ; then
      echo "${linenumber} #include <A href=\"${includename}.html\">${includename}</A>"  
    elif [ -f "${ROOT}/include/${includename}" ] ; then
      if [ "$TROOT" = "" ] ; then
        echo "${linenumber} #include <A href=\"${includename}.html\">${includename}</A>"
      else 
        echo "${linenumber} #include <A href=\"${ROOT}/include/${includename}.html\">${includename}</A>"
      fi
    elif [ -f "${ROOT}/${includename}" ] ; then
      echo "${linenumber} #include <A href=\"${ROOT}/${includename}.html\">${includename}</A>"
    else
      /bin/echo  "$line"
    fi
  else
    /bin/echo "$line"
  fi
done

