#!/bin/bash
# Script for patching  Doxygen xhtml output
#
#  Time-stamp: <2011-12-05 16:16:03 wamorita>
#
# Usage: xhtmlpatch.sh
#
#    Reset forces full re-generaton of documentation be removing output dirs
#    Quick skips the postprocessing of package-->namespace
#
# Assumes:
#   * Assumes envset.sh has setup environment
#   * Doxygen config file, Doxyfile, is present in same directory
#   * Log file output is to dodox.log
# set -xv

# Who am I / Where am I etc ?
Script=${0##*/}
ScriptDir=$(cd $(dirname $0) ; pwd)
Date=`date +%Y-%m-%d`

# Delete temp file on all exits
#trap "rm -f $tmp_file;exit" 1 2 3 15

Temp=tempfile
HTMLDIR=$OUTDIR/html

Components=_m_i_m_i_r__components_page.xhtml
Database=_database_page.xhtml

#  $1 = sed script
#  $2 = filename
function MonoLinePatch
{
    sed "$1" $2 > $Temp
    mv -f $Temp $2
    echo "Patched $2"
}

# Multiline patch
# $1 = sed script
# $2 = filename
function MultiLinePatch
{
    # Multi-line replace by transforming LFs, doing replace, restoring LFs
    sed ':a;N;$!ba;s/\n/\xef/g' $2 |
    sed "$1" |
    sed 's/\xef/\n/g' > $Temp
    mv -f $Temp $2
    echo "Patched $2"
}

echo "$Script - MonoLinePatching xhtml output"

cd $HTMLDIR
# Related Pages Patchs
MonoLinePatch '/<.div><.div>/d; s!</body>!</p></div></div></body>!' $Components
MonoLinePatch '/<.div><.div>/d; s!</body>!</p></div></div></body>!' $Database

# Class page correction patch
for classpage in class_*xhtml
do
    MultiLinePatch 's#</div>\xef</div>\xef<!--#</div>\xef<!--#g' $classpage
done

# Source page correction patch
for sourcepage in *source.xhtml
do
    MultiLinePatch 's#</div>\xef</div>\xef<!--#</div>\xef<!--#g' $sourcepage
done

