#!/bin/sh
# html2latex,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"
VERSION="$(grep "value version =" ../../main/pcaml.ml | sed -e 's/^[^"]*"\([^"]*\).*$/\1/')"

FILES="$FILES1 1 $FILES2 2 $FILES3 3 $FILES4 4 $FILES5"

/bin/echo '\documentclass[twoside]{report}
\marginparwidth 0pt \oddsidemargin 0pt \evensidemargin 0pt \marginparsep 0pt
\topmargin 0pt \textwidth 6.5in \textheight 8.5 in
\usepackage{longtable}
\usepackage{fancyhdr}
\usepackage{hyperref}
\fancyhead{}
\fancyhead[LE,RO]{\leftmark}

\newcommand{\clearemptydoublepage}{%
\newpage{\pagestyle{empty}\cleardoublepage}}

\begin{document}
'

/bin/echo "\def\cslversion{$(grep "value version =" ../../main/pcaml.ml | sed -e 's/^[^"]*"\([^"]*\).*$/\1/')}"

/bin/echo '
\pagestyle{empty}
\begin{center}
~\vfill
\Huge           Camlp5 - Reference Manual \\
\Large          version \cslversion \\[1cm]
\large          Daniel de Rauglaudre \\
                \today \\[1cm]
\vfill
\normalsize	Copyright \copyright\ 2007-2017 Institut National de
                Recherche en Informatique et Automatique

This document was generated by a shell script from the html documentation
pages.
\end{center}
\clearemptydoublepage
\setcounter{page}{1}
\pagestyle{plain}
\tableofcontents'

for i in $FILES; do
  /bin/echo '\clearemptydoublepage'
  if [ "$i" = "1" ]; then
    /bin/echo '\part{Parsing tools}'
  elif [ "$i" = "2" ]; then
    /bin/echo '\part{Printing tools}'
  elif [ "$i" = "3" ]; then
    /bin/echo '\part{Language extensions}'
  elif [ "$i" = "4" ]; then
    /bin/echo '\part{Appendix}'
    /bin/echo '\appendix{}'
  else
    cat $i |
    sed \
      -e 's/\\/\\backslash/g' \
      -e 's/%/\\%/g' \
      -e 's|<p [^>]*>|<p>|g' |
    sed \
      -e '{:b; /<\/p>$/N; s|<\/p>\n *|<\/p>|g; tb}' \
      -e 's|</p><p>|\n\n\\\n\n\\noindent |g' \
      -e '{s/^ *<p>/\\noindent /; s|</p>|\n\n|}' |
    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}' |
    sed \
      -e 's/{/\\{/g' -e 's/}/\\}/g' \
      -e '0,/<div id="content">/d' \
      -e '/<div id="tableofcontents">/,/<\/div>/d' \
      -e 's/^ *<h1[^>]*>/\\pagestyle{fancy}\\chapter{/' -e 's|</h1>|}|' \
      -e 's/^ *<h2>/\\section{/' -e 's|</h2>|}|' \
      -e 's/^ *<h3>/\\subsection{/' -e 's|</h3>|}|' \
      -e 's/^ *<h4>/\\subsubsection{/' -e 's|</h4>|}|' \
      -e 's/^ *<h5>/\\textbf{/' -e 's|</h5>|}|' \
      -e 's| *<table width[^>]*>\(.*\)</table>|\\begin{center}\n\\begin{tabular}\1\\hline\n\\end{tabular}\n\\end{center}|' \
      -e 's| *<table[^>]*>\(.*\)</table>|\\small\n\\begin{center}\n\\begin{longtable}\1\\hline\n\\end{longtable}\n\\end{center}\\normalsize|' \
      -e 's/<thead>//g' -e 's|</thead>||g' \
      -e 's/<tbody>//g' -e 's|</tbody>||g' \
      -e 's/<tr>\(<th[^>]*>.*<th[^>]*>.*<th[^>]*>\)/{|l|c|l|}\ntr\1/' \
      -e 's/<tr>\(<th[^>]*>.*<th[^>]*>\)/{|l|l|}\ntr\1/' \
      -e 's/tr<th/\\hline\n<th/' \
      -e 's|</th><th|} \& <th|g' \
      -e 's/<th[^>]*class="half"[^>]*>/\\makebox[5.5cm][l]{/g' \
      -e 's/<th[^>]*>/\\makebox[3cm][l]{/g' \
      -e 's|</th></tr>|} \\\\\n\\hline\n|g' \
      -e 's|<tr><td[^>]*>|{|g' \
      -e 's|</td><td[^>]*>|} \& {|g' \
      -e 's|</td></tr>|} \\\\\n|g' \
      -e 's|<br/>|\\\\|g' \
      -e 's/^ *<ul>/\\begin{itemize}/' -e 's|^ *</ul>|\\end{itemize}|' \
      -e 's/^ *<ol>/\\begin{enumerate}/' -e 's|^ *</ol>|\\end{enumerate}|' \
      -e 's/^ *<li>/\\item /' -e 's| *</li>||' \
      -e 's/^ *<dl><dd>/\\begin{description}\n\\item[\\ \\ \\ \\ \\ ]/' \
      -e 's|</dd></dl>|\\end{description}|' \
      -e 's/^ *<dl[^>]*>/\\begin{description}/' -e 's|</dl>|\\end{description}|' \
      -e 's/^ *<dt>/\\item[/' -e 's|</dt>|]|' \
      -e 's/^ *<dd>/\n\\\n\n/' -e 's|</dd>||' \
      -e 's/^ *<pre[^>]*>/\\begin{verbatim}/' -e 's|^ *</pre>|\\end{verbatim}|' \
      -e 's/<code>/\\texttt{/g' -e 's|</code>|}|g' \
      -e 's/<tt[^>]*>/\\texttt{/g' -e 's|</tt>|}|g' \
      -e 's/<em>/\\emph{/g' -e 's|</em>|}|g' \
      -e 's/<i>/\\emph{/g' -e 's|</i>|}|g' \
      -e 's/<b>/\\textbf{/g' -e 's|</b>|}|g' \
      -e 's/<a [^>]*>//g' -e 's|</a>||g' \
      -e 's/<span [^>]*>//g' -e 's|</span>||g' \
      -e 's/&lt;/</g' -e 's/&gt;/>/g' -e 's/&amp;/\\\&/g' -e 's/&nbsp;/\\ /g' \
      -e 's/_/\\_/g' \
      -e 's/#/\\#/g' \
      -e 's/~/\\~{}/g' \
      -e 's/\$/\\$/g' \
      -e 's/\^/\\^/g' \
      -e 's/\\backslash/$\\backslash$/g' \
      -e '/\\begin{verbatim}/,/\\end{verbatim}/s/\\_/_/g' \
      -e '/\\begin{verbatim}/,/\\end{verbatim}/s/\\#/#/g' \
      -e '/\\begin{verbatim}/,/\\end{verbatim}/s/$\\backslash\$/\\/g' \
      -e '/\\begin{verbatim}/,/\\end{verbatim}/s/\\\$/$/g' \
      -e '/\\begin{verbatim}/,/\\end{verbatim}/s/\\{/{/g' \
      -e '/\\begin{verbatim}/,/\\end{verbatim}/s/\\}/}/g' \
      -e '/\\begin{verbatim}/,/\\end{verbatim}/s/\\^/^/g' \
      -e '/<div class="trailer">/,$d' \
      -e 's/<div[^>]*>/\\noindent /' -e 's|</div>||' \
      -e "s|<version/>|$VERSION|" \
      -e '/<licence\/>/r ../../LICENSE' -e '/<licence\/>/d'
  fi
done

/bin/echo '\end{document}'
