#!/bin/sh

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

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

grep_html_files_for_bad_characters()
{
    echo "**************************************" >> $TO_HTML_LOG
    echo "Results of grep for errant backslashes" >> $TO_HTML_LOG
    echo "**************************************" >> $TO_HTML_LOG
    if grep '\\[^n]' $HTML_TEST_DIR/*.html >> $TO_HTML_LOG ; then
       ERROR=1
       echo
       echo "cc_manual_to_html of $DIR/$SUBDIR FAILED: backslashes"
    fi
}

grep_log_for_error_messages()
{
    if  grep "rror" $TO_HTML_LOG > /dev/null ; then
       ERROR=1
       echo
       echo "cc_manual_to_html of $DIR/$SUBDIR FAILED: Error message"
    fi
    if grep "Unknown" $TO_HTML_LOG > /dev/null ; then
       ERROR=1
       echo
       echo "cc_manual_to_html of $DIR/$SUBDIR FAILED: Unknown command"
    fi
    if grep "an't" $TO_HTML_LOG > /dev/null ; then
       ERROR=1
       echo
       echo "cc_manual_to_html of $DIR/$SUBDIR FAILED: Can't find something"
    fi
    if grep "not found" $TO_HTML_LOG > /dev/null ; then
       ERROR=1
       echo
       echo "cc_manual_to_html of $DIR/$SUBDIR FAILED: Something not found"
    fi
}

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

   if  [ -d html_test ] ; then
      mv html_test html_test.bck
   fi
   mkdir html_test

   ERROR=0

   if [ -f docdirs ]; then
      PASSED=""
      FAILED=""

      DOC_SUBDIRS=`cat docdirs`
      for SUBDIR in $DOC_SUBDIRS; do
         ERROR=0
         echo
         echo $SUBDIR
         echo
         LATEX_CONV_INPUTS=${SUBDIR}:../../examples:../../demo:${orig_inputs}
         export LATEX_CONV_INPUTS
         HTML_TEST_DIR="html_test/$SUBDIR"
         TO_HTML_LOG="$SUBDIR/to_html.log"
         if [ $SUBDIR != "Title" ]; then
            if [ -f $SUBDIR/html_wrapper ]; then

               /bin/rm -f $TO_HTML_LOG

               echo "*********************" >  $TO_HTML_LOG
               echo "cc_manual_to_html log" >> $TO_HTML_LOG
               echo "*********************" >> $TO_HTML_LOG
   
               if [ $DIR = "basic" -o $DIR = "support" ]; then
                  cc_manual_to_html -author "<A HREF=http://www.cgal.org/ TARGET=_top>The CGAL project</A>" -date "`date +%b` `date +%e`, `date +%Y`" -title "$TITLE" -extended -header "../../../include" -o html_test user_part.tex $SUBDIR/html_wrapper -ref_manual ref_part.tex ${SUBDIR}/${SUBDIR}_ref/html_wrapper >> $TO_HTML_LOG 2>&1
               else
                  cc_manual_to_html -author "<A HREF=http://www.cgal.org/ TARGET=_top>The CGAL project</A>" -date "`date +%b` `date +%e`, `date +%Y`" -title "$TITLE" -extended -header "../../../include" -o html_test $SUBDIR/html_wrapper >> $TO_HTML_LOG 2>&1
               fi

               grep_html_files_for_bad_characters
               grep_log_for_error_messages

               if [ $ERROR != 1 ] ; then
                  PASSED="$PASSED $SUBDIR"
               else
                  FAILED="$FAILED $SUBDIR"
               fi
               echo >> $TO_HTML_LOG
               echo "EOF" >> $TO_HTML_LOG
           else
               FAILED="$FAILED $SUBDIR"
               echo
               echo "HTML conversion of $SUBDIR FAILED: missing html_wrapper file!"
               echo "HTML conversion of $SUBDIR FAILED: missing html_wrapper file!" >> $TO_HTML_LOG
            fi
            mv $TO_HTML_LOG html_test/$SUBDIR
         fi
     done
     
     cp Title/title.html html_test
     echo $PASSED > docdirs_passed_html
     echo $FAILED > docdirs_failed_html

   else
     TO_HTML_LOG="html_test/to_html.log"
     HTML_TEST_DIR="html_test"
     SUBDIR="" 
     echo "*********************" > $TO_HTML_LOG
     echo "cc_manual_to_html log" >> $TO_HTML_LOG
     echo "*********************" >> $TO_HTML_LOG

     cc_manual_to_html -author "<A HREF=http://www.mpi-sb.mpg.de/GALIA target=_top>The GALIA project</A>" -date "`date +%b` `date +%e`, `date +%Y`" -header "../../../include" -o html_test wrapper.tex >> $TO_HTML_LOG 2>&1

     grep_html_files_for_bad_characters
     grep_log_for_error_messages
     
     echo >> $TO_HTML_LOG
     echo "EOF" >> $TO_HTML_LOG
     cp title.html html_test
   fi

   if [ -d html_test -a -r html_test/contents.html -a -d html_test.bck ]; then
      rm -rf html_test.bck
   fi

   echo "test_html:  ...leaving directory $DIR"
   cd ..
done




syntax highlighted by Code2HTML, v. 0.9.1