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 movefilter (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 >movefilter <<!GROK!THIS!
#!$perlpath
#
# movefilter - accept an emailed player move on stdin, send customized
#              versions out to the players. This is very similar to
#              mailturn.
#
# Usage: | movefilter <pbemname> [<player-group>]
#
# # 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 .);
use Getopt::Std;
use Mail::Sendmail;
require "pbemtools.pl";
!GROK!THIS!

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

&getopts('d') if ($#ARGV > 0);
my $debug = 1 if ($opt_d);
die "Usage: movefilter <pbemname> [<player-group>]\n" unless @ARGV > 0;
my $pbem = shift(@ARGV);
&parseconfig("$pbem.config") or die "No config file $pbem.config\n";
my $group = shift(@ARGV);
my $groups;
if ($group) {
  $group =~ tr/A-Z/a-z/;
  $groups = $groups{$arg};
}

@Players = grep($groups =~ /$_/i,@Players) if $groups;
my %hdr;
while (<STDIN>) {
  last if /^$/;
  chop;
  if (/(\w+): (.*)/) {
	$hdr{$1} = $2;
  }
}

my $sub;
my $sender = ($hdr{"Reply-To"} ? $hdr{"Reply-To"} : $hdr{"From"});
my $subject = $hdr{'Subject'};

# 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 = $sender;
$sub =~ /(\w+)@/;
$sub = $1;
my $player;
foreach (keys %Email) {
    $player = $_, last if ($Email{$_} =~ /$sub/i);
}
&bounce_and_die($sender) unless $player;
$player =~ tr/A-Z/a-z/;
$sub = "\u$player";
				 
my @turn = <STDIN>;		# Slurp!
my @output;			 
foreach (@Players) {		 
    next if $Email{$_} =~ /npc/i;
    @output = &turnify($_,$debug,@turn);
    next unless @output;	 
    if ($debug) {		
	open($_,">$_");	 
        select($_);
        $to = $Mailinglist ? $Mailinglist : $Email{$_};
        print "From: $sender\n";
        print "To: $to\n";
        print "Subject: $Subjecttag ($sub) $subject\n";
        print "X-PBEM-Character: $_\n";
        print "X-PBEM-Group: $group\n" if $group;
        print "\n";
        print join("",@output);
        close($_);
    } else {			
	my %mail = ('To' => $Email{$_}, 'From' => $sender, 'Subject' =>
		"$Subjecttag ($sub) $subject", 'X-PBEM-Character' => $_,
		'Body' => join("",@output));
	$mail{'X-PBEM-Group'} = $group if $group;
	$mail{'smtp'} = $Smtp if $Smtp;
	$mail{'Reply-To'} = $Mailinglist if $Mailinglist;
	warn "Failed email to $Email{$_}\n" unless sendmail %mail;
    }			

}

exit 0;


# Bounce message back to $sender, if we've got one, and quit
sub bounce_and_die {
    my $sender = shift;
    if (open(LOG,">>$Turndir/movefilter.log")) {
	print LOG "movefilter ($pbem, $group) failed on mail from $sender\n";
	close(LOG);	
    }
    if ($sender) {
	my %mail = ('To' => $sender, 'Cc' => $Email{'gm'}, 'From' => $Email{'gm'},
		'Subject' => "Unrecognized message to $pbem");
	$mail{'smtp'} = $Smtp if $Smtp;
	my $message = join("",<STDIN>);
	$mail{'Body'} = <<EOP;
This is an automated response.  If you reply to it, you'll reach the
$pbem GM.

Your $pbem move couldn't be distributed. This almost
certainly means that it was sent from an unusual email address.
Try sending your move from your usual email address (the address at which
you receive PBEM Turns).

Your message body is included below.
If you think something else is the problem, let the GM know.

---------Your message follows--------
$message
EOP
   	sendmail %mail;
  }
  exit 0;
}
!NO!SUBS!
chmod 755 movefilter
$eunicefix movefilter
