#!/usr/bin/perl
#
# Given a list of images, create a set of HTML pages,
#   one page per image.  The pages will each show a single
#   image; clicking on the image will send the user to the
#   next page in the sequence.
#
# Add hooks for links into the first and last pages 
#   in the sequence.  The user can edit by hand later.
#
# MWR 9/18/2017

$debug = 1;

if ($#ARGV == 0) {
  printf "usage: make_click_pages.pl prefix image1 image2 ... \n";
  exit 0;
}

$num_image = 0;
for (my $i = 0; $i <= $#ARGV; $i++) {
  if ($debug > 0) {
    printf " arg %5d is ..%s.. \n", $i, $ARGV[$i];
  }

  if ($i == 0) {
    $prefix = $ARGV[$i];
    if ($debug > 0) {
      printf "prefix is ..%s.. \n", $prefix;
    }
    next;
  }
  else {
    $this_image = $ARGV[$i];
    # save the name of this image for later use
    $image_array[$num_image] = $this_image;
    if ($debug > 0) {
      printf " image %5d is ..%s.. \n", $num_image, $image_array[$num_image];
    }
    $num_image++;
  }

  
  # sanity check: make sure file exists
  if (!-f $this_image) {
    printf "error: file $this_image does not exist?!  quitting \n";
    exit(1);
  }

}


# now that we've parsed the arguments, we can start creating
#   the HTML files
for ($i = 0; $i < $num_image; $i++) {

  if (make_html($prefix, $i, $num_image) != 0) {
    printf "error: make_html returns with error for image %s \n",
              $image_array[$i];
    exit(1);
  }


}


# all done!  clean up

exit 0;



#######################################################################
# PROCEDURE: make_html
#
# DESCRIPTION: Given a prefix, and the index of this image in
#              the global "image_array[]", create a small HTML
#              page which 
#
#                 a) displays the image
#                 b) contains a link to the NEXT image in the sequence
#
# USAGE:      make_html   prefix   index
#
#             where
#                   prefix             is first portion of name for HTML file
#
#                   index              is position of image in the 
#                                         global "image_array"
#
#                   num_image          is total number of images; we need
#                                         to know if this is last image
#
# RETURNS:    0                        if all goes well
#             1                        if an error occurs
#
sub make_html {

  my ($prefix, $index, $num_image);

  $prefix = $_[0];
  $index = $_[1];
  $num_image = $_[2];

  # create the file's name
  my $html_file = sprintf "%s_%02d.html", $prefix, $index;

  # create file, fill with boilerplate
  open(HTML_FILE, ">$html_file") || die("make_html: can't create file $html_file");
  printf HTML_FILE "<html>  \n";
  printf HTML_FILE "<body>  \n";
  printf HTML_FILE "  \n";
  printf HTML_FILE "<P>  \n";

  # is this the last image in the sequence?  
  #    if yes, we'll add a link to a fixed "lecture.html", 
  #                  which the user can edit later
  #    if no,  we'll add a link to the HTML file for the next
  #                  image in the sequence
  if ($index >= ($num_image - 1)) {
    printf HTML_FILE "<a href=\"./lecture.html\">  \n";
  }
  else {
    my $next_html_file = sprintf "%s_%02d.html", $prefix, $index + 1;
    printf HTML_FILE "<a href=\"$next_html_file\">  \n";
  }

  # figure out the size of the file
  my $this_image = $image_array[$index];
  my $cmd = "identify $this_image";
  my $ret = exec_cmd($cmd);
  my @words = split(/\s+/, $ret);
  my $size_str = $words[2]; 
  $size_str =~ s/x/ /;
  @words = split(/\s+/, $size_str);
  my $width = $words[0];
  my $height = $words[1];
  if ($debug > 0) {
    printf " width $width  height $height   \n";
  }

  printf HTML_FILE "<img src=\"$this_image\" width=$width height=$height> \n";
  printf HTML_FILE "</a> \n";




  # fill in the end of the HTML file
  printf HTML_FILE "  \n";
  printf HTML_FILE "</body>  \n";
  printf HTML_FILE "</html>  \n";

  close(HTML_FILE);


  return(0);
}






##############################################################################
# PROCEDURE: exec_cmd
#
# DESCRIPTION: Execute the given shell command line.  If the $debug flag
#              is set, we print to stdout the command line, and
#              also print out the result string.
#
# RETURNS:
#              the result of the command
#
#
sub exec_cmd {

  my($cmd, $ret);

  $cmd = $_[0];

  if ($debug > 0) {
    printf "cmd is ..$cmd.. \n";
  }
  $ret = `$cmd`;
  if ($debug > 0) {
    printf "ret is ..$ret.. \n";
  }

  return($ret);
}


