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 mailturn (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 >mailturn <<!GROK!THIS!
#!$perlpath
#
# mailturn
# This program accepts turn source, and creates customized
# versions of the turn for each player to reflect language knowledge.
# It mails these out to the players. 
#
# Usage: mailturn [-d] <pbemname> <filename or turn #>
# Ex:    mailturn mage mage-3
# Ex:    mailturn mage 3
#   Either will use 'mage-3' as the source file for the turn.
# The -d switch is for debugging. Instead of mailing the turns,
# they'll be output as files named by character.
#
# # 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;
use Mail::Sendmail;


!GROK!THIS!

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

&getopts('d') if ($#ARGV > 0);
my $archive = 1 if ($opt_d);

my $pbem = shift(@ARGV);
&parseconfig("$pbem.config") or die "No config file $pbem.config\n";
my $infile = shift(@ARGV);
$infile = "$pbem-$infile" if $infile =~ /^\d+$/;
open(IN,"$Turndir/$infile") or die "Unable to open $Turndir/$infile\n";

my($subject,$to);
unless ($archive) {
  print "Enter subject line";
  print " ($Subjecttag will be prepended)" if $Subjecttag;
  print ":\n> ";
  $subject = <STDIN>;
  chop($subject);
  $subject = "$Subjecttag $subject" if $Subjecttag;
}

my @turn = <IN>;	# Slurp!
close(IN);

foreach (@Players) {
  next unless $Email{$_}; # Skip players who have left the game.
  next if $Email{$_} =~ /npc/i; # Skip players who have left the game.
  @output = &turnify($_,$archive,@turn);
  warn "No Turn sent to $_ this time\n", next unless @output;
  if ($archive) {
    # Use for debugging or archiving
    open($_,">$Turndir/$infile.$_");
    select($_);
    print join("",@output);
    close($_);
  } else {
    my %mail = ( 'To' => $Email{$_}, 'Subject' => $subject, 
	'Body' => join("",@output));
    $mail{'Reply-To'} = $Mailinglist if ($Mailinglist);
    $mail{'smtp'} = $Smtp if ($Smtp);
    warn "Email to $Email{$_} failed\n" unless sendmail %mail;
  }
  chmod(0644,"$Turndir/$infile.$_") if ($archive && $Cgiurl);
}

chmod(0644,"$Turndir/$infile") if $Cgiurl;
exit 0;

!NO!SUBS!
chmod 755 mailturn
$eunicefix mailturn
