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 movepipe (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 >movepipe <<!GROK!THIS!
#!$perlpath
#
# movepipe - take a player move email on stdin and:
#   (1) reformat it into html, saving it into the move directory
#   (2) optionally update the chapter master page
#
# Usage: movepipe <pbem-shortname> [<player-name>]
#  If a player name is not given, we attempt to determine the player
#  from their email address.
# Example:  (within elm)  | movepipe mage
#
# # 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';

my \$cp = '$cp';
my \$mv = '$mv';
my \$tail = '$tail';
!GROK!THIS!

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

die "Usage: movepipe <pbemname> [<character>]\n" unless $#ARGV >= 0;
my $pbem = shift(@ARGV);
&parseconfig("$pbem.config") or die "No config file $pbem.config\n";
my $given_name = shift(@ARGV);


my %hdr;
while (<STDIN>) {
  last if /^$/;
  chop;
  if (/(\w+): (.*)/) {
	$hdr{$1} = $2;
  }
}

my $sub;
if ($given_name) {
  $sub = $given_name;
  $sub =~ tr/A-Z/a-z/;
  $sub = "\u$sub";
} else {
  # If your subject lines contain [playername], use this:
  #$sub = $hdr{"Subject"};
  #$sub =~ /\[(.*)\]/;
  #$sub = $1;
  
  # If you want to identify players by email address, use this:
  $sub = ($hdr{"Reply-To"} ? $hdr{"Reply-To"} : $hdr{"From"});
  $sub =~ /(\w+)@/;
  $sub = $1;
  my $player;
  foreach (keys %Email) {
    $player = $_, last if ($Email{$_} =~ /$sub/i);
  }
  die "No such player: $sub\n" unless $player;
  $player =~ tr/A-Z/a-z/;
  $sub = "\u$player";
}

my @date = split(" ",$hdr{"Date"});
my $date = join("-",@date[1..3]);

# Figure out the directory by figuring out which turn is yet to play
opendir(DIR,$Turndir) or die "Unable to open $Turndir\n";
my @files = grep(/^$pbem-\d+$/o,readdir(DIR));
closedir(DIR);
@files = reverse sort byturn grep(s/$pbem-(\d+)/$1/,@files);
my $nextturn = $files[0] + 1;
my $dir = "$Movedir/$nextturn";
unless (-e $dir) {
  mkdir($dir,0700);
  chmod(0755,$dir) if $Webdir;
}

my $ext = "";
my $file = $sub . $ext ."-".$date.".html";
while (-r "$dir/$file") {
  $ext++;
  $file = $sub.$ext."-".$date.".html";
}

die "Unable to write to $dir/$file\n" unless open(OUT,">$dir/$file");

print OUT "<html><head><title>\n";
print OUT $hdr{"Subject"}," ($date)\n";
print OUT "</title></head><body><pre>\n";
print OUT "Player: $sub\n";
print OUT "Date: ",$hdr{"Date"},"\n";
print OUT "Subj: ",$hdr{"Subject"},"\n\n";

while (<STDIN>) {
  last if /^-- ?$/;
  s#\&#&amp;#g;
  s#\<#&lt;#g;
  s#\>#&gt;#g;
  s#\{\s*#<i>Plan:\n#g;
  s#\s*\}#</i>#g;
  s#\[\s*#<i>To GM:\n#g;
  s#\s*\]#</i>#g;
  s#^#<hr>#g;
  next if m#\S+\@\S+\.\S+#;
  print OUT;
}

print OUT "\n</pre></body></html>\n";
close(OUT);

exit 0 unless $Webdir; # Skip the rest if we're not maintaining a web site.

chmod(0644,"$dir/$file");
# Now we want to insert a link to $file in the master html file
# for the current chapter at the point marked by
# <!-- INSERT HERE -->
# The move is for move # $nextturn

my $tmp = "/tmp/movepipe.$$";
my $marker = "\<!-- INSERT HERE --\>";
my $anchor = $file;
$anchor =~ s/\.html//;
my $master = "$Webdir/ch${Chapter}master.html";
# Create it if it doesn't exist 
unless (-e $master) {
 my $chapter_name = "";
 warn "Looks like this is the first move for a new chapter.\n";
 warn "You'll want to edit $master to set the chapter name.\n";
 &writemaster($master,$chapter_name,$pbem,$nextturn) or die "Unable to create chapter master file\n";
}

my $addendum;
if ($anchor =~ s/^([A-Za-z]+)(\d+)/$1/) {
  $addendum = $2;
}
$anchor =~ s/-9(\d)$/-199$1/;
$anchor .= ")";
$anchor .= " Addendum $addendum" if $addendum;
$anchor =~ s/([a-z])-/$1 (/;

system $cp, "$master", "$master~";

die "Unable to open $master\n" unless open(IN,"$master");
die "Unable to open $tmp\n" unless open(OUT,">$tmp");

while (<IN>) {
  if (/$marker/) {
    print OUT "  <li><a href=\"moves/$nextturn/$file\">$anchor</a>\n";
  }
  print OUT;
}
close(OUT);
close(IN);

system $mv, $tmp, $master;
chmod(0644,$master);
system("$Cgipath/expand.cgi $ENV{'LOGNAME'} $pbem $Chapter nobody | $tail +2 > $Webdir/ch$Chapter.html");


exit 0;

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