case $CONFIG in
'')
	if test -f config.sh; then TOP=.;
	elif test -f ../config.sh; then TOP=..;
	elif test -f ../../config.sh; then TOP=../..;
	elif test -f ../../../config.sh; then TOP=../../..;
	elif test -f ../../../../config.sh; then TOP=../../../..;
	else
		echo "Can't find config.sh."; exit 1
	fi
	. $TOP/config.sh
	;;
esac
: This forces SH files to create target in same directory as SH file.
: This is so that make depend always knows where to find SH derivatives.
case "$0" in
*/*) cd `expr X$0 : 'X\(.*\)/'` ;;
esac
echo "Extracting expand.cgi (with variable substitutions)"
: This section of the file will have variable substitutions done on it.
: Move anything that needs config subs from !NO!SUBS! section to !GROK!THIS!.
: Protect any dollar signs and backticks that you do not want interpreted
: by putting a backslash in front.  You may delete these comments.
$spitshell >expand.cgi <<!GROK!THIS!
#!$perlpath
#
# expand.cgi - A cgi script to take a master html file, expand
#              a given section, and return the file.
#
# The master html should 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
#
# Usage: 
# <a name="section" href="/cgi-bin/expand.cgi?user+pbem+chapter+section#section>
# Section Name
# </a>
#
# # Copyright 1998 Alan Schwartz
# # 
# # This file is part of pbemtools.
# # 
# # pbemtools is free software; you can redistribute it and/or modify
# # it under the terms of the GNU General Public License as published by
# # the Free Software Foundation; either version 2, or (at your option)
# # any later version.
# # 
# # pbemtools is distributed in the hope that it will be useful,
# # but WITHOUT ANY WARRANTY; without even the implied warranty of
# # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# # GNU General Public License for more details.
# # 
# # You should have received a copy of the GNU General Public License
# # along with pbemtools; see the file COPYING.
# # If not, write to the Free Software Foundation,
# # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# # 
# # Report problems and direct all questions to:
# # 
# #     alansz@pennmush.org
# # 

use lib qw($installperllib .);
require 'pbemtools.pl';
!GROK!THIS!

: In the following dollars and backticks do not need the extra backslash.
$spitshell >>expand.cgi <<'!NO!SUBS!'

print <<EOP;
Content-type: text/html

EOP
my $arg = join("+",@ARGV);
# Lower-case $arg
$arg = "\L$arg";
unless ($arg && $arg =~ /([-_a-z0-9]+)\+([-_a-z0-9 ]+)\+(\d+)\+([-_a-z0-9 ]+)(#\4)?/) {
  print "Error: The expand cgi script was called improperly.\n";
  print "Please inform $Email{'gm'} of the link you followed.\n";
  exit 0;
}
my ($user,$pbem,$chapter,$section) = ($1,$2,$3,$4);
$ENV{'HOME'} = &gethomedir($user);
unless (parseconfig("$pbem.config")) {
  print "Error: The expand cgi script couldn't find $pbem.config.\n"; 
  print "Please inform $Email{'gm'} of the link you followed.\n";
  exit 0;
}

if (($chapter > $Chapter) or ($chapter < 1)) {
  print "Error: The expand cgi script was called with a bad chapter number.\n";
  print "Please inform $Email{'gm'} of the link you followed.\n";
  exit 0;
}
 
my $master = "$Webdir/ch${chapter}master.html";
unless (open(IN,$master)) {
  print "Error: The expand cgi script couldn't open the chapter $chapter master.\n";
  print "Please inform $Email{'gm'} of the link you followed.\n";
  exit 0;
}

my $href;
my ($current,$count);
while (<IN>) {
  if (m#href\s*=\s*"(.*?)"#) {
    $href = $1;
    s#href\s*=\s*"([^/\#])#href="$Weburl/$1# unless $href =~ m#//#;
  }
  $current = $1 if $count == 1 && /\<li\>.*name="([^"]*)"/i;
  $count++ if /\<ul\>/i;
  print unless ($count > 1 && $current !~ /$section($|\D)/i);
  $count-- if /\<\/ul\>/i;
}

print <<EOP;
</body></html>
EOP

exit 0;
!NO!SUBS!
chmod 755 expand.cgi
$eunicefix expand.cgi
