#!/usr/local/bin/perl

$, = ' ';		# set output field separator
$\ = "\n";		# set output record separator

@files = @ARGV;

print <<'EOD';
\documentstyle[twoside]{report}
\raggedbottom

\pagestyle{headings}

\begin{document}

\appendix

\chapter{Subroutine Descriptions}

\section{Introduction}

This appendix includes a list of all the PGPLOT subroutines,
and then gives detailed instructions for the use of each routine in
Fortran programs. The subroutine descriptions are in alphabetical order.

\section{Arguments}

The subroutine descriptions indicate the data type of each
argument. When arguments are described as ``input'', they may be
replaced with constants or expressions in the {\tt CALL}
statement, but make sure that the constant or expression has the
correct data type.

\begin{description}

\item[{\tt INTEGER} arguments]
should be declared
{\tt INTEGER} or {\tt INTEGER*4} in the calling program,
not {\tt INTEGER*2}.

\item[{\tt REAL} arguments]
should be declared
{\tt REAL} or {\tt REAL*4} in the calling program, not
{\tt REAL*8} or {\tt DOUBLE PRECISION}.

\item[{\tt LOGICAL} arguments]
these should be declared
{\tt LOGICAL} or {\tt LOGICAL*4} in the calling program.

\item[{\tt CHARACTER} arguments]
may be any valid Fortran
{\tt CHARACTER} variable (declared
{\tt CHARACTER*n} for some integer {\tt n}).

\end{description}

\section{Index of Routines}

EOD

# Extract documentation from pgplot source code: output index of routines
print '\begin{description}';
while (<>) {
    chop;	# strip record separator
    if (/^C\*/) {
      ($module, $rest) = split (' ', $_, 2);
      $module = substr($module, 2);
      print "\\item[$module] $rest";
      $ref{$module} = "<A href=\"#$module\">$module</A>";
      push (@modules, $module);
    }
}

# reverse sort so that modules with the same first few characters occur
# longest to shortest.
@modules = sort {length($b) <=> length($a)} @modules;

print '\end{description}';
print ' ';

print '{\small';
print '\hrule';

# Extract documentation from pgplot source code: output LaTeX code

@ARGV = @files;
while (<>)
{
  /^C\*/ && do
  {
    print '';

    print ''; chop;
    ($module, $rest) = split (' ', substr($_, 2), 2);
    print "\\subsection*{$module $rest \}";
    next;
  };

  /^C\+/ && do
  {
    print '\begin{verbatim}' if $echo == 0;
    $echo = 1;
    print &Getline0();
    next;
  };

  /^C--/ && do
  {
    print '\end{verbatim}' if $echo == 1;
    print '\hrule' if $echo == 1;
    $echo = 0;
    next;
  };

  next if ! $echo;

  /^C/ && do
  {
    chop;
    print substr($_, 2) if $echo == 1;
    next;
  };

  chop; print;
}

print <<'EOD';
}
\end{document}
EOD


sub Getline0 {
    if ($getline_ok = (($_ = <>) ne '')) {
	chop;	# strip record separator
    }
    $_;
}
