# Daves Slide hack - see http://www.treblig.org/dsh.html
# Copyright David Alan Gilbert (c) 2001 - use and copy under
# the GNU Public license - see http://www.gnu.org
# Version 0.01 7/1/2001

# Pull in the config file for your talk
do "dshrc";

# Scan for Slide files
opendir CURRENTDIR, ".";
@dirtext = grep /^S.*.slide$/, readdir CURRENTDIR;
closedir CURRENTDIR;
@sorteddir=sort @dirtext;

# Create the index file header
open INDEXFILE, ">index.html" || die "Oops - couldn't open index file";
print INDEXFILE $genheader1;
print INDEXFILE $talktitle;
print INDEXFILE "$endheader<center><H1>$talktitle</H1></center>";
print INDEXFILE $genheader2;
print INDEXFILE "<!--- Created with dsh - see http://www.treblig.org/dsh.html --->";
print INDEXFILE "<ol>";

$slidenum=1;

# For each slide file
foreach $slidefilename (@sorteddir) {
  $slidebasename=$slidefilename;
  $slidebasename =~ s/S[0-99]*(.*)\.slide/$1/;
  open INFILE, "<$slidefilename" || die "Oops - couldn't open input file";
  open OUTFILE, ">$slidenum.html" || die "Oops  - couldn't open output file";

  # Extract the slide title from the first line of the slide file
  $headerline=<INFILE>;
  $headerline =~ s/^<H1>(.*)<\/H1>$/$1/ || die "Couldn't find header in $slidebasename";

  # Generate all the output file headers
  print OUTFILE $genheader1;
  print OUTFILE $headerline;
  print OUTFILE "$endheader<center><H1>$headerline</H1></center>";
  print OUTFILE $genheader2;

  # For the previous/next/index links
  print OUTFILE "<center><p>";
  if ($slidenum!=1) {
    $slidenumm1=$slidenum-1;
    print OUTFILE "<A href=\"$slidenumm1.html\">Previous </a>";
  };
  print OUTFILE "<A href=\"index.html\">Index</a>";
  if ($slidenum!=scalar(@sorteddir)) {
    $slidenump1=$slidenum+1;
    print OUTFILE "<A href=\"$slidenump1.html\"> Next</a>";
  };
  print OUTFILE "</p></center><br><HR>";

  print INDEXFILE "<li><A href=\"$slidenum.html\">$headerline</a></li>";

  # Just copy the slide file straight through now
  while (<INFILE>) { print OUTFILE; };

  print OUTFILE  $genfooter;
  close INFILE;
  close OUTFILE;
  $slidenum++;
}

# Tidy up the index file
print INDEXFILE "</ol>";
print INDEXFILE $genfooter;
close INDEXFILE;
