#! /bin/sh
# replace <pro>ginette</pro> by a sequence like
# <a href="./ginette.html">ginette</a>
# we have to deal with the path of ginette.html ...
#
# see call in savesaxo.sh
#
# update :
# $Id$
# fplod 2007-08-20T11:25:39Z aedon.locean-ipsl.upmc.fr (Darwin)
# correction from 
# 4.11. How do I match only the first occurrence of a pattern?
# in sed faq http://www.student.northpark.edu/pemente/sed/sedfaq4.html#s4.11
#++ still not working perfectly because path of links on the different occurences of <pro>...</pro> may not be the same 
# ex: see restoreboxparam.pro : domdef and saveboxparam
# fplod 2007-06-26T13:32:06Z aedon.locean-ipsl.upmc.fr (Darwin)
# improvment for multiple occurences of <pro>...</pro> on one line
# see http://www.gentoo.org/doc/en/articles/l-sed2.xml 
# Sed by example, Part 2
# especially for directory-overview files
# fplod 2007-03-20T14:02:14Z aedon.locean-ipsl.upmc.fr (Darwin)
# creation
#
set -u
output=${1} # ++ parameter
#
# first find all files containing <pro>...</pro>
list_html_pro=$(find ${output} -name "*.html" -exec grep -l "<pro>.*</pro>" {} \;)
if [ "${list_html_pro}" = " " ]
then
   echo "iii : no <pro>...</pro> in html files"
else
   for file_html in ${list_html_pro}
   do
#      echo "file_html ${file_html}"
#      read a
       fpath=$(dirname ${file_html} | sed -e "s+\(${output}/\)\(.*\)+\2+")
       list_link=$(tr -s " " "\n" < ${file_html} | grep "<pro>.*</pro>"   | sed -e "s/^.*<pro>//" -e "s/<\/pro>.*$//")
#      echo "liste link" ${list_link}
#      read a
       for link in ${list_link}
       do
          # replace <pro>something</pro> by
          #<a href="something.html">something</a>
          # modulehtml is the html file name to be used
          modulehtml=${link}.html
          modulemin=$(echo ${modulehtml} | tr "[:upper:]" "[:lower:]" )
          # lpath is the path on module relatively to the location of
          # the html file containing the <pro>...</pro>
          lpath=$(find ${output} -name "${modulemin}")
          if [ "${lpath}" = "" ]
          then
             echo "eee : path of ${modulemin} not found under ${output}"
             echo "eee : ${link} is used in ${file_html}"
          else
             lpath=$(dirname ${lpath} | sed -e "s+\(${output}/\)\(.*\)+\2+")
#echo "path du fichier html ${fpath}"
#echo "lpath ${lpath}"
             if [ "${lpath}" = "${fpath}" ]
             then
                relpath="./"
             else
                nblev=$(echo ${fpath} |  sed -e "s@/\$@@" | awk -F "/" '{print NF}')
           #     nblev=$(( ${nblev} + 1))
                relpath=""
                ilev=1
                while [ ${ilev} -le ${nblev} ]
                do
                   relpath="${relpath}../"
                   ilev=$(( ${ilev} + 1 ))
                done
                relpath=${relpath}/${lpath}
             fi
#echo "relpath ${relpath}"
             cat <<EOF > /tmp/pro2href${$}.sed
1{x;s@^@first@;x;}
1,/<pro>${link}<\/pro>/{x;/first/s///;x;s@<pro>${link}<\/pro>@<a href="${relpath}/${link}.html">${link}<\/a>@;}
EOF
             sed -f /tmp/pro2href${$}.sed \
             ${file_html} > ${file_html}_modify
             mv ${file_html}_modify ${file_html}
             rm /tmp/pro2href${$}.sed
          fi
       done
   done
fi
exit 0
