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 webturn (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 >webturn <<!GROK!THIS!
#!$perlpath
#
# webturn: Update a master html file to include a link to a new turn.
#
# Usage: webturn <pbemname> <turn>
# <turn> be either a turn number or a turn filename.
# Example:
#        webturn mage mage-3
#        webturn mage 3
# will both insert a link to the cgi script that will read mage-3
# in the turn directory. If you're not using cgi scripts, the
# program won't do anything.
#
# # 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';
use Getopt::Std;

my \$cp = "$cp";
my \$mv = "$mv";
my \$tail = "$tail";

!GROK!THIS!

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

&getopts('q');

die "Usage: webturn <pbemname> <turn>\n" unless $#ARGV == 1;
my $pbem = shift(@ARGV);
&parseconfig("$pbem.config") or die "No config file $pbem.config\n";
unless ($Cgiurl) {
 warn "Cgiurl is not set, webturn has nothing to do!\n" unless $opt_q;
 exit 0;
}
$turn = shift(@ARGV); # Global
$turn =~ s/$pbem-//;
die "I can't find $Turndir/$pbem-$turn\n" unless -r "$Turndir/$pbem-$turn";

my $master = "$Webdir/ch${Chapter}master.html";
my $tmp = "/tmp/webturn.$$";
my $user = $ENV{'LOGNAME'};

my $title;
print "Turn title (Turn N: blah): ";
chop($title = <STDIN>);

open(OUT,">$tmp") || die "Couldn't open $tmp\n";
unless (open(IN,$master)) {
  my $chapter_name;
  warn "Looks like we're doing the first Turn in this Chapter.\n";
  print "What's the title of this Chapter: ";
  chop($chapter_name = <STDIN>);
  warn "Creating $master...\n";
  &writemaster($master,$chapter_name,$pbem,$turn) or die "Unable to create $master\n";
  open(IN,$master) or die "Unable to open $master anyway.\n";
}

while (<IN>) {
  if (/\<!-- (\w+) --\>/) {
	$name = $1;
	$lcname = "\L$name";
	if ($Email{$lcname}) {
	  # An active player. Add a reference
		  print OUT <<EOP;
 <li><a href="$Cgiurl/spewturn.cgi?$user+$pbem+$turn+\u$lcname">$title</a>
EOP
        }
  } elsif (/\<!-- INSERT HERE --\>/) {
	  # This is where new moves are inserted. Let's change it
          # so that new moves are now inserted for the next turn.
	  my $next = $turn + 1;
	  print OUT <<EOP;
  </ul>
<li><a name="move$next" href="$Cgiurl/expand.cgi?$user+$pbem+$Chapter+move$next#move$next">For Turn $next</a>
  <ul>
EOP
  }
  print OUT;
}
close(IN);
close(OUT);

print "Producing new $master\n";
system("$cp $master $master.bak");
system("$mv $tmp $master");
chmod(0644,$master);
system("$Cgipath/expand.cgi $user $pbem $Chapter nobody | $tail +2 > $Webdir/ch$Chapter.html");

!NO!SUBS!
chmod 755 webturn
$eunicefix webturn
