#!/usr/bin/perl
#
# This utility script modifies LaTeX source files (*.tex) generated by doxygen
# for i) generating a more optimized Developers Manual and ii) working around
# an apparent bug in latex (or incompatbility between doxygen and latex) when
# generating the reference manual.
#
# The script contains logic to prevent multiple applications of the desired
# modifications.  This allows it to be used when the Developers/Reference files
# are in a mixed state (some modified, some unmodified).

################################
# Reference Manual: refman.tex #
################################
if ($ARGV[0] eq "--reference") {

  # JAS: This stuff no longer needed by new ref manual?
  if (0) {

    $texfile = "latex-ref/refman.tex";
    $newtexfile = $texfile . "_";
    open (INPUT,  "<$texfile")    || die "cannot open original file $texfile\n$!";
    open (OUTPUT, ">$newtexfile") || die "cannot open new file $newtexfile\n$!";
    print "Processing $texfile\n";

    # read input file until EOF
    while (<INPUT>) { # read each line of file
    # These no longer match, so comment out
    #  if (/^\\(chapter{Dakota Directory Hierarchy}|input{dirs}|chapter{Dakota Hierarchical Index}|input\{hierarchy}|chapter{Dakota File Index}|input\{files}|chapter{Dakota Page Index}|input\{pages}|chapter{Dakota Directory Documentation}|input{dir_000000}|chapter{Dakota Page Documentation}|printindex)/) {
      if (/^\\(chapter{File Index}|input\{files}|printindex)/) {
	print OUTPUT "%$_";
      }
      elsif (/^\\include\{(\w*Commands|Bibliography)}/) {
	s/\\include\{/\\input\{/;
	print OUTPUT;
      }
      else {
	print OUTPUT;
      }
    }

    # close both files
    close (INPUT);
    close (OUTPUT);

    # Replace original file with new file
    rename $newtexfile, $texfile;

    #################################################
    # Reference Manual: Commands/Bibliography files #
    #################################################
    @texfiles = (<latex-ref/*Commands.tex>, "latex-ref/Bibliography.tex");

    # for each LaTeX Commands file in latex-ref, perform modifications
    foreach $texfile (@texfiles) {

      open (INPUT, "$texfile") || die "cannot open original file $texfile\n$!";
      $_ = <INPUT>;
      if ( /\\chapter/ ) {
	print "$texfile already processed\n";
	close (INPUT);
      }
      else {
	print "Processing $texfile\n";
	seek INPUT, 0, 0; # rewind to beginning
	$newtexfile = $texfile . "_";
	open (OUTPUT, ">$newtexfile") || die "cannot open new file $newtexfile\n$!";

	# read input file until EOF
	while (<INPUT>) { # read each line of file

	  # Promote sectioning macros up one level:
	  # \section       -> \chapter
	  # \subsection    -> \section
	  # \subsubsection -> \subsection

	  # With Doxygen-1.6.1 (maybe 1.5.1) and newer, no longer need
	  #s/\\section/\\chapter/go;
	  #s/\\subsection/\\section/go;
	  #s/\\subsubsection/\\subsection/go;

	  # Change table declarations
	  s/table\}\[h\]/table}[htp!]/g;

	  print OUTPUT;
	}

	# close both files
	close (INPUT);
	close (OUTPUT);

	# Replace original file with new file
	rename $newtexfile, $texfile;
      }
    }
  } # JAS
  #####################################################
  # Reference Manual: Workaround for apparent p{} bug #
  #####################################################
  # In keywords.tex, doxygen writes tables using an
  # arithmetic argument to p{}. latex is unable to 
  # correctly parse them. The workaround used here is
  # to extract the arithmetic argument calculate it 
  # once at the top of the file and save the result 
  # in a variable (\kludgelength), then substitute 
  # the variable.
  $texfile = "latex-ref/keywords.tex";
  $newtexfile = $texfile . "_";

  # BMA: This workaround may not be needed with newer Doxygen/LaTeX
  if (-e $texfile) {
    open (INPUT,'<',"$texfile")    || die "cannot open original file $texfile\n$!";
    print "Processing $texfile\n";
    # Find the first example of the arithmetic expression,
    # then rewind the file
    while (my $line = <INPUT>) {
      if ($line  =~ /p\{(.*?)\}/) {
        $arwidth = $1;
        last;
      }
    }
    # Make sure keywords.tex hasn't already been modified. This is indicated
    # by the presence of \kludgelength in the file.
    if( $arwidth ne "\\kludgelength") {
      seek(INPUT,0,0); 
      # Write out header to replacement keywords.tex file
      open (OUTPUT,'>', "$newtexfile") || die "cannot open new file $newtexfile\n$!";
      print OUTPUT "\\newlength{\\kludgelength}\n";
      print OUTPUT "\\setlength{\\kludgelength}\n    {$arwidth}\n";
      while ($line = <INPUT>) {
  	  $line =~ s/p\{\Q$arwidth\E\}/p{\\kludgelength}/;
  	  print OUTPUT $line;
      }
  
      close(INPUT);
      close(OUTPUT);
      rename $newtexfile, $texfile;
    } else {
      print "..keywords.tex already modified. Skipping.\n";
      close(INPUT);
    }
  }

} elsif ($ARGV[0] eq "--developers") {
  #################################
  # Developers Manual: refman.tex #
  #################################
  $texfile = "latex-dev/refman.tex";
  $newtexfile = $texfile . "_";
  open (INPUT,  "<$texfile")    || die "cannot open original file $texfile\n$!";
  open (OUTPUT, ">$newtexfile") || die "cannot open new file $newtexfile\n$!";
  print "Processing $texfile\n";

  # read input file until EOF
  while (<INPUT>) { # read each line of file
    if (/^\\(chapter{Todo List}|label{todo}|hypertarget{todo}{}|include{todo})/) {
      print OUTPUT "%$_";
    }
    elsif (/^\\include\{(DakLibrary|FnEvals|IteratorFlow|StyleConventions|SpecChange|VarContainersViews)}/) {
      s/\\include\{/\\input\{/;
      print OUTPUT;
    }
    else {
      print OUTPUT;
    }
  }

  # close both files
  close (INPUT);
  close (OUTPUT);

  # Replace original file with new file
  rename $newtexfile, $texfile;

  ######################################
  # Developers Manual: extra dox files #
  ######################################
  @texfiles = ("latex-dev/DakLibrary.tex",  "latex-dev/FnEvals.tex",
	       "latex-dev/IteratorFlow.tex", "latex-dev/SpecChange.tex",
	       "latex-dev/StyleConventions.tex", 
	       "latex-dev/VarContainersViews.tex");

  # for each LaTeX special topics file in latex-dev, perform modifications
  foreach $texfile (@texfiles) {

    open (INPUT, "$texfile") || die "cannot open original file $texfile\n$!";
    $_ = <INPUT>;
    if ( /\\chapter/ ) {
      print "$texfile already processed\n";
      close (INPUT);
    }
    else {
      print "Processing $texfile\n";
      seek INPUT, 0, 0; # rewind to beginning
      $newtexfile = $texfile . "_";
      open (OUTPUT, ">$newtexfile") || die "cannot open new file $newtexfile\n$!";

      # read input file until EOF
      while (<INPUT>) { # read each line of file

	# Promote sectioning macros up one level:
	# \section       -> \chapter
	# \subsection    -> \section
	# \subsubsection -> \subsection

	# With Doxygen-1.6.1 (maybe 1.5.1) and newer, no longer need
	#s/\\section/\\chapter/go;
	#s/\\subsection/\\section/go;
	#s/\\subsubsection/\\subsection/go;

	print OUTPUT;
      }

      # close both files
      close (INPUT);
      close (OUTPUT);

      # Replace original file with new file
      rename $newtexfile, $texfile;
    }
  }
} 
print "LaTeX modification Script (for $ARGV[0]) Complete.\n";

