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 moveremind (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 >moveremind <<!GROK!THIS!
#!$perlpath
#
# moveremind: Send a reminder message to players who haven't made a move
#             in the current turn yet.
#
# Usage: moveremind <pbem>
#
# @COPYRIGHT @
#

use lib qw($installperllib .);
use Mail::Sendmail;
require 'pbemtools.pl';

!GROK!THIS!

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

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

my %message = ('Subject' => "$Subjecttag PBEM move reminder",
	'From' => $gm);
$message{'smtp'} = $Smtp if $Smtp;
$message{'Body'} = <<EOM;
I have not yet received your move for the current PBEM Turn.
Please send a move as soon as you can. Thanks.

 - GM

EOM


# Figure out what turn we're on, and where those moves are stored.
opendir(DIR,$Turndir) or die "Unable to open $Turndir\n";
my @files = readdir(DIR);
closedir(DIR);
@files = reverse sort byturn grep(s/$pbem-(\d+)/$1/,@files);
my $nextturn = $files[0] + 1;
my $dir = "$Movedir/$nextturn";

# Get the list of who's moved
opendir(DIR,$dir) or die "Unable to open $dir\n";
@files = readdir(DIR);
closedir(DIR);
@files = grep(s/^(.*?)-\d.*/$1/,@files);
my $name;
foreach (@files) {
  $name = "\L$_";
  delete $Email{$name};
}
delete $Email{'gm'};

# Anybody left in $Email gets one.
foreach $name (keys %Email) {
  next unless $Email{$name}; # Skip blank email addresses
  next if $Email{$name} =~ /npc/i; # Skip blank email addresses
  my %custom_message = %message;
  $custom_message{'To'} = $Email{$name};
  sendmail %custom_message;
  print STDERR "Reminding \u$name ...\n";
}

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 moveremind
$eunicefix moveremind
