#!/bin/sh

if [ $# -ne 0 ]; then
    DOCDIRS="$*"
elif [ -f wrapper.tex ]; then
    DOCDIRS="."
elif [ -f docdirs ]; then
    DOCDIRS=`cat docdirs`
else
    DOCDIRS=""
fi

LATEX_CONV_INPUTS="../../examples:../../demo:$LATEX_CONV_INPUTS"
export LATEX_CONV_INPUTS

for DIR in $DOCDIRS; do
   cd $DIR
   echo "tohtml: Now in directory $DIR ..."
   CURR_DIR=`pwd`
   case "$CURR_DIR" in
        *installation)  TITLE="CGAL Installation Guide";
                        INCL="-header ../../include";;
        *general)       TITLE="CGAL Reference Manual - General Introduction";
                        INCL="-header ../../include";;
        *kernel)        TITLE="CGAL 2D and 3D Kernel Manual";
                        INCL="-header ../../include";;
        *kernel_d)      TITLE="CGAL dD Kernel Manual";
                        INCL="-header ../../include";;
        *basic)         TITLE="CGAL Basic Library Manual";
                        INCL="-header ../../../include";;
        *support)       TITLE="CGAL Support Library Manual";
                        INCL="-header ../../../include";;
        *use_of_stl)    TITLE="The Use of STL and STL Extensions in CGAL";
                        INCL="";;
        *)              echo "UNKNOWN manual part; title will be CGAL Reference Manual";
                        TITLE="CGAL Reference Manual";
                        INCL="-header ../../include";;
   esac

   if [ -d html ]
   then
       mv html html.bck
   fi

   mkdir html
   HTML_FILES=`ls *.html`
   if [ "$HTML_FILES" != "" ]; then
      cp *.html html
   fi

   if [ -f docdirs ]; then
      if [ -f docdirs_passed_html ]; then
         SUB_DIRS=`cat docdirs_passed_html`
      else
         SUB_DIRS=`cat docdirs`
      fi
      DOCMAINS=""
      for SUB_DIR in $SUB_DIRS; do
          if [ $SUB_DIR != "Title" ]; then
#             DOCMAINS="$DOCMAINS $SUB_DIR/html_wrapper ${SUB_DIR}_ref/html_wrapper"
             DOCMAINS="$DOCMAINS $SUB_DIR/html_wrapper"
          fi
      done
      EXTENDED="-extended"
   else
      SUB_DIRS=""
      DOCMAINS=wrapper.tex
      EXTENDED=""
   fi

   if [ -f wrapper.bbl -a -s wrapper.bbl ]; then
      BBL="-bbl wrapper.bbl"
   else
      BBL=""
   fi

   echo "*********************" > html/to_html.log
   echo "cc_manual_to_html log" >> html/to_html.log
   echo "*********************" >> html/to_html.log

   cc_manual_to_html -author "<A HREF=""http://www.cgal.org"" target=""_top"">www.cgal.org</A>" -date "`date +%b` `date +%e`, `date +%Y`" -title "$TITLE" $EXTENDED $INCL $BBL -aux wrapper.aux -o html ${DOCMAINS} >> html/to_html.log 2>&1

   cd html
   echo "*******************************" >> to_html.log
   echo "results of grep for references " >> to_html.log
   echo "*******************************" >> to_html.log
   ref_grep >> to_html.log

   echo "********************************" >> to_html.log
   echo "results of grep for backslashes " >> to_html.log
   echo "********************************" >> to_html.log
   backslash_grep >> to_html.log
   cd ..

   echo >> html/to_html.log
   echo "EOF" >> html/to_html.log

   if [ -f htmlfiles ]; then
      echo "copying html files now..."
      cp `cat htmlfiles | dos2unix` html
   fi

   for SUB_DIR
       in $SUB_DIRS
   do
       if [ -f $SUB_DIR/htmlfiles ]
       then
	   (cd $SUB_DIR;
	   cp `cat htmlfiles | dos2unix` ../html/$SUB_DIR
           cd ..
	   ) 
       fi
   done


   if [ -d Title ]; then
      cp Title/title.html html
   else
      cp title.html html
   fi
   
   if [ -d html -a -r html/contents.html -a -d html.bck ]
   then
       rm -rf html.bck
   fi
   echo "tohtml: ...leaving directory $DIR"
   cd ..
done


syntax highlighted by Code2HTML, v. 0.9.1