#! /bin/sh
# 
# Used by the makefile lib/petsc/conf/rules html to replace #includes in the source files with links when possible
#
#
filename=$1
PETSC_DIR=$2
#
#
root=`dirname $filename`
cd $root
p=`pwd`
ROOT=`echo $p |sed s?${PETSC_DIR}??g | sed s?/[A-Za-z]*?../?g | sed s?..//??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?<a name="line[0-9]*">[ 0-9]*: </a><font color="#A020F0">??g' -e 's?#include [ ]*&lt;\([a-z./]*\)&gt;[a-zA-Z0-9/<>#*"=. ]*?\1?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\">&lt;${includename}&gt;</A>"  
    elif [ -f "${ROOT}include/${includename}" ] ; then
      echo "${linenumber} #include <A href=\"${ROOT}include/${includename}.html\">&lt;${includename}&gt;</A>"
    elif [ -f "${ROOT}/${includename}" ] ; then
      echo "${linenumber} #include <A href=\"${ROOT}${includename}.html\">&lt;${includename}&gt;</A>"
    else
      /bin/echo  "$line"
    fi
  else
    /bin/echo "$line"
  fi
done

