#!/usr/dist/bin/perl

if($#ARGV != 0)
{
  print "\n\n";
  print "Usage: makehandout filename.tex";
  print "\n\n";
  exit(-1);
}

if(!-e "@ARGV[0]")
{
  print "ERROR: file @ARGV[0] does not exist\n\n";
  exit(-1);
}

$filename=`basename @ARGV[0] .tex`;
chomp($filename);
$texname="$filename.tex";
$pdfname="$filename.pdf";

$handoutbasename="$filename-Handout";
$handouttexname="$filename-Handout.tex";
$handoutpdfname="$filename-Handout.pdf";

#Apparently, its enough to have this file in your tex path
#system("cp home/grad/cbourke/.latex/NoteSlide.pdf .");

print "+-------------------------------------------------+\n";
print "|                                                 |\n";
print "| Handout Builder: 2x3 Beamer Presentation        |\n";
print "|                  With Note Pages                |\n";
print "|                                                 |\n";
print "|  Progress                                       |\n";
print "|  =======================================        |\n";
print "|  Running sed script...\t";

system("sed -e 's/documentclass{beamer}/documentclass[handout]{beamer}/g' $texname > $handouttexname");

print "DONE              |\n";

print "|  First LaTeX Run...   \t";

# We run pdfLaTeX in nonstopmode, dumping any error messages
# into FirstTeX.output

$FirstRun = system("pdflatex --interaction nonstopmode $handouttexname > FirstTeX.output");

if($FirstRun != 0)
{
    print "\n\n";
    print "There was a Fatal pdfLaTeX Error:\n";
    print "--------------------------------------------------\n";
    system("cat FirstTeX.output"); #write out the log file to stdout
    print "--------------------------------------------------\n\n";
    #system("nedit $filename.bib &");
    #system("rm FirstTeX.output");
    exit(-1);
}
else
{
  print "DONE              |\n";
}

#we examine the successful output log to get the total number of pages
$PageLine = `more FirstTeX.output | grep pages`;
chomp($PageLine);
$PageLine=~m/\(([0-9]+) pages/;
$TotalNumberOfPages = $1;
system("rm FirstTeX.output");

print "|  Second LaTeX Run...  \t";
#system("pdflatex $texname > /dev/null");
  print "DONE              |\n";

#print "Building Handouts...\n";
#system("buildpdf $handouttexname");

print "|  Making TeX File...   \t";

$NoteSlideFile="\n
\\documentclass[landscape]{article}\n
\\usepackage{pdfpages} \n
\\begin{document} \n
\\includepdfmerge{";
		
for($i=1; $i<$TotalNumberOfPages; $i++)
{
  $NoteSlideFile="$NoteSlideFile $handoutpdfname, $i, \n";
  $NoteSlideFile="$NoteSlideFile NoteSlide.pdf, 1, \n";
}
  $NoteSlideFile="$NoteSlideFile $handoutpdfname, $TotalNumberOfPages, \n";
  $NoteSlideFile="$NoteSlideFile NoteSlide.pdf, 1} \n";

$NoteSlideFile="$NoteSlideFile\\end{document}\n";
#print "$NoteSlideFile";

open(INTERLEAVED, ">./temp001.tex") || die;
print INTERLEAVED "$NoteSlideFile";
close(INTERLEAVED);

print "DONE              |\n";
print "|  Final LaTeX Run...   \t";

system("pdflatex temp001.tex > /dev/null");
print "DONE              |\n";
print "|  Running pdfnup...    \t";

system("pdfnup --nup 1x1 --orient landscape --frame true --trim \"0cm 0cm 0cm 0cm\" --outfile temp.pdf temp001.pdf");
system("pdfnup --nup 2x3 --paper letter --pages all --orient portrait --frame false --trim \"-1cm -1cm -1cm -1cm\" --outfile $handoutpdfname temp.pdf");

print "DONE              |\n";
print "|  Cleaning Up...       \t";

system("cleanlatex $filename");
system("rm temp001.pdf");
system("rm temp001.tex");
system("rm temp.pdf");
#system("rm $handouttexname");
print "DONE              |\n";
print "|                                                 |\n";
print "+-------------------------------------------------+\n";



