#!/usr/bin/perl -w
use vars qw($d);

@ARGV=('simplot.doc','program.doc','xyplot.doc',glob('pl*.doc'));
open(OUT,">manual.tex") or die "could not open $file.html\n";
print OUT <<'EOF';
\documentclass[a4page,english,twoside]{article}
\usepackage{xspace,paralist,multicol,fancyhdr,longtable,txfonts,booktabs,colortbl,graphicx,hyperref}

\usepackage[
   pdftex,
   papersize={210mm,297mm},
   margin=20mm,
   twoside 
]{geometry}

\definecolor{pink}{rgb}{1,.8,.8}
\definecolor{lightblue}{rgb}{.8,.8,1}
\newcommand{\pink}[1]{\multicolumn{1}{>{\columncolor{pink}}l}{#1}}
\newcommand{\lightblue}[1]{\multicolumn{1}{>{\columncolor{lightblue}}l}{#1}}
\author{Wybo Dekker\\\texttt{wybo@dekkerdocumenten.nl}}
\title{{\Huge simplot} -- a plot package for C\\including an interpreter}
\hypersetup{a4paper,colorlinks=true}
\pagestyle{fancy}
\fancyhead[RE,LO]{\textsl{SIMPLOT -- THE MANUAL}}
\fancyhead[RO,LE]{\textsl{\leftmark}}

\def\NN{\tabularnewline}
\def\FL{\toprule}
\def\ML{\NN\midrule}
\def\LL{\NN\bottomrule}

\parskip2ex
\parindent0pt
\begin{document}
\pagenumbering{alph}\setcounter{page}{0}
\maketitle
\newpage
\mbox{}\thispagestyle{empty}
\newpage
\pagenumbering{arabic}\setcounter{page}{3}
\setcounter{secnumdepth}{0}
\setcounter{tocdepth}{1}
\columnsep=10mm
\begin{multicols}{3}
\parskip-1ex
\columnseprule=.2pt
\tableofcontents
\end{multicols}
\newpage
EOF

$intab=$name=$verbatim=0;
while(<>) {
  chomp;
  replace() unless $verbatim;

  s|^UL$|\\begin{compactitem}|;		# unordered list
  s|^ENDUL$|\\end{compactitem}|;
  s|^OL$|\\begin{compactenum}|;		# ordered list
  s|^ENDOL$|\\end{compactenum}|;
  s|^DL$|\\begin{compactdesc}|;		# description list
  s|^ENDDL$|\\end{compactdesc}|;

  /^PAGE/ and do {
    print OUT "\\newpage\n";
  },next;
  /^$/ and do {
  },next;
  /^NAME/ and do {
    s|^NAME\s+||;
    print OUT "\\def\\this{\\textbf{$_}\\xspace}\n";
    print OUT "\\newpage\\section[$_]{\\hypertarget{$_}{$_} - ";
    $name=$_;
  },next;
  /^SHORT/ and do {
    s|^SHORT\s+||;
    print OUT "$_}\n";
  },next;
  /^TYPE/ and do { 
    ($d,$return)=split;
  },next;
  /^PNG / and do {
    s|^PNG (.*)|\n\n\\includegraphics[scale=.8]{graphics/$1}\n\n|;
    print OUT;
  },next;
  /^FILE / and do {
    s|^FILE ||;
    verbatim("$_");
  },next;
  /^ARG/ and do {
    $intab or ptabhead();
    $intab=1;
    s|^ARG\s*||;
    $line=join('&',split(/\s+/,$_,4));
    print OUT "$line\\NN\n";
  },next;
  /^HEAD\s+(.*)/ and do {
    $intab and ptabtail();
    $intab=0;
    print OUT "\\subsection{$1}\n";
  },next;  
  /^PAR$/ and do {
    print OUT "\n\n";
  },next;
  /^TAB (.*)/ and do {
    print OUT "\n\n\\begin{longtable}{$1}\n";
    while (<>) {
      last if /^ENDTAB/;
      replace();
      if (s/\\b$//) {	# blue lines end in \b
        s/^/\\rowcolor{lightblue}/;
	s/\|/\&/g;
      } else {		# blue cells start with \b:
        s/\|\\b (\w+)/|\\lightblue{$1}/g;	# |\b -> <td bgcolor=lightblue>
        s/^\\b (\w+)/\\lightblue{$1}/g;	# \b -> <td bgcolor=lightblue> (at start of line)
	s/\|/\&/g;				# |   -> <td>
      }
      s|$|\\NN|;
      print OUT;
    }
    print OUT "\\end{longtable}\n\n";
  },next;
  /^VERB/ and do {
    print OUT "\\begin{verbatim}\n";
    $verbatim=1;
  },next;
  /^ENDVERB/ and do {
    print OUT "\\end{verbatim}\n";
    $verbatim=0;
  },next;
  /^USAGE/ and do {
    s|^USAGE ||;
    print OUT "\\subsection{USAGE}$_\n\\subsection{DESCRIPTION}\n";
  },next;

  print OUT "$_\n";
}
print OUT "\\end{document}\n";
close OUT;
system("pdflatex -interaction=batchmode manual; pdflatex -interaction=batchmode manual");

sub ptabhead {
  print OUT "\n\n\\begin{tabular}{lllll}\\FL\n".
            "\\rowcolor{lightblue}parameter&type&units&description\\ML\n";
}
sub ptabtail {
  print OUT "\\pink{returns:}&$return&&\\LL\n".
            "\\end{tabular}\n\n";
}
sub verbatim {
  $file=shift;
  open(FILE,$file) or die "$file not found\n";
  print OUT "\\begin{verbatim}\n";
  while(<FILE>) {
    next if /#!/;
    print OUT;
  }
  print OUT "\\end{verbatim}\n";
  close FILE;
}
sub replace {
  s/\\n /\\$&/g;
  s/\\lt /\$<\$ /g;
  s/\\gt /\$>\$ /g;
  s/\\le /\$<=\$ /g;
  s/\\ge /\$>=\$ /g;
  s/\\arg\s+([A-Za-z0-9,]+)/\\textbf{$1}/g;
  s/\\arg( -[A-Za-z0-9])/\\textbf{$1}/;
  s/\\arg( \[0-9.-\])/\\textbf{$1}/;
  s/\\rout\s+(\w+)/\\hyperlink{$1}{$1}/g;
  s/\^(\d+)/\\textsuperscript{$1}/g;
  s/\\dt\s+(\S+)/\\item\[$1]/ and do { s/([^_])_/$1 /g; s/ _/_{} /g };
  s/\\it\s+(\S+)/\\emph{$1}/g;
  s/\\tt\s+(\S+)/\\texttt{$1}/g;
  s/([_#\$%&])/\\$1/g;
  s/\^/\\^{}/g;
}
