#!/usr/local/bin/perl5 -T
#
# expand.cgi - A cgi script to take a master html file, and expand
# a given section, and return the file.
#
# The master html will contain bulleted lists (<ul>) with sublists
# which are expandable/collapsable. We loop through the list,
# only expanded the requested portion.
#
# This nifty idea is due to Greg Atchity. The cgi implementation
# is by Alan Schwartz

$target = $ARGV[0];
$target =~ s/#.*//; 
$target =~ s/[^\w\s\d]//g;
$master = '/home/alansz/www/rw-pbem/master.html';
$top = "/~alansz/rw-pbem/";

&header;
unless (open(IN,$master)) {
  print "Sorry, can't.\n";
  exit 0;
}

while (<IN>) {
  if (m#href\s*=\s*"(.*?)"#) {
    $href = $1;
    s#href\s*=\s*"([^/\#])#href="$top$1# unless $href =~ m#//#;
  }
  $current = $1 if $count == 1 && /\<li\>.*name="([^"]*)"/i;
  $count++ if /\<ul\>/i;
  print unless ($count > 1 && $current !~ /$target/i);
  $count-- if /\<\/ul\>/i;
}
&footer;
exit 0;

#-----------------------------------

sub header {
  print <<EOP;
Content-type: text/html

EOP
}

sub footer {
  print <<EOP;
</body></html>
EOP
}
