#!/usr/bin/perl -w

###########################################################################
# $Id: //dev/server/docs/user_docs/DevGuide_docs/printgen/printgen.pl#1 $
#
# The printgen script takes a series of HTML pages and merges them into a
# single, printable page. Steps to making this work:
#
# 1. Each page needs to be tagged with two comments: <!-- #StartPrint -->
#    and <!-- #EndPrint -->. Any text appearing between these two tags will
#    be included. Please make sure this is the body text only, and none of
#    the individual page's header | footer | navigation blocks appear
#    within these tags.
# 2. Create header and footer pages which will be used by the generated page.
#    These should be print-friendly, e.g. not navigation menus or other
#    screen-specific oddities. Use the provided footer.html and header.html.
# 3. Create/modify a Makefile to invoke the script. This includes such
#    arguments as the list of pages to include and their order, location
#    of the header, footer, and generated pages. 
# 4. Work with Release Engineering as needed to ensure that the build system 
#    surrounding this Makefile excludes internal files (this script's 
#    directory, the Makefile itself) from the build. This is also called out 
#    in the Makefile itself.
# 4. As an example, when done with the above steps for a new doc, the
#    directory tree should look something like this:
#    <doc's top-level directory, dev_guide for example>
#    /pages (source files)
#    /printgen (this script, the header/footer to include)
#    /image, /css, /icons and other directories
#    Makefile (to drive the script)
#    index.html (frameset page to load the real starting page from the
#    pages directory (usually a file named default.htm).
#
# @author Marios Leventopoulos <mleventopoulos@rsasecurity.com>
# @since ClearTrust 5.5
###########################################################################

use strict;

my %option;			# track command line arguments/options
use Getopt::Std;
getopts("o:h:f:", \%option);

# if not set, set output to "-". opening "-" for writing means STDOUT

$option{'o'} = "-" unless defined $option{'o'};


open (STDOUT, "> $option{'o'}")
  or die "Error: $option{'o'}: $!\n";


# If a header file is given, print it as is
if (my $file = $option{'h'}) {
  open (FD, $file) or die "Error: $file: $!\n";
  local $/ = undef;		# read all lines in one go

  my $file_text = <FD>;
  print $file_text;
  close FD;
}

# The remaining arguments are expected to be the input files
for my $file (@ARGV) {
  open (FD, $file) or die "Error: $file: $!\n";

  local $/ = undef;		# read all lines in one go

  my $file_text = <FD>;
  #  ($file_body) = ($file_text =~ m,<\s*[Bb][Oo][Dd][Yy][^>]*>(.*)</[Bb][Oo][Dd][Yy]>,s);
  # <!-- #EndPrint -->
  # <!-- #StartPrint -->
  my $tags_found = 0;
  while ($file_text =~ m,<!--\s*\#StartPrint\s*-->(.*?)<!--\s*\#EndPrint\s*-->,sg) {
    my $file_body = $1;
    $tags_found++;
    if (defined $file_body) {
      print "<!-- Start of Print content from $file, part $tags_found -->\n";
      print $file_body;
      print "<!-- End of Print content from $file, part $tags_found -->\n";
    }
  }

  warn "Warning: No <!-- #StartPrint --> tag found in $file. Skipping...\n"
    unless $tags_found;
  close FD;
}

# If a footer file is given, print it as is at the end
if (my $file = $option{'f'}) {
  open (FD, $file) or die "Error: $file: $!\n";
  local $/ = undef;		# read all lines in one go

  my $file_text = <FD>;
  print $file_text;
  close FD;
}
