#!/bin/bash
# html2texi,v

FILES1="index.html strict.html ptools.html"
FILES2="parsers.html lexers.html fparsers.html bparsers.html grammars.html"
FILES3="printers.html pprintf.html pretty.html"
FILES4="locations.html ml_ast.html ast_transi.html ast_strict.html q_ast.html pcaml.html directives.html syntext.html opretty.html redef.html quot.html revsynt.html scheme.html macros.html pragma.html extfun.html"
FILES5="commands.html library.html sources.html about.html"
FILES="$FILES1 1 $FILES2 2 $FILES3 3 $FILES4 4 $FILES5"
VERSION="$(grep "value version =" ../../main/pcaml.ml | sed -e 's/^[^"]*"\([^"]*\).*$/\1/')"

/bin/echo '\input texinfo   @c -*-texinfo-*-
@c %**start of header
@setfilename camlp5.info'
/bin/echo "@settitle Camlp5 manual $VERSION"
/bin/echo '@c %**end of header'

/bin/echo '
@titlepage
@title Camlp5 manual
@author Daniel de Rauglaudre
@end titlepage'

/bin/echo '
@node Top
@top Camlp5: a preprocessor pretty printer for OCaml'

/bin/echo "@noindent Version $VERSION"

/bin/echo '
@menu'

(
  /bin/echo 'sed \'
  for i in $FILES; do
    if [ "$i" = "1" ]; then
      :
    elif [ "$i" = "2" ]; then
      :
    elif [ "$i" = "3" ]; then
      :
    elif [ "$i" = "4" ]; then
      :
    elif [ "$i" = "5" ]; then
      :
    else
      N=$(grep '<h1' $i | sed -e 's|<h1[^>]*>||' -e 's|</h1>||')
      /bin/echo "  -e 's/@pxref{$i}/@pxref{$N}/' \\"
    fi
  done
  /bin/echo
) > tmp

for i in $FILES; do
  if [ "$i" = "1" ]; then
    /bin/echo
    /bin/echo 'Parsing Tools'
    /bin/echo
  elif [ "$i" = "2" ]; then
    /bin/echo
    /bin/echo 'Printing Tools'
    /bin/echo
  elif [ "$i" = "3" ]; then
    /bin/echo
    /bin/echo 'Language extensions'
    /bin/echo
  elif [ "$i" = "4" ]; then
    /bin/echo
    /bin/echo 'Appendix'
    /bin/echo
  else
    N=$(grep '<h1' $i | sed -e 's|<h1[^>]*>||' -e 's|</h1>||')
    /bin/echo "* $N::"
  fi
done

/bin/echo '
@end menu'

/bin/echo

chapter=chapter

for i in $FILES; do
  if [ "$i" = "1" ]; then
    :
  elif [ "$i" = "2" ]; then
    :
  elif [ "$i" = "3" ]; then
    :
  elif [ "$i" = "4" ]; then
    :
  elif [ "$i" = "5" ]; then
    chapter=appendix
  else
    N=$(grep '<h1' $i | sed -e 's|<h1[^>]*>||' -e 's|</h1>||')
    /bin/echo "
@node $N
@$chapter $N"
    cat $i |
    sed \
      -e 's|^\(.*\)<ul>|\n\1<ul>|' \
      -e 's|^\(.*\)<dl>|\n\1<dl>|' |
    sed \
      -e '{:b; /^$/N; s/^\n$//; tb}' |
    sed \
      -e 's|</p>|\n</p>|' \
      -e 's|\([^ ]\)</li>|\1\n</li>|g' \
      -e 's|\([^ ]\)</dd>|\1\n</dd>|g' \
      -e '/<pre[^>]*>/,/<\/pre>/s/ /~~/g' |
    sed \
      -e '/<p>/,/<\/p>/s/^ *//' \
      -e '/<li>/,/^ <\/li>/s/^ *//' \
      -e '/<dd>/,/<\/dd>/s/^ *//' \
      -e '/<div[^>]*>/,/<\/div>/s/^ *//' \
      -e '/<pre[^>]*>/,/<\/pre>/s/~~/ /g' |
    sed \
      -e '{/<table[^>]*>$/N; :b; /<tr>$/N; s/\n *//; tb; /<\/tr>$/N; s/\n *//; tb; /<\/th>$/N; s/\n *//; tb; /<\/td>$/N; s/\n *//; tb; /<thead>$/N; s/\n *//; tb; /<\/thead>$/N; s/\n *//; tb; /<tbody>$/N; s/\n *//; tb; /<\/tbody>$/N; s/\n *//; tb}' \
      -e 's|@|@@|g' \
      -e 's|{|@{|g' -e 's|}|@}|g' \
      -e "s|''|@w{'}@w{'}|g" |
    sed \
      -e '{s|<a href=\"\([^.]*\.html\)[^>]*>|(@pxref{\1}) |; tb; bc; :b; h; s|(@pxref{.*}) ||; :c}' \
      -e '{/<\/a>/bd; be; :d; /<\/a/G; x; s/^.*$//; x; s|</a>\(.*\)\n.*\((@pxref{.*})\).*$| \2\1|; s/\n//; :e}' |
    sed \
      -e '/<div id="tableofcontents">/,/<\/div>/d' \
      -e '/<div class="trailer">/,$d' \
      -e 's|<div[^>]*>|@noindent |' -e 's|</div>||' \
      -e '1,/<h1/d' \
      -e 's|<h2>|@section |' -e 's|</h2>||' \
      -e 's|<h3>|@subsection |' -e 's|</h3>||' \
      -e 's|<h4>|@subsubsection |' -e 's|</h4>||' \
      -e 's|<h5>|@noindent @strong{|' -e 's|</h5>|}|' \
      -e '/<pre>/,/<\/pre>/s/^  //' \
      -e 's|<br/>|@*|g' \
      -e 's|^ *<p>|@noindent |' -e '/<\/p>/d' \
      -e 's|^ *<ul>|@itemize|' -e 's|^ *</ul>|@end itemize|' \
      -e 's|^ *<ol>|@enumerate|' -e 's|^ *</ol>|@end enumerate|' \
      -e 's|^ *<li>|@item |' -e '/<\/li>/d' \
      -e 's|^ *<dl><dd>|@exampleindent 5\n@quotation |' \
      -e 's|^ *</dd></dl>|@end quotation|' \
      -e 's|^ *<dl[^>]*>|@exampleindent 5\n@table @asis|' \
      -e 's|^ *</dl>|@end table|' \
      -e 's|^ *<dt>|@item |' -e 's|</dt>||' \
      -e 's|^ *<dd>||' -e '/<\/dd>/d' \
      -e 's|<b>|@strong{|g' -e 's|</b>|}|g' \
      -e 's|<em>|@emph{|g' -e 's|</em>|}|g' \
      -e 's|<tt[^>]*>||g' -e 's|</tt>||g' \
      -e 's|<code>||g' -e 's|</code>||g' \
      -e 's|<span [^>]*>|@emph{|g' -e 's|</span>|}|g' \
      -e 's|</a>||g' \
      -e 's|<a [^>]*>||g' \
      -e 's|<pre>|@exampleindent 5\n@example|' -e 's|</pre>|@end example|' \
      -e 's|<pre [^>]*>|@exampleindent 0\n@example|' \
      -e 's|<table border="1">|@multitable @columnfractions .35 .35 .23\n|' \
      -e 's|<table[^>]*>|\n@multitable @columnfractions .50 .50\n|' \
      -e 's|</table>|@end multitable|' \
      -e 's|<tr><th[^>]*>|@item |g' -e 's|</th><th[^>]*>| @tab |g' \
      -e 's|</th></tr>|\n@item\n|g' \
      -e 's|<tr><td>|@item |g' -e 's|</td><td[^>]*>| @tab |g' \
      -e 's|</td></tr>|\n|g' \
      -e 's/&lt;/</g' -e 's/&gt;/>/g' -e 's/&nbsp;/ /g' -e 's/&amp;/\&/g' \
      -e "s|<version/>|$VERSION|" \
      -e '/<licence\/>/r ../../LICENSE' -e '/<licence\/>/d'
  fi
done |
sh ./tmp

/bin/echo '
@bye'
