#! perl -w
@mykeys=qw/explicitslide ~explicitslide \\bs \\endslide/;
open IX,">0_index.html" or die "cant open index";
  print IX << "EOF";
<!doctype html public '-//W3C//DTD HTML 4.01//EN'
  'http://www.w3.org/TR/html4/strict.dtd'>
<html>
<head>
<title>Representing Smooth Surfaces</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="gsl.css" >
</head><body class="slide"><h1>Index of Slides</h1>
<p><a href="./">Jump to Directory of Files</a></p>
<ol class="regularfont">
EOF

for(@mykeys){
  $keyword{$_}=1;
}
$slide_number=1;
while(<>){
  next if /^\s*$/;
  next if /^\%/;
  if (/^explicitslide\s+(.*)/){
    &explicitslide($1);
    $slide_number++;
    next;
  }
  if (/^\\bs\{(.*)\}/){
    &slide($1);
    $slide_number++;
    next;
  }
  else{
    die "$ARGV:$.: unexpected global token $_";
  }
}
print IX << "EOF";
</ol>
</body>
</html>
EOF
close IX;
sub opensl {
  my $fn;
  $fn=sprintf("0the_s%03d.html",$slide_number);
  open FO,">$fn" or die "cant open $fn";
}

sub explicitslide {
  my($title)=@_;
  &header($title);
  while(<>){
    #print STDERR "now $_";
    last if /^~explicitslide$/;
    if (/^(\S+)/){
      die "bad keyword in explicitslide: '$_'" if $keyword{$1};
    }
    print FO;
  }
  &footer();
}
sub nextslide{
  my($nslide);
  my($pslide);
  my($curr);
  my($time);
  my($ptime);
  $time=1.1+(1.2*($slide_number)/50.0);
  $min=int(60*($time-int($time)));
  $hr=int($time);
  $ptime=sprintf("%d:%02d",$hr,$min);
  $nslide=sprintf("0the_s%03d.html",$slide_number+1);
  $pslide=sprintf("0the_s%03d.html",$slide_number-1);
  $curr=sprintf("0the_s%03d.html",$slide_number);
  

  print FO << "EOF";
  <p class="footer">$slide_number: <a href="$pslide">Prev</a>
    <a href="$nslide">Next</a> <a href="0_index.html">Index</a>
$ptime
</p>
EOF
#<a href="http://validator.w3.org/check?uri=http://www.mit.edu/~kenta/six/sl2/$curr">HTML</a>
#<a href="http://jigsaw.w3.org/css-validator/validator?uri=http://www.mit.edu/~kenta/six/sl2/$curr">css</a>

}
sub slide {
  my($title)=@_;
  &header($title);
print FO << "EOF";
</head>
<body class="slide">
EOF

&nextslide();
print FO << "EOF";
<h1>$title</h1>
<p class="nothing"><img src="line.redblueS.gif" class="wholewidth" alt="------------"></p>
EOF

  while(<>){
    #print STDERR "now $_";
    last if /^\\endslide$/;
    if (/\S/){
      if (/^\\ii/){
	&list();
	next;
      }
      else{
	die "$ARGV:$.:bad keyword in slide '$_'" if /\\bs/;
	print FO;
	next;
      }
    }
    #print STDERR "ig $_";
  }

  &footer();

}

sub list(){
  print FO "<UL>\n";
  while(<>){
    next if /^\s*$/;
    next if /^\s*\%/;
    if (/^\\ii/){
      &list();
      next;
    }
    last if /^\\eii/;
    die "$ARGV:$.:not list $_" unless /^\\item\s+(.+)/;
    print FO "<li>$1</li>\n";
  }
  print FO "</UL>\n";
}

sub header {
  my($title)=@_;
  my($fn);
  $fn=sprintf("0the_s%03d.html",$slide_number);

  print IX "<li><a href=\"$fn\" >$title</a></li>\n";
  &opensl();  
  print FO << "EOF";
<!doctype html public '-//W3C//DTD HTML 4.01//EN'
  'http://www.w3.org/TR/html4/strict.dtd'>
<html>
<head>
<title>$title</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="gsl.css" >


EOF
}
sub footer{
  print FO << "EOF";

</body>
</html>
EOF
  close FO;
}
