#!/usr/bin/perl -w

use Getopt::Long;
my ($all, $pdf, $html, $man, $text) = (0,0,0,0,0);
GetOptions(all  => \$all,
	   pdf  => \$pdf,
	   html => \$html,
	   text => \$text,
	   man  => \$man,
	  );

($pdf, $html, $man, $text) = (1,1,1,1) unless ($pdf or $html or $man or $text);
($pdf, $html, $man, $text) = (1,1,1,1) if $all;

my $message = "\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-\n\n";
$message .= "Made the following document types:\n\n";

## what the heck are these anyway???
unlink qw(pod2htmd.tmp pod2htmi.tmp);

if ($html) {
  opendir D, "./" or die "could not read this directory\n";
  my @list = grep {/pod$/} readdir D;
  closedir D;

  print "making html from ";
  foreach (sort @list) {
    my @parts = split(/\./, $_);
    my $command =
      sprintf
	"pod2html --htmlroot=. --podpath=. --outfile=%s.html --infile=%s.pod",
	  $parts[0], $parts[0];
    print "$_\n                 ";
    system $command;
  };
  print $/;


  $message .= "   html pages   athena.html and athena_*.html\n";
};


my @sections = (qw(
  groups
  freeze
  import
  save
  plot
  plotstyles
  bkg
  editmenu
  groupmenu
  calibration
  deglitch
  truncate
  rebin
  smooth
  conv
  safluo
  deadtime
  align
  pixel
  merge
  diff
  lograt
  pca
  peakfit
  sum
  prefs
  bindings
  keys
  plugins
  tft
  help
  misc
		  ));


my $regex = join("|", sort(@sections));

if ($pdf or $man or $text) {

  print "Constructing monolithic pod file\n";
  open POD, ">athenadoc.pod";
  open TOP, "athena.pod";
  my $found_back = 0;
  while (<TOP>) {
    next if (/=head1 SECTIONS OF THE DOCUMENT/);
    next if (/=item Handling data/);
    next if (/=item Data operations/);
    next if (/=item Analysis/);
    next if (/=item Using Athena/);
    unless (/^L.*(athena_($regex))\b.*\|\|/) {
      print POD;
      next;
    };
    open SEC, "$1.pod";
    $found_back = 0;
    while (<SEC>) {
      #($found_back=1), next if (/=back/ and not $found_back);
      #next unless $found_back;
      last if (/=over 5/);
      print POD;
    };
    close SEC;
  };

  close TOP;
  close POD;
};

if ($pdf) {
  print "Converting pod to latex\n";
  use Pod::LaTeX;
  my $parser = Pod::LaTeX->new ( );
  $parser->TableOfContents(1);
  $parser->StartWithNewPage(1);
  my $preamble = "";
  map { $preamble .= $_; } (<DATA>);
  $parser->UserPreamble($preamble);
  $parser->parse_from_file ('athenadoc.pod', 'athenadoc.tex');


  print "Running latex thrice\n";
  system "pdflatex athenadoc.tex";
  system "pdflatex athenadoc.tex";
  system "pdflatex athenadoc.tex";
  rename "athenadoc.pdf", "athena.pdf";
  $message .= "   PDF          athena.pdf\n";
};

if ($man) {
  print "Making man page\n";
  use Pod::Man;
  $parser = Pod::Man->new(section=>1);
  $parser->parse_from_file('athenadoc.pod', 'athena.1');
  $message .= "   man page     athena.1\n";
};

if ($text) {
  print "Making plain text page\n";
  use Pod::Text;
  $parser = Pod::Text->new();
  $parser->parse_from_file('athenadoc.pod', 'athena.txt');
  $message .= "   plain text   athena.txt\n";
};

if ($pdf or $man or $text) {
  unlink "athenadoc.pod";
};

$message .= "\n";
print $message;


__DATA__
\documentclass{article}

\usepackage{makeidx}
\makeindex


\begin{document}

 \thispagestyle{empty}
%%   \begin{center}
%%     \includegraphics*[width=0.7\linewidth]{images/artemis_logo}
%%   \end{center}
  \vspace*{\stretch{1}}
  \noindent\rule{\linewidth}{1ex}
  \begin{flushleft}
    \textsf{%
      {\Huge U}{\huge sing} {\Huge A}{\huge THENA }}%
  \end{flushleft}
  \vspace{2ex}
  \begin{flushright}
    {\Large Bruce Ravel} \\
    {\large \texttt{bravel\char64anl.gov} \\
    \texttt{http://feff.phys.washington.edu/\char126ravel/software/exafs/} }
  \end{flushright}
  \noindent\rule{\linewidth}{1ex}
  \vspace*{\stretch{2}}

  \clearpage

\tableofcontents

\clearpage
