#! /bin/sh

if [ $# -lt 5 ]; then
  echo "Usage: mbxmlutils-xsd2html <InXsdFile> <InDoxygenXmlDir> <FigureDir>"
  echo "                           <ProjectName> <DocygenPrefix>"
  echo "                           [<AddInXsdFile> ...]"
  echo "  InXsdFile: Input xsd file which should be converted to html"
  echo "  InDoxygenXmlDir: Directory of the Doxygen XML output of the"
  echo "                   corrospondending documentation"
  echo "  FigureDir: Colon separaded directory to search for figures (*.eps, *.png)"
  echo "  ProjectName: The name of the Project (e.g. 'OpenMBV')"
  echo "  DoxygenPrefix: Prefix of doxygen names (e.g. 'OpenMBV::')"
  echo "  AddInXsdFile: Additional input xsd files (only used for generating example code"
  exit
fi

FILE=$1 #"/home/markus/project/MBSim/openmbv/schema/openmbv.xsd"
DOXYGENDOCDIR=$2 #"/home/markus/project/MBSim/openmbv/doc_/xml"
FIGUREDIR=$3
PROJECT=$4 #"OpenMBV"
DOXYGENCLASSPREFIX=$5 #"OpenMBV::"
shift 5
ADDFILES="$@"


if [ ! -e $FILE ]; then
  echo "Input file $FILE not found!"
  echo "You must install the package before you can generate the documentation."
  exit 1
fi


DOXYGENFUNCTIONPREFIX1="set"
DOXYGENFUNCTIONPREFIX2="add"
BASENAME=$(dirname $FILE)/$(basename $FILE .xsd)
BASENAME2=$(basename $FILE .xsd)
prefix=/home/mbsim/win64-dailyrelease/local
EXEEXT=.exe

rm -f .$BASENAME2.done


if [ $DOXYGENDOCDIR != "none" ]; then
  echo "Combine Doxygen XML documentation"
  echo "  In file: $DOXYGENDOCDIR/index.xml"
  echo "  XSL file: $DOXYGENDOCDIR/combine.xslt"
  echo "  Out file: .$PROJECT.doxygen.xml"
  /usr/bin/Xalan -o .$PROJECT.doxygen.xml $DOXYGENDOCDIR/index.xml $DOXYGENDOCDIR/combine.xslt || exit 1
  echo "DONE"

  echo "Add Doxygen class and element function documentation to XML Schema"
  echo "  In file: $FILE"
  echo "  XSL file: ${prefix}/share/mbxmlutils/xsl/addDoxygenToXsd.xsl"
  echo "  Out file: $BASENAME.doxygen.xsd"
  /usr/bin/Xalan -o $BASENAME.doxygen.xsd \
    -p DOXYGENDOC "'$(pwd)/.$PROJECT.doxygen.xml'" \
    -p DOXYGENCLASSPREFIX "'$DOXYGENCLASSPREFIX'" \
    -p DOXYGENFUNCTIONPREFIX1 "'$DOXYGENFUNCTIONPREFIX1'" \
    -p DOXYGENFUNCTIONPREFIX2 "'$DOXYGENFUNCTIONPREFIX2'" \
    $FILE ${prefix}/share/mbxmlutils/xsl/addDoxygenToXsd.xsl || exit 1
  echo "DONE"
else
  echo "No Doxygen provided (argument = 'none'), just copy"
  cp $FILE $BASENAME.doxygen.xsd || exit 1
fi




HTMLDIR=$(sed -rne '/ targetNamespace *=/s|^.*targetNamespace *= *"([^"]+)".*$|\1|p' $BASENAME.doxygen.xsd | sed -re 's|\.|_|g;s|:|_|g;s|/|_|g')
echo "<root>" > addinput.xml
for F in $ADDFILES; do
  echo "<a>$F</a>" >> addinput.xml
done
echo "</root>" >> addinput.xml
echo "Generate HTML documentation"
echo "  In file: $BASENAME.doxygen.xsd"
echo "  XSL file: ${prefix}/share/mbxmlutils/xsl/xsdToHtml.xsl"
echo "  Out file: $HTMLDIR/index.html"
rm -rf $HTMLDIR
mkdir $HTMLDIR
/usr/bin/Xalan -o $HTMLDIR/index.html \
  -p PROJECT "'$PROJECT'" \
  -p DATETIME "'$(date "+%a %b %d %Y %H:%M:%S")'" \
  -p PWD "'$PWD'" \
  $BASENAME.doxygen.xsd ${prefix}/share/mbxmlutils/xsl/xsdToHtml.xsl || exit 1
echo "DONE"
echo "Remove all namespace declaration from the html output using sed (maybe a bug in Xalan)"
sed -re "s/ xmlns:[^ ]+ *= *\"[^\"]+\"//g" $HTMLDIR/index.html > $HTMLDIR/index.html_ || exit 1
mv -f $HTMLDIR/index.html_ $HTMLDIR/index.html || exit 1
echo "DONE"

echo "Copy *.png"
for D in $(echo $FIGUREDIR | tr ':' ' '); do
  ls $D/*.png >& /dev/null && cp -f $D/*.png $HTMLDIR
done

echo "Export equations"
echo "  In file: $HTMLDIR/index.html (modified by sed)"
echo "  XSL file: ${prefix}/share/mbxmlutils/xsl/xsdToEquation.xsl"
echo "  Out file: equation.txt"
# $HTMLDIR/index.html is html code which is not valid XML. Hence we extract all relevant img element
# using sed, convert it to valid XML and than process it with Xalan
(
echo "<root>"
cat $HTMLDIR/index.html |
tr '\n' ' ' |
sed -re "s@(< *img\>[^>]*\<class *= *\"(_eqn|_inlineeqn)\"[^>]*>)@\n<MBXMLUTILS/> \1</img>\n@g" |
grep "MBXMLUTILS"
echo "</root>"
) |
/usr/bin/Xalan -o equation.txt - ${prefix}/share/mbxmlutils/xsl/xsdToEquation.xsl || exit 1
echo "DONE"

# Generating equation
while grep -E "^MBXMLUTILS_BEGINEQN$" equation.txt >& /dev/null; do
  B=$(sed -rne "/^MBXMLUTILS_BEGINEQN$/=;/^MBXMLUTILS_BEGINEQN$/q" equation.txt)
  E=$(sed -rne "/^MBXMLUTILS_ENDEQN$/=;/^MBXMLUTILS_ENDEQN$/q" equation.txt)
  FILE=$(sed -rne "$[$B+1]p" equation.txt)
  echo "Generating equation in $FILE"
  cat << EOF > equation.tex
\documentclass{report}
\usepackage{graphicx,psfrag,amsmath,amssymb,amsbsy,color}
\setlength{\parindent}{0cm}
\setlength{\textwidth}{100cm}
\setlength{\textheight}{100cm}
\begin{document}
\pagestyle{empty}
EOF
  sed -rne "$[$B+2],$[$E-1]p" equation.txt >> equation.tex
  echo '\end{document}' >> equation.tex
  if latex -help >& /dev/null; then
    latex -halt-on-error -file-line-error equation.tex || exit 1
      dvips -E -o equation.eps equation.dvi || exit 1
      gs -q -dBATCH -dNOPAUSE -dEPSCrop -r144 -sDEVICE=pngalpha -sOutputFile=$FILE equation.eps || exit 1
  else
    echo "WARNING: Skip generation of png-equations for HTML documentation (latex, dvips or gs not found in PATH)"
  fi
  mv $FILE $HTMLDIR/$FILE
  sed -rne "$[$E+1],\$p" equation.txt >> equation.txt.new
  mv equation.txt.new equation.txt
done
rm -f equation.*

touch .$BASENAME2.done
