#!/usr/local/bin/perl5 -T
#
# allturns.cgi - A cgi script which should be given a player name
# and return the concatenation of all the player's turns

push(@INC,'/home/alansz/rw');
require 'rw-lib.ph';

my $arg = $ARGV[0];
$arg = "\L$arg";
$arg =~ /([-a-z ]+)/;	 # We've got to untaint arg
my $player = $1;

&header;

print <<EOP;
<html><head>
<title>Riverworld PBEM: \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?#+player'
unless (open(IN,'/home/alansz/www/rw-pbem/master.html')) {
  print "Can't open master html file\n";
  exit 0;
}
while (<IN>) {
  push(@files,"turn$1") if /spewturn\?(\d+)\+$player/io;
}
close(IN);

unless (chdir('/home/alansz/rw/turns')) {
  print "Sorry, can't change dir.\n";
  exit 0;
}

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

&footer;
exit 0;

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

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

EOP
}

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

# Sort words of the form turn# by the #.
sub byturn {
  local($c,$d) = ($a,$b);
  $c =~ s/turn//;
  $d =~ s/turn//;
  return $c <=> $d;
}
