#!/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";

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->UserPostamble('end{document}');
  $parser->parse_from_file ('hephaestus.pod', 'hephaestus.tex');
  print "Running latex thrice\n";
  system "pdflatex hephaestus.tex";
  system "pdflatex hephaestus.tex";
  system "pdflatex hephaestus.tex";
  $message .= "   PDF          hephaestus.pdf\n";
};


if ($html) {
  print "Making html page\n";
  use Pod::Html;
  system("pod2html --htmlroot=. --podpath=. --infile=hephaestus.pod --outfile=hephaestus.html");
  $message .= "   html page    hephaestus.html\n";
};

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

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


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



__DATA__
\documentclass{article}

\usepackage{makeidx}
\makeindex


\begin{document}

 \thispagestyle{empty}
%%   \begin{center}
%%     \includegraphics*[width=0.7\linewidth]{images/hephaestus_logo}
%%   \end{center}
  \vspace*{\stretch{1}}
  \noindent\rule{\linewidth}{1ex}
  \begin{flushleft}
    \textsf{%
      {\Huge U}{\huge sing} {\Huge H}{\huge EPHAESTUS }}%
  \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
