#!/usr/bin/perl

sub usage {
   print "Usage: printify [file1 file2 ...]\n";
}

sub find_files {
   local @files;

   if (-s "intro.html") {
      push @files, "intro.html";
   }

   local $i = 0;

   while (-s "exercise$i.html") {
      push @files, "exercise$i.html";
      $i++;
   }

   if (-s "summary.html") {
      push @files, "summary.html";
   }

   return @files;
}

sub check_files {
   local @files = @_;
   local $ok = 1;

   foreach $file (@files) {
      local $file_ok = 1;

      @grep = `grep "<!-- MAIN CONTENT -->" $file`;

      if (@grep < 1) {
         print "Problem with $file -- begin content tag is missing.\n";
         print "Looking for alternate tag instead.\n";

	 @grep = `grep "<!-- BEGIN Content -->" $file`;

         if (@grep < 2) {
            $file_ok = 0;
            print "Cannot print $file -- begin content tag is missing.\n";
         }
         elsif (@grep > 2) {
            $file_ok = 0;
            print "Cannot print $file -- begin content tag is not unique\n";
         }
         else {
            print "OK\n";
         }
      }
      elsif (@grep > 1) {
         $file_ok = 0;
         print "Cannot print $file -- begin content tag is not unique\n";
      }
      else {
         $use_main_begin{$file} = 1;
      }

      if ($file_ok) {
	 @grep = `grep "<!-- END Content -->" $file`;

         if (@grep < 1) {
            print "Problem with $file -- end content tag is missing.\n";
            print "Looking for alternate end content tag instead.\n";

            @grep = `grep "<!-- END OF MAIN CONTENT -->" $file`;

            if (@grep < 1) {
               $file_ok = 0;
               print "Cannot print $file -- end content tag is missing.\n";
            }
            elsif (@grep > 1) {
               $file_ok = 0;
               print "Cannot print $file -- end content tag is not unique\n";
            }
            else {
               print "OK\n";
               $use_main_end{$file} = 1;
            }
         }
         elsif (@grep > 1) {
            $file_ok = 0;
            print "Cannot print $file -- end content tag is not unique\n";
         }
      }

      $ok &= $file_ok;
   }

   if (!$ok) {
      print <<EOT;
Unable to find the <!-- BEGIN Content -->/<!-- END Content --> or
<!-- MAIN CONTENT -->/<!-- END OF MAIN CONTENT --> tags in $file.
Could not create printable.html file.
EOT
      exit 10;
   }
   else {
      print "Verified that all files have suitable content tags\n";
   }
}

sub extract_header {
   local $header = "";
   local $file;

   foreach $file (@_) {
      open IN, "<$file" || die "Could not open $file for reading\n";

      while (<IN>) {
         if (!$header && /BEGIN PAGETITLE/) {
            $header = $_;
         } elsif ($header) {
            $header = "$header$_";

            if (/END L1 COMPONENT V.1/) {
               print "Using header from $file.\n";
               close IN;
               return $header;
            }
         }
      }

      print "No header found in $file.\n";
      close IN;
   }

   print "Could not find a recognizable header section in any file.\n";
   exit 11;
}

sub extract_title {
   if ($_[0] =~ /<\s*h1\s+class\s*=\s*"?j1holtitle"?\s*>\s*(lab-\d{4})\s*:\s*(.+?)\s*<\s*\/h1\s*>/i) {
      print <<EOT;
Using "$1" as lab number.
Using "$2" as lab name.
EOT
      return ($1, $2);
   }
   else {
      print "Could not extract lab number and name from header.\n";
      exit 12;
   }
}

sub get_content {
   local $content = "";
   local $content_begin = $use_main_begin{$_[0]} ? "<!-- MAIN CONTENT -->" : "<!-- BEGIN Content -->";
   local $content_end = $use_main_end{$_[0]} ? "<!-- END OF MAIN CONTENT -->" : "<!-- END Content -->";

   print "Adding content between $content_begin and $content_end.\n";

   open IN, "<$file" || die "Could not open $file for reading\n";

   while (<IN>) {
      if (!$content && /$content_begin/) {
         $content = $_;
      } elsif ($content) {
         $content = "$content$_";

         if (/$content_end/) {
            last;
         }
      }
   }

   close IN;
   return $content;
}

if (grep {/^-{1,2}h(elp)?$/i} @ARGV) {
   usage();
   exit;
}

my @files;

# If we're given a list of files, use them
if (@ARGV) {
   @files = @ARGV;
}
# If not, go figure out which files exist
else {
   @files = find_files();
}

# Make sure all files have required tags
check_files(@files);

# Extract header information from one file
$header = extract_header(@files);

# Extract title from header
($number, $name) = extract_title($header);

# Create printable page
open OUT, ">printable.html" || die "Could not open printable.html for writing\n";

# Print header
print "Writing page header.\n";
print OUT <<EOH;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html class="regenabled gecko radius jsenabled regloaded" lang="en-US">
  <head>
    <title>$number: $name</title>

    <!-- BEGIN METADATA -->
    <meta http-equiv="content-language" content="en">
    <meta name="collection" content="reference">
    <meta name="keywords" content="">
    <meta name="description" content="Java SE Technologies at a Glance">
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <meta name="archive" content="FALSE">
    <meta name="robots" content="index,follow">
    <meta name="date" content="2008-08-12">
    <link rel="stylesheet" href="css/handsonlabs.css">
    <link rel="stylesheet" href="css/default_developer.css">
    <link rel="stylesheet" href="css/handsonlabs_print.css">
    <!-- END METADATA -->
  </head>
  <body>
EOH

# Print cover page
print "Writing cover page.\n";
print OUT <<EOH;
    <div class="j1holpagebreak">
      <table class="j1coverpage">
        <tbody>
          <tr class="j1coverpage">
            <td>
              <div class="pagetitle">
                <h1>JavaOne Hands-On Lab</h1>
                <h2 id="j1holcovernumberid">$name</h2>
                <h2 id="j1holcovertitleid">$number</h2>
                <h3 style="margin-top: 50px;">Copyright 2009 Sun Microsystems</h3>
              </div>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    <!-- Blank page for back of cover sheet -->
    <div class="j1holpagebreak">&nbsp;</div>
$header
EOH

# Extract and print contents from printable pages
foreach $file (@files) {
   print "Writing content from $file.\n";
   $content = get_content($file);
   print OUT <<EOH;
<div class="j1holpagebreak">
$content
</div>
EOH
# <!-- =================== -->
}

# Print footer
print "Writing page footer.\n";
print OUT <<EOH;
      <!-- BEGIN VNV5 FOOTER  -->
      <div class="j1holnoprint"><table border="0" cellpadding="0" cellspacing="10" width="100%">
        <tbody><tr>
            <td>
              <table class="vatop" border="0" cellpadding="0" cellspacing="0" width="100%">
                <tbody><tr>
                  <td colspan="4" class="grey3" valign="top"><img src="im/a.gif" alt="" border="0" height="2" width="1"></td></tr>
                  <tr>
                    <td><img src="im/a.gif" alt="" border="0" height="1" width="190"><br>
                    <a href="http://www.sun.com/"><img src="im/logo_sun_small_sdn.gif" alt="" border="0" height="29" vspace="5" width="61"></a></td>
                    <td valign="top" width="100%"><img src="im/a.gif" alt="" border="0" height="1" width="350"><br>
                      <div class="footer">
                        <a href="http://developers.sun.com/global/aboutsun.html">About Sun</a> &nbsp;|&nbsp;
                        <a href="http://developers.sun.com/global/aboutsdn.html">About This Site</a> &nbsp;|&nbsp;
                        <a href="http://developers.sun.com/global/newsletters.html">Newsletters</a> &nbsp;|&nbsp;
                        <a href="http://developers.sun.com/global/contact.html">Contact Us</a> &nbsp;|&nbsp;
                        <a href="http://developers.sun.com/global/employment.html">Employment</a><br>
                        <a href="http://developers.sun.com/global/howtobuy.html">How to Buy</a> &nbsp;|&nbsp;
                        <a href="http://developers.sun.com/global/licensing.html">Licensing</a> &nbsp;|&nbsp;
                        <a href="http://developers.sun.com/global/termsofuse.html">Terms of Use</a> &nbsp;|&nbsp;
                        <a href="http://developers.sun.com/global/privacy.html">Privacy</a> &nbsp;|&nbsp;
                        <a href="http://developers.sun.com/global/trademarks.html">Trademarks</a>
                        <br><span class="sp10">&nbsp;</span><br>
                        <br><span class="sp10">&nbsp;</span><br>
                        Copyright <span id="copyDate" class="cssDate">1994-2009 </span> Sun Microsystems, Inc.
                    </div></td>
                    <td><img src="im/a.gif" alt="" border="0" height="1" width="40"></td>
                    <td valign="top"><div class="footer"><b><a href="http://developers.sun.com/global/aboutsdn.html">A Sun Developer Network Site</a></b></div>
                      <div class="footer">
                        <img src="im/a.gif" alt="" border="0" height="1" width="170"><br>
                        Unless otherwise licensed, code in all technical manuals herein (including articles, FAQs, samples) is provided under this <a href="http://developers.sun.com/global/berkeley_license.html">License</a>.
                        <br><span class="sp5">&nbsp;</span><br>
                        <a href="http://developers.sun.com/global/rss_sdn.html"><img src="im/ic_feed_16x.gif" alt="XML" align="top" border="0" height="16" width="16"></a>&nbsp;<a href="http://developers.sun.com/global/content_feeds.html">Sun Developer RSS Feeds</a>
                  </div></td></tr>
                  <tr><td colspan="4" class="grey3" valign="top"><img src="im/a.gif" alt="" border="0" height="2" width="1"></td></tr>
              </tbody></table>
          </td></tr>
      </tbody>
    </table></div>
  </body>
</html>
      <!-- END VNV5 FOOTER -->
EOH

close OUT;
print "Done.\n";


