import java.io.*;
import java.text.*;
import java.util.*;

/**
   This program imitates the slide-making capability of the "classic" HTML Tidy.
   It creates slides from an HTML file, split along <h2> boundaries.
*/
public class SlideMaker
{
   public static void main(String[] args)
   {
      if (args.length == 0) usage();
      try
      {
         BufferedReader in = new BufferedReader(new FileReader(args[0]));
         SlideMaker maker = new SlideMaker(in);
         maker.process();
      }
      catch (IOException ex)
      {
         ex.printStackTrace();
      }
   }

   public static void usage()
   {
      System.err.println("usage: SlideMaker htmlFile");
   }

   public SlideMaker(BufferedReader in)
   {
      this.in = in;
   }

   public void process() throws IOException
   {
      htmlPrefix = readUntil(startOfBody);
      readUntil(startOfSlide); // skip <h1>
      while (!isLast)
      {
         StringBuffer slide = readUntil(startOfSlide);
         emit(slide);
      }
   }

   private StringBuffer readUntil(String tag) throws IOException
   {
      StringBuffer buffer = new StringBuffer();
      if (nextLine != null) 
      {
         buffer.append(nextLine);
         buffer.append("\n");
      }
      while ((nextLine = in.readLine()) != null)
      {
         if (nextLine.toLowerCase().indexOf(endOfBody) >= 0)
         {
            isLast = true;
            return buffer;
         }
         if (nextLine.toLowerCase().indexOf(tag) >= 0)
            return buffer;
         buffer.append(nextLine);
         buffer.append("\n");
      }
      return buffer;
   }

   private void emit(StringBuffer slide) throws IOException
   {
      counter++;
      String fileName = fileNamePrefix + pad(counter) + fileNameSuffix;
      PrintWriter out = new PrintWriter(new FileWriter(fileName));
      try
      {
         out.println(htmlPrefix);
         emitHeader(out);
         out.println(slide);
         emitFooter(out);
         out.println(htmlSuffix);
      }
      finally
      {
         out.close();
      }      
   }

   private void emitHeader(PrintWriter out)
   {
      if (counter == 1)
         out.println(MessageFormat.format(firstSlideHeader, new Object[] {
            fileNamePrefix, pad(2) }));
      else if (isLast)
         out.println(MessageFormat.format(lastSlideHeader, new Object[] {
            fileNamePrefix, pad(counter - 1), pad(1) }));     
      else
         out.println(MessageFormat.format(slideHeader, new Object[] {
            fileNamePrefix, pad(counter - 1), pad(counter + 1), pad(1) }));
   }

   private void emitFooter(PrintWriter out)
   {
      if (counter == 1)
         out.println(MessageFormat.format(firstSlideFooter, new Object[] {
            fileNamePrefix, pad(2) }));
      else if (isLast)
         out.println(MessageFormat.format(lastSlideFooter, new Object[] {
            fileNamePrefix, pad(counter - 1), pad(1) }));     
      else
         out.println(MessageFormat.format(slideFooter, new Object[] {
            fileNamePrefix, pad(counter - 1), pad(counter + 1), pad(1) }));
   }

   private String pad(int n)
   {
      String r = "" + n;
      while (r.length() < digits) r = "0" + r;
      return r;
   }

   private BufferedReader in;
   private String nextLine;

   private String fileNamePrefix = "slide";
   private String fileNameSuffix = ".html";
   private int digits = 3;
   private String firstSlideHeader = "<body>\n"
      + "<div onclick=\"document.location=''{0}{1}.html''\">\n"
      + "<center><small>"
      + "<a href=\"{0}{1}.html\">next</a></small></center>\n"
      + "<hr></div>";
   private String firstSlideFooter = 
        "<div onclick=\"document.location=''{0}{1}.html''\"><hr>\n"
      + "<center><small>"
      + "<a href=\"{0}{1}.html\">next</a></small></center>\n"
      + "</div>";
   private String slideHeader = "<body>\n"
      + "<div onclick=\"document.location=''{0}{2}.html''\">\n"
      + "<center><small><a href=\"{0}{1}.html\">previous</a> | \n"
      + "<a href=\"{0}{3}.html\">start</a> | \n"
      + "<a href=\"{0}{2}.html\">next</a></small></center>\n"
      + "<hr></div>";
   private String slideFooter = 
        "<div onclick=\"document.location=''{0}{2}.html''\"><hr>\n"
      + "<center><small><a href=\"{0}{1}.html\">previous</a> | \n"
      + "<a href=\"{0}{3}.html\">start</a> | \n"
      + "<a href=\"{0}{2}.html\">next</a></small></center>\n"
      + "</div>";
   private String lastSlideHeader = "<body>\n"
      + "<center><small><a href=\"{0}{1}.html\">previous</a> | \n"
      + "<a href=\"{0}{2}.html\">start</a></small></center>\n"
      + "<hr>";
   private String lastSlideFooter = "<hr>\n"
      + "<center><small><a href=\"{0}{1}.html\">previous</a> | \n"
      + "<a href=\"{0}{2}.html\">start</a></small></center> \n";

   private int counter;
   private StringBuffer htmlPrefix;
   private String htmlSuffix = "</body>\n</html>";   
   private String startOfSlide = "<h2>";
   private String startOfBody = "<body";
   private String endOfBody = "</body>";
   private boolean isLast; // true when "</body>" encountered
}
