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 allturns.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 >allturns.cgi <<!GROK!THIS!
#!$perlpath
#
# allturns.cgi: A CGI script. Given a user name, pbem name, a chapter number,
#               and a player name, return the concatenation of all 
#		the player's turns.
#
# Usage: <a href="/cgi-bin/allturns.cgi?user+pbem+chapter+player">turns</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 >>allturns.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 ]+)/) {
  print "Error: The allturns cgi script was called improperly.\n";
  print "Please inform $Email{'gm'} of the link you followed.\n";
  exit 0;
}
my ($user,$pbem,$chapter,$player) = ($1,$2,$3,$4);
$ENV{'HOME'} = &gethomedir($user);
unless (parseconfig("$pbem.config")) {
  print "Error: The allturns 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 allturns 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 allturns cgi script couldn't open the chapter $chapter master.\n";
  print "Please inform $Email{'gm'} of the link you followed.\n";
  exit 0;
}

# Ok, proceed
 
print <<EOP;
<html><head>
<title>$Fullname, chapter $chapter: \u$player (story to date)</title>
</head>
<body><pre>
EOP

# Make a list of turns this player was in, by reading master.html
# We search for 'spewturn.cgi?user+pbem+#+player'
my @files;
while (<IN>) {
  push(@files,"$pbem-$1") if /spewturn\.cgi\?$user\+$pbem\+(\d+)\+$player/io;
}
close(IN);
@files = sort byturn @files;

for my $file (@files) {
  if (open(IN,"$Turndir/$file")) {
	print &turnify($player,1,<IN>);
	close(IN);
  } else {
	print "*** Unable to retrieve $file ***\n";
  }
}

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

exit 0;


# Sort words of the form turn# by the #.
sub byturn {
  local($c,$d) = ($a,$b);
  $c =~ s/turn//;
  $d =~ s/turn//;
  return $c <=> $d;
} 
!NO!SUBS!
chmod 755 allturns.cgi
$eunicefix allturns.cgi
