#! /bin/sh
#
# DESCRIPTION
# ===========
#
# replace <element>ginette</element> by a sequence like with element given
# in argument <a href="./ginette.html">ginette</a>
# in all html files in dirhtml given in argument
# We have to deal with the path of ginette.html in refhtml also given in
# argument.
#
# see call in savesaxo.sh
#
# EVOLUTIONS
# ==========
# $Id$
#
# fplod 2007-08-21T09:06:58Z aedon.locean-ipsl.upmc.fr (Darwin)
# add -e (element) parameter to define element syntax like
# <pro></pro> or <proidl>...</proidl>
# add -i parameter : this directory is the one where we are looking for
# html files to be modified. so replace output by dirhtml
# add -r parameter :  this directory is the one where we are looking for
# html files to be linked
# remove lowercase translation (because of idl help files which are in
# uppercase)
# 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 -o posix
set -u
#
command=$(basename ${0} .sh)
log_date=$(date -u +"%Y%m%dT%H%M%SZ")
log=/tmp/${command}.${log_date}
#
usage=" Usage : ${command} -i dirhtml -r refhtml -e element"
#
while [ ${#} -gt 0 ]
do
   case ${1} in
      -i)
         dirhtml=${2}
         shift
      ;;
      -r)
         refhtml=${2}
         shift
      ;;
      -e)
         element=${2}
         shift
      ;;
      -h)
         echo "${usage}"
         exit 0
      ;;
      *)
         # other choice
         echo "${usage}"
         exit 1
      ;;
   esac
   # next flag
   shift
done
#
# check for dirhtml
if [ ! -d ${dirhtml} ]
then
   echo "eee : ${dirhtml} not found"
   exit 1
fi
# ++ check for readable/writable
#
# check for refhtml
if [ ! -d ${refhtml} ]
then
   echo "eee : ${refhtml} not found"
   exit 1
fi
# ++ check for readable
#
# check for element
if [ "${element}" == "" ]
then
   echo "eee : ${element} empty"
   exit 1
fi
#
# first find all files containing <element>...</element>
list_html_element=$(find ${dirhtml} -name "*.html" -exec grep -l "<${element}>.*</${element}>" {} \;)
if [ "${list_html_element}" == "" ]
then
   echo "iii : no <${element}>...</${element}> in html files"
else
   for file_html in ${list_html_element}
   do
      #echo "file_html ${file_html}"
      #read a
      fpath=$(dirname ${file_html} | sed -e "s+\(${dirhtml}/\)\(.*\)+\2+")
      #echo "fpath ${fpath}"
      #read a
      list_link=$(tr -s " " "\n" < ${file_html} | grep "<${element}>.*</${element}>" | sed -e "s/^.*<${element}>//" -e "s/<\/${element}>.*$//")
      # echo "liste link" ${list_link}
      # read a
      for link in ${list_link}
      do
         # replace <element>something</element> by
         #<a href="something.html">something</a>
         # modulehtml is the html file name to be used
         modulehtml=${link}.html
         # lpath is the path on module relatively to the location of
         # the html file containing the <element>...</element>
         lpath=$(find ${refhtml} -name "${modulehtml}")
         if [ "${lpath}" = "" ]
         then
            echo "eee : path of ${modulehtml} not found under ${refhtml}"
            echo "eee : ${link} is used in ${file_html}"
         else
            lpath=$(dirname ${lpath} | sed -e "s+\(${refhtml}/\)\(.*\)+\2+")
            #echo "path du fichier html ${fpath}"
            #echo "lpath ${lpath}"
            #read a
            if [ "${lpath}" = "${fpath}" ]
            then
               path="./"
            fi
            if [ "${lpath:0:1}" = "/" ]
            then
               # absolute path
               path=${lpath}
            else
               nblev=$(echo ${fpath} |  sed -e "s@/\$@@" | awk -F "/" '{print NF}')
               relpath=""
               ilev=1
               while [ ${ilev} -le ${nblev} ]
               do
                  relpath="${relpath}../"
                  ilev=$(( ${ilev} + 1 ))
               done
               path=${relpath}/${lpath}
            fi
            #echo "path ${path}"
            #read a
            cat <<EOF > /tmp/pro2href${$}.sed
1{x;s@^@first@;x;}
1,/<${element}>${link}<\/${element}>/{x;/first/s///;x;s@<${element}>${link}<\/${element}>@<a href="${path}/${link}.html">${link}<\/a>@;}
EOF
            sed -f /tmp/pro2href${$}.sed \
               ${file_html} > ${file_html}_modify
            #diff  ${file_html}  ${file_html}_modify
            #read a
            mv ${file_html}_modify ${file_html}
            rm /tmp/pro2href${$}.sed
         fi
      done
   done
fi
exit 0
