#!/usr/bin/perl
# The Simplest Slideshow
# create.pl
#   input: a list of images
#   output: a set of HTML files, one per image.

use strict;

# Input is a file of image urls and associated text,
# separated by a tab.
undef $/;
my $buf = <>;
my @images = split (/\n/, $buf);

# Grab the template.
my $template;
&ReadFile ("template.htm", \$template);

# Generate the HTML slides pages.
for (my $i=0; $i<$#images; $i++) {
  my $prev = $i > 0 ? $i-1 : -1;
  my $next = $i < $#images-1 ? $i+1 : -1;
  &CreateSlide ($template, $images[$i], $i, $prev, $next);
}

sub CreateSlide
{
  my ($template, $txtLine, $this, $prev, $next) = @_;

  my ($thisImgsrc, $thisText) = split (/\t/, $txtLine);

  my $prevPage = $prev . ".htm";
  my $nextPage = $next . ".htm";

  # Merge this slide's data into the template.
  $template =~ s/<%(.*?)%>/eval($1)/gse;

  # Gray out any empty links (first, last slide).
  $template =~
    s/<a href=-1.htm>(.*?)<\/a>/<font color=gray>\1<\/font>/gsi;

  open (OUTPUT, ">slides/$this.htm");
  print OUTPUT $template;
  close OUTPUT;
}

sub ReadFile
{
  my ($fname, $buf) = @_;
  if (open (INPUT, $fname)) {
    my $oldSeparator = undef $/;
    $$buf = <INPUT>;
    $/ = $oldSeparator;
    close INPUT;
  }
}