#! /usr/bin/perl
## (configure will change the top line to the location of perl on your system)
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
## cmail: a tool to aid playing chess by email
## Copyright (C) 1993,2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
## 
##  cmail 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 3 of the License, or (at
##  your option) any later version.
## 
##  cmail 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 this program. If not, see http://www.gnu.org/licenses/.  *
##
## Email:     evan@quadstone.co.uk
## Snailmail: Evan Welsh
##            Quadstone Ltd
##            16 Chester Street
##            Edinburgh EH3 7RA
##            Scotland
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#


#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
## Print verbose diagnostics for debugging
sub debug {
    if ($DEBUG) {
	local ($old) = select ;	## Remember selected output
	select (logfile) ;
	$| = 1 ;		## Keep it flushed
	print @_ ;		## Print arguments
	select ($old) ;
    }
}
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#


#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
## Create a directory for storing games in if it doesn't already exist
sub need_chess_dir {
    local ($old) ;

    ## ################################################################# ##
    ## Check for existence of the named chess directory
    ## ################################################################# ##

    if (! (-d "$CMAILDIR")) {

	## ############################################################# ##
	## Ask user for confirmation if attached to tty
	## ############################################################# ##

	if (-t) {
	    $old = select ;	## Remember selected output
	    select (stdout) ;	## Write to standard output
	    $| = 1 ;		## Keep it flushed
	    print (  "CMail directory \"$CMAILDIR\" does not exist."
		   . " Create it? [y/q]: ") ;

	    $_ = <tty> ;	## Read response from tty
	    die "Bye!\n" if (/^[qQ].*/) ; ## Quit if q selected

	    select ($old) ;	## Re-select the old output
	}

	## ############################################################# ##
	## Create a cmail directory or die
	## ############################################################# ##

	die "cmail: Can't create CMail directory: \"$CMAILDIR\"\n"
	    unless mkdir ("$CMAILDIR", 511) ;
	print (  "Created cmail directory \"$CMAILDIR\".\n"
	       . "You can move it but remember to set the CMAIL_DIR"
	       . " environment variable.\n") ;
    }

    ## ################################################################# ##
    ## Change to the $CMAILDIR directory whether newly created or not
    ## ################################################################# ##

    die "Couldn't changed directory to \"$CMAILDIR\"\n"
	unless (chdir "$CMAILDIR") ;

    ## ################################################################# ##
    ## Check for existence of the named chess directory
    ## ################################################################# ##

    if (! (-d "$ARCDIR")) {

	## ############################################################# ##
	## Ask user for confirmation if attached to tty
	## ############################################################# ##

	if (-t) {
	    $old = select ;	## Remember selected output
	    select (stdout) ;	## Write to standard output
	    $| = 1 ;		## Keep it flushed
	    print (  "Archive directory \"$ARCDIR\" does not exist."
		   . " Create it? [y/q]: ") ;

	    $_ = <tty> ;	## Read response from tty
	    die "Bye!\n" if (/^[qQ].*/) ; ## Quit if q selected

	    select ($old) ;	## Re-select the old output
	}

	## ############################################################# ##
	## Create a chess directory or die
	## ############################################################# ##

	die "cmail: Can't create archive directory: \"$ARCDIR\"\n"
	    unless mkdir ("$ARCDIR", 511) ;
	print (  "Created archive directory \"$ARCDIR\".\n"
	       . "You can move it but remember to set the CMAIL_ARCDIR"
	       . " environment variable.\n") ;
    }
}
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#


#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
## Parse command-line arguments
sub parse_flags {
    ## ################################################################# ##
    ## Set up defaults from the environment or from hard-wired constants
    ## ################################################################# ##

    $SHOWC       = 0 ;
    $SHOWW       = 0 ;
    $OUTPUT_POS  = $ENV{'CMAIL_OUTPUT_POS'} ;
    $LOGFILE     = $ENV{'CMAIL_LOGFILE'} ;
    $MAILPROG    = $ENV{'CMAIL_MAILPROG'} ;
    $MAILPROG    = "/usr/sbin/sendmail" if (   (-x "/usr/sbin/sendmail")
					    && (! $MAILPROG)) ;
    $MAILPROG    = "/usr/lib/sendmail" if (   (-x "/usr/lib/sendmail")
					   && (! $MAILPROG)) ;
    $MAILPROG    = "/etc/sendmail" if (   (-x "/usr/lib/sendmail")
					   && (! $MAILPROG)) ;
    $MAILPROG    = "/usr/ucb/Mail" if (   (-x "/usr/ucb/Mail")
				       && (! $MAILPROG)) ;
    $MAILPROG    = "/usr/ucb/mail" if (   (-x "/usr/ucb/mail")
				       && (! $MAILPROG)) ;
    $MAILPROG    = "Mail" unless ($MAILPROG) ;
    $HOMEDIR     = $ENV{'HOME'} ;
    $CMAILDIR	 = $ENV{'CMAIL_DIR'} ;
    $CMAILDIR	 = $ENV{'CHESSDIR'} unless ($CMAILDIR) ;
    $CMAILDIR	 = "$HOMEDIR/Chess" unless ($CMAILDIR) ;
    $CMAILDIR	 = "~/Chess"        unless ($HOMEDIR) ;
    $NUM_GAMES	 = "?" ;
    $NUM_WGAMES	 = "?" ;
    $NUM_BGAMES	 = "?" ;
    $TIME_DELAY	 = $ENV{'CMAIL_TIME_DELAY'} ;
    $TIME_DELAY	 = 0 unless ($TIME_DELAY) ;
    $PW_NAME     = &get_pw_name () ;
    $MY_NNAME	 = $PW_NAME ;
    $MY_NNAME	 = $ENV{'LOGNAME'} unless ($MY_NNAME) ;
    $MY_NNAME	 = $ENV{'USER'} unless ($MY_NNAME) ;
    $MY_NNAME	 = "?" unless ($MY_NNAME) ;
    $PGN_EVENT   = "Email correspondence game" ;
    $PGN_SITE	 = "NET";
    $PGN_ROUND	 = "-";
    $PGN_MODE    = "EM";
    $SEND_MAIL   = 1 ;
    $REMAIL      = 0 ;
    $LOAD_XBOARD = 1 unless $ENV{'CMAIL_NO_XBOARD'} ;
    $REUSE       = 1 ;
    @TD_FLAGS    = ("-td", $TIME_DELAY) ;
    @NCP_FLAGS   = ("-ncp") ;

    ## ################################################################# ##
    ## Define the usage string
    ## ################################################################# ##

    $USAGE = ("cmail
        [-h] [-c] [-w] [-[x]v] [-[x]mail] [-[x]xboard] [-[x]reuse] [-remail] 
        [-game <name>] [-(w|b|)games <number>] [-(me|opp) <short name>]
        [-(w|b|my|opp)name <full name>] [-(w|b|my|opp)na <email>]
        [-dir <directory>] [-arcdir <directory>] [-mailprog <mail program>]
        [-logFile <file>] [-event <event>] [-site <site>] [-round <round>]
	[-mode <mode>]") ;

    ## ################################################################# ##
    ## Overwrite defaults if specified on the command-line
    ## ################################################################# ##

    @UNREC_ARGS = () ;
    while ($ARGV = shift) {
	$UNREC = 0 if ($ARGV =~ /^-/) ;
	if    ("$ARGV" eq "-h")		  {die ("Usage: $USAGE\n")     ;}
	elsif ("$ARGV" eq "-c")		  {$SHOWC       = 1            ;}
	elsif ("$ARGV" eq "-w")		  {$SHOWW       = 1            ;}
	elsif ("$ARGV" eq "-v")		  {$DEBUG       = 1            ;
				           @DEBUG_FLAGS = ("-debug")   ;}
	elsif ("$ARGV" eq "-xv")	  {$DEBUG       = 0            ;
				           $QUIET       = 1            ;}
	elsif ("$ARGV" eq "-mail")	  {$SEND_MAIL   = 1            ;}
	elsif ("$ARGV" eq "-xmail")	  {$SEND_MAIL   = 0            ;}
	elsif ("$ARGV" eq "-xboard")	  {$LOAD_XBOARD = 1            ;}
	elsif ("$ARGV" eq "-xxboard")	  {$LOAD_XBOARD = 0            ;}
	elsif ("$ARGV" eq "-reuse")	  {$REUSE       = 1            ;}
	elsif ("$ARGV" eq "-xreuse")	  {$REUSE       = 0            ;}
	elsif ("$ARGV" eq "-remail")      {$LOAD_XBOARD = 0            ;
					   $SEND_MAIL   = 1            ;
					   $REMAIL      = 1            ;}
	elsif ("$ARGV" eq "-game")	  {$PGN_GAME    = shift        ;}
	elsif ("$ARGV" eq "-games")       {$NUM_GAMES   = shift        ;}
	elsif ("$ARGV" eq "-wgames")      {$NUM_WGAMES  = shift        ;}
	elsif ("$ARGV" eq "-bgames")      {$NUM_BGAMES  = shift        ;}
	elsif ("$ARGV" eq "-me")	  {$MY_NNAME    = shift        ;}
	elsif ("$ARGV" eq "-opp")	  {$OPP_NNAME   = shift        ;}
	elsif ("$ARGV" eq "-myname")	  {$MY_FNAME    = shift        ;}
	elsif ("$ARGV" eq "-oppname")	  {$OPP_FNAME   = shift        ;}
	elsif ("$ARGV" eq "-wname")	  {$WHITE_FNAME	= shift	       ;}
	elsif ("$ARGV" eq "-bname")	  {$BLACK_FNAME	= shift	       ;}
	elsif ("$ARGV" eq "-myna")	  {$MY_ADDRESS  = shift        ;}
	elsif ("$ARGV" eq "-oppna")	  {$OPP_ADDRESS = shift        ;}
	elsif ("$ARGV" eq "-wna")	  {$WHITENA	= shift	       ;}
	elsif ("$ARGV" eq "-bna")	  {$BLACKNA	= shift	       ;}
	elsif ("$ARGV" eq "-dir")	  {$CMAILDIR    = shift        ;}
	elsif ("$ARGV" eq "-arcdir")	  {$ARCDIR      = shift        ;}
	elsif ("$ARGV" eq "-mailprog")	  {$MAILPROG    = shift        ;}
	elsif ("$ARGV" eq "-logFile")     {$LOGFILE     = shift        ;}
	elsif ("$ARGV" =~ /^-(td|timeDelay)$/)
	                                  {@TD_FLAGS    = ($ARGV,
							   shift)      ;}
	elsif ("$ARGV" =~ /^-noChessComputer$/)
	                                  {@NCP_FLAGS   = ($ARGV,
							   shift)      ;}
	elsif ("$ARGV" =~ /^-[x]?ncp$/)	  {@NCP_FLAGS	= ($ARGV)      ;}
	elsif ("$ARGV" eq "-event")	  {$PGN_EVENT	= shift        ;}
	elsif ("$ARGV" eq "-site")	  {$PGN_SITE	= shift        ;}
	elsif ("$ARGV" eq "-round")	  {$PGN_ROUND	= shift        ;}
	elsif ("$ARGV" eq "-mode")	  {$PGN_MODE	= shift        ;}
	elsif ("$ARGV" =~ /^-/ || $UNREC) {
	    push(@UNREC_ARGS, $ARGV) ;
	    $UNREC = 1 ;
	} else {
	    die("cmail: Unrecognised flag \"$ARGV\"\nUsage: $USAGE\n") ;
	}
    }

    ## ################################################################# ##
    ## Assign a value to $ARCDIR if not specified on the command line
    ## ################################################################# ##

    $ARCDIR = $ENV{'CMAIL_ARCDIR'} unless ($ARCDIR) ;
    $ARCDIR = $CMAILDIR unless ($ARCDIR) ;
    $ENV{'CMAIL_ARCDIR'} = $ARCDIR ; ## Make sure this is set for xboard

    ## ################################################################# ##
    ## Propagate some CMAIL variables through xboard to the cmail
    ## grandchild so that it uses the same important variables as this one
    ## ################################################################# ##

    $ENV{'CMAIL_MAILPROG'} = $MAILPROG ;
    $ENV{'CMAIL_DIR'}      = $CMAILDIR ;
    $ENV{'CHESSDIR'}       = $CMAILDIR ; ## Make xboard use $CMAILDIR
    $ENV{'CMAIL_ARCDIR'}   = $ARCDIR ;
    if ($LOGFILE) {
	$ENV{'CMAIL_LOGFILE'}  = $LOGFILE ;
    } else {
	$LOGFILE = "&STDERR" ;
    }

    ## ################################################################# ##
    ## Work out how many games of each colour will be played
    ## ################################################################# ##

    die "cmail: Illegal number of games: $NUM_GAMES\n"
	if ($NUM_GAMES < 0) ;
    die "cmail: Illegal number of white games: $NUM_WGAMES\n"
	if ($NUM_WGAMES < 0) ;
    die "cmail: Illegal number of black games: $NUM_BGAMES\n"
	if ($NUM_BGAMES < 0) ;
    if ("$NUM_GAMES" ne "?") {
	if ("$NUM_WGAMES" eq "?") {
	    if ("$NUM_BGAMES" eq "?") {
		$NUM_BGAMES = int($NUM_GAMES / 2) ;
	    }
	    $NUM_WGAMES = $NUM_GAMES - $NUM_BGAMES ;
	} elsif ("$NUM_BGAMES" eq "?") {
	    $NUM_BGAMES = $NUM_GAMES - $NUM_WGAMES ;
	}
    } elsif ("$NUM_WGAMES" eq "?") {
	if ("$NUM_BGAMES" eq "?") {
	    $NUM_GAMES  = 1 ;
	    $NUM_WGAMES = 1 ;
	    $NUM_BGAMES = 0 ;
	} else {
	    $NUM_GAMES  = $NUM_BGAMES ;
	    $NUM_WGAMES = 0 ;
	}
    } else {
	if ("$NUM_BGAMES" eq "?") {
	    $NUM_GAMES  = $NUM_WGAMES ;
	    $NUM_BGAMES = 0 ;
	} else {
	    $NUM_GAMES  = $NUM_WGAMES + $NUM_BGAMES ;
	}
    }
    die "cmail: Illegal number of games: $NUM_GAMES\n"
	if ("$NUM_GAMES" eq "0") ;
    die (  "cmail: Inconsistent numbers of games specified:"
	 . " $NUM_WGAMES + $NUM_BGAMES != $NUM_GAMES\n")
	unless ($NUM_GAMES == $NUM_WGAMES + $NUM_BGAMES) ;
}
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#


#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
## Initialisation of variables and environment
sub showGPL {
    ## ################################################################# ##
    ## Show copyright notice
    ## ################################################################# ##

    while (<DATA>) {
	last if (/^{END OF GPL COPYRIGHT}$/) ;
	s/\$Revision[:] (.*) \$/$1/ ;
	print ;
    }

    ## ################################################################# ##
    ## Show conditions if requested
    ## ################################################################# ##

    while (<DATA>) {
	last if (/^{END OF GPL CONDITIONS}$/) ;
	print if ($SHOWW) ;
    }

    ## ################################################################# ##
    ## Show warranty if requested
    ## ################################################################# ##

    if ($SHOWC) {
	print "\n" if ($SHOWW) ;
	print while (<DATA>) ;
    }

    
    exit 0 if ($SHOWC || $SHOWW) ; ## Abort if showed conditions or warranty
}
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#


#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
## Initialisation of variables and environment
sub initialise {
    local ($p) = "[.PRNBQKprnbqk]" ;
    local ($l) = "$p $p $p $p $p $p $p $p\n" ;
    local ($board) = "$l$l$l$l$l$l$l$l" ;
    local ($tp) = ".* to play\n" ;
    $posdiag = "\{--------------\n$board$tp--------------\}\n+" ;

    &parse_flags (@ARGV) ;	## Parse command-line arguments

    &showGPL unless $QUIET ;

    open (tty, "< /dev/tty") ;	## Open tty for reading

    &need_chess_dir () ;	## Check for the existence of CMAILDIR

    open (logfile, ">$LOGFILE") if ($DEBUG) ; ## Default is STDERR

    &debug ("Called <initialise>\n") ;
}
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#


#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
## Prompt for a game name, if <cr> use a default
sub prompt_for_game_name {
    &debug ("Called <prompt_for_game_name>\n") ;
    local ($old) = select ;	## Remember the selected output
    select (stdout);		## Prompt goes to stdout
    $| = 1 ;			## Keep it flushed

    print "Game name [<cr> to use default]: " ;
    die "cmail: tty not open\n" unless (-t) ;
    <tty> =~ /(.*)/ ;		## Read line from tty
    $PGN_GAME = "$1" ;		## Assign to game name
    
    select ($old) ;		## Re-select the old output
}
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#


#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
## Prompt for opponent's address
sub prompt_for_opp_address {
    &debug ("Called <prompt_for_opp_address>\n") ;
    local ($old) = select ;	## Remember the selected output
    select (stdout);		## Prompt goes to stdout
    $| = 1 ;			## Keep it flushed

    ## ################################################################# ##
    ## Prompt for opponent's email address
    ## ################################################################# ##

    print "Opponent's email address: " ;
    die "cmail: tty not open\n" unless (-t) ;
    <tty> =~ /(.*)/ ;
    $OPP_ADDRESS = $1 ;

    ## ################################################################# ##
    ## Use name as default if still blank
    ## ################################################################# ##

    $OPP_ADDRESS = $OPP_NNAME if ("" eq $OPP_ADDRESS) ;
    
    select ($old) ;		## Re-select the old output
}
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#


#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
## Prompt for opponent's name
sub prompt_for_opp_name {
    &debug ("Called <prompt_for_opp_name>\n") ;
    local ($old) = select ;	## Remember the selected output
    select (stdout) ;		## Prompt goes to stdout
    $| = 1 ;			## Keep it flushed
    print "Opponent's name: " ;

    die "cmail: tty not open\n" unless (-t) ; ## Check tty is open
    <tty> =~ /(.*)/ ;		## Read line from tty
    $OPP_NNAME = $1 ;		## Match!
    die "cmail: Can't proceed without the opponent's name.\n"
	unless ($OPP_NNAME) ;

    select ($old) ;		## Re-select the old output
}
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#


#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
## Prompt for move
sub prompt_for_move {
    local ($prompt) = shift ;
    local ($pattern) = shift ;
    &debug ("Called <prompt_for_move>\n") ;

    local ($move) = "" ;
    local ($old) = select ;	## Remember the selected output
    select (stdout) ;		## Write to stdout
    $| = 1 ;			## Keep it flushed
    die "cmail: tty not open\n" unless (-t) ; ## Check tty is open

    do {
	print $prompt ;
	<STDIN> =~ /(.*)/ ;	## Read line from tty
	$move = $1 ;		## Match!
    } until ($move =~ /^$pattern$/) ;

    select ($old) ;		## Re-select the old output
    return ($move) ;
}
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#


#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
## Load the game
sub play_game {
    &debug ("Called <play_game>\n") ;

    $| = 1 ;			## Start flushing output buffer

    ## ################################################################# ##
    ## Load xboard unless inhibited by command-line arguments
    ## ################################################################# ##

    if (($STARTING_NEW_GAME) && ($NUM_WGAMES == 0)) {
	print (  "Bypassing xboard and mailing $NUM_BGAMES empty"
	       . " black games.\n") ;
    } elsif ($LOAD_XBOARD) {
	## ############################################################# ##
	## Remove output file from previous run, but preserve
	## $PGN_GAME.game.out.* because they will be empty black games
	## ############################################################# ##

	unlink "$PGN_GAME.out" ;

	## ############################################################# ##
	## Invoke xboard with loads of flags
	## ############################################################# ##

	if ($PGN_GAME) {
	    if (@ARCHIVE) {
		local ($date) = &get_date_from_games (@ARCHIVE) ;
		$XBOARD_ARGS = join (' ', (("-lgf",
					    "'$ARCDIR/$PGN_GAME.$date.archive'"),
					   @NCP_FLAGS,
					   "-xics",
					   @TD_FLAGS,
					   @DEBUG_FLAGS,
					   @UNREC_ARGS)) ;
	    } else {
		$XBOARD_ARGS = join (' ', (("-cmail", "'$PGN_GAME'"),
					   @TD_FLAGS,
					   @NCP_FLAGS,
					   "-xics",
					   @DEBUG_FLAGS,
					   @UNREC_ARGS)) ;
	    }
	} else {
	    $PGN_GAME = "unknown.cmail" ;
	    $XBOARD_ARGS = join (' ', (("-lgf", "'$PGN_GAME'"),
				       @NCP_FLAGS,
				       "-xics",
				       @TD_FLAGS,
				       @DEBUG_FLAGS,
				       @UNREC_ARGS)) ;
	    $REUSE = 0 ;
	}

	$LOG_FILE = "$PGN_GAME.log" ;
	&debug ("Invoking xboard with args: $XBOARD_ARGS\n") ;
	$PID_FILE = "$PGN_GAME.pid" ;
	if (   (! $REUSE)
	    || (! (   (-f $PID_FILE)
		   && ($XBOARD_PID = `cat '$PID_FILE'`)
		   && ("$XBOARD_PID" =~ /^\d+$/)
		   && (kill "SIGUSR1", $XBOARD_PID)))) {
	    print "Loading xboard for game \"$PGN_GAME\"..." ;
#  	    system ("gdb xboard") ;
	    system (  "{ ({ xboard $XBOARD_ARGS & } ;"
		    . "   echo \$! > '$PID_FILE' ;"
		    . "   wait ;"
		    . "   rm '$PID_FILE') & } >'$LOG_FILE' 2>&1") ;
	    print (  "done.\n"
		   . "If nothing happens look for an error message in\n"
		   . "$CMAILDIR/$LOG_FILE\n") ;
	} else {
	    print ("Revived existing xboard for game \"$PGN_GAME\".\n"
		   . "If nothing happens"
		   . " remove $CMAILDIR/$PID_FILE and try again.\n") ;
	}

	return 1 ;
    }
    return 0 ;
}
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#


#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
## Enter moves on tty instead of xboard
sub play_on_tty {
    &debug ("Called <play_on_tty>\n") ;

    local (@results) = @_ ;

    local (@games, $game, $to_play) ;

    ## ################################################################# ##
    ## Check we have access to tty
    ## ################################################################# ##

    if (open(STDIN, '/dev/tty')) {
	## ############################################################# ##
	## Read in games
	## ############################################################# ##
	
	local ($infile) = "$PGN_GAME.game.in" ;
	if (-f $infile) {
	    @games = &get_games_from_file($infile) ;
	    &debug ("Read in games from \"$infile\"\n") ;

	    if (@results) {
		foreach $game (@games) {
		    $result = shift (@results) ;
		    if ($result && ($game =~ /\[Result\s+"[^*]+"\]/)) {
			$game = "" ;
		    }
		}
	    }

	    ## ################################################################# ##
	    ## Accept move on tty for each game in turn
	    ## ################################################################# ##
		
	    local ($comment_orig, $comment, $comment_line,
		   $pos, $move, $normal_move, $action, $result) ;

	    foreach $game (@games) {
		next unless $game ;

		$game =~ s/[\s\n]*[*]?[\s\n]*$// ;
		$pos = "" ;
		$pos = $1 if ($game =~ s/($posdiag)//) ;
		print $game, "\n\n", $pos ;

		($number, $to_play) = &get_to_play ($game) ;

		$comment_orig = "" ;
		$game_nocomment = $game ;
		if ($game_nocomment =~ s/\n?{\n?([^{]*)}$//) {
		    $comment_orig = $1 ;
		    $comment_orig =~ s/([^\n])$/$1\n/ ;
#  		    &debug ("Comment is:\n{\n$comment_orig}\n") ;
		}
		if ($game =~ /\[Result\s+"[^*]+"\]/) {
		    &prompt_for_move ("Game finished, press \"Return\" to continue: ", "") ;
                    next ;
		}

		$tmpgame = $game_nocomment ;
		$comment = $comment_orig ;

	      outer:
		while (1) {
		    if ($game =~ /{\n?.* offers a draw\n?}$/) {
			$move = &prompt_for_move ("Enter move [MOVE/(r)esign/(a)ccept/(c)omment/re(t)ry]: ",
						  "([-a-h0-9PRNBQK][-a-h0-9PRNBQK]+|[ract])") ;
		    } else {
			$move = &prompt_for_move ("Enter move [MOVE/(r)esign/(c)omment/re(t)ry]: ",
						  "([-a-h0-9PRNBQK][-a-h0-9PRNBQK]+|[rct])") ;
		    }
		    $normal_move = 0 ;
		    $result = "" ;
		    if ($move =~ /^\s*r\s*$/i) {
			if ($to_play eq "White") {
			    $result = "0-1" ;
			} else {
			    $result = "1-0" ;
			}
			$move = "\n{$to_play resigns} $result" ;
		    } elsif ($move =~ /^\s*a\s*$/i) {
			$move = "\n{Draw agreed} $result" ;
		    } elsif ($move =~ /^\s*c\s*$/i) {
                        while ($comment_line = &prompt_for_move ("Enter comment: ", ".*")) {
			    $comment .= $comment_line . "\n" ;
                        }
			next ;
		    } elsif ($move =~ /^\s*t\s*$/i) {
			print $game, "\n\n", $pos ;
			$tmpgame = $game_nocomment ;
			$comment = $comment_orig ;
			print "Try again.\n" ;
			next ;
		    } else {
			$normal_move = 1 ;
		    }

		    $tmpgame .= "\n{\n" . $comment . "}" if ($comment) ;
		    if (! $normal_move) {
			$tmpgame .= "$move"  ;
		    } elsif ($to_play eq "White") {
			$tmpgame .= "\n$number. $move"  ;
		    } elsif ($tmpgame =~ /}$/) {
			$tmpgame .= "\n$number. ... $move"  ;
		    } else {
			$tmpgame .= " $move"  ;
		    }

		    $tmpgame =~ s/\[Result\s+"(.*)"\]/[Result "$result"]/ if ($result) ;
		    $comment = "" ;
		  middle:
		    while (1) {
			if ($normal_move) {
			    $action = &prompt_for_move ("Enter action [(d)raw/(c)omment/(s)end/re(t)ry]: ",
							"[dcst]") ;
			} elsif ($result) {
			    $action = &prompt_for_move ("Enter action [(s)end/re(t)ry]: ",
							"[st]") ;
			} else {
			    $action = &prompt_for_move ("Enter action [(c)omment/(s)end/re(t)ry]: ",
							"[cst]") ;
			}
			if ($action =~ /^\s*d\s*$/i) {
			    if ($normal_move) {
				$comment .= "$to_play offers a draw\n" ;

				while (1) {
				    $action = &prompt_for_move ("Enter action [(c)omment/(s)end/re(t)ry]: ",
								"[cst]") ;
				    if ($action =~ /^\s*c\s*$/i) {
					while ($comment_line = &prompt_for_move ("Enter comment: ", ".*")) {
					    $comment .= $comment_line . "\n" ;
                        		}
				        next ;
				    } elsif ($action =~ /^\s*t\s*$/i) {
					print $game, "\n\n", $pos ;
					$tmpgame = $game_nocomment ;
					$comment = $comment_orig ;
					print "Try again.\n" ;
					next outer;
				    } elsif ($action =~ /^\s*s\s*$/i) {
					$tmpgame .= "\n{\n" . $comment . "}" ;
					last middle ;
				    }
				}
			    } else {
				print "You can't offer a draw at this point.\n" ;
				next ;
			    }
			} elsif ($action =~ /^\s*c\s*$/i) {
			    if ($result) {
				print "You can't enter a comment after the game is finished.\n" ;
			    } else {
				while ($comment_line = &prompt_for_move ("Enter comment: ", ".*")) {
				    $comment .= $comment_line . "\n" ;
                        	}
			    }
			    next ;
			} elsif ($action =~ /^\s*t\s*$/i) {
			    print $game, $pos ;
			    $tmpgame = $game_nocomment ;
			    $comment = $comment_orig ;
			    print "Try again.\n" ;
			    next outer;
			} elsif ($action =~ /^\s*s\s*$/i) {
			    $tmpgame .= "\n{\n" . $comment . "}\n" if ($comment) ;
			    last ;
			}
		    }

		    last ;
		}

		$tmpgame .= "\n*" if ($normal_move) ;
		$tmpgame .= "\n\n" ;
		$game = $tmpgame ;
	    }
	} else {
	    die "cmail: No games to be read\n" ;
	}
    } else {
	die "cmail: Can't open tty" ;
    }

    return (@games) ;
}
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#


#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
## Find the game the user wants by any means possible
sub find_game {
    &debug ("Called <find_game>\n") ;

    ## ################################################################# ##
    ## Ask user for a game name if not already known
    ## ################################################################# ##

    &prompt_for_game_name () if ("" eq "$PGN_GAME") ;

    ## ################################################################# ##
    ## Find out opponent's details
    ## ################################################################# ##

    if ("" eq "$PGN_GAME") {
	## ############################################################# ##
	## Failed to find the game name so construct a default from players
	## ############################################################# ##
	
	&prompt_for_opp_name () if ("" eq "$OPP_NNAME") ; ## Ask user
	die "cmail: Can't proceed without your opponent's short name (-opp)\n"
	    if ("" eq "$OPP_NNAME") ;
	die "cmail: Can't proceed without your own short name (-me)\n"
	    if ("" eq "$MY_NNAME") ;
	if ($NUM_WGAMES > 0) {
	    $PGN_GAME = "$MY_NNAME-vs-$OPP_NNAME" ; ## Construct default
	} else {
	    $PGN_GAME = "$OPP_NNAME-vs-$MY_NNAME" ; ## Construct default
	}
    } elsif (("" eq "$OPP_ADDRESS") && ("" ne "$RETURN_ADDRESS")) {
	$OPP_ADDRESS = $RETURN_ADDRESS ; ## Use return address instead
	&debug (  "Using return address \"$OPP_ADDRESS\""
		. " for opponent address\n") ;
    }

    ## ################################################################# ##
    ## If no $PGN_GAME.game.in file, assume we're starting a new game
    ## ################################################################# ##

    &start_new_game () unless (-f "$PGN_GAME.game.in") ;
}
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#


#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
## Get the date
sub get_date {
    local ($the_time) = time ;
    local ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
	localtime ($the_time) ;
    $mon ++ ;
    $year += 1900 ;
    if ($mon < 10) {
	$mon = "0$mon" ;
    }
    if ($mday < 10) {
        $mday = "0$mday" ;
    }
    "$year.$mon.$mday" ;
}
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#


#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
## Start a new game
sub start_new_game {
    print (  "Starting new game"
	   . " -- $NUM_WGAMES as white, $NUM_BGAMES as black.\n") ;
    local ($to_play) = "white" ;
    $move_num = 0 ;
    $STARTING_NEW_GAME = 1 ;

    local (@game) ;

    ## ################################################################# ##
    ## Ask user for opponent's email address if unknown
    ## ################################################################# ##

    &prompt_for_opp_address () if ("" eq "$OPP_ADDRESS") ;

    ## ################################################################# ##
    ## Give up if we haven't got anywhere to send a move to
    ## ################################################################# ##

    die "cmail: Can't proceed without your opponent's email address.\n"
	if ("" eq "$OPP_ADDRESS") ;

    ## ################################################################# ##
    ## Create an empty game file
    ## ################################################################# ##

    open (GAMEFILE, "> $PGN_GAME.game.in") ;
    for ($j = 1; $j <= $NUM_GAMES; $j ++) {
	$PW_GCOS      =  &get_pw_gcos () ;

	$PGN_MYCOL    =  $MY_FNAME ;
	$PGN_MYCOL    =  $PW_GCOS unless $PGN_MYCOL ;
	$PGN_MYCOL    =  $MY_NNAME unless $PGN_MYCOL ;
	$PGN_MYCOLNA  =  $MY_ADDRESS ;
	$PGN_MYCOLNA  =  "?" unless ($PGN_MYCOLNA) ;
				    
	$PGN_OPPCOL   =  $OPP_FNAME ;
	$PGN_OPPCOL   =  "?" unless ($PGN_OPPCOL) ;
	$PGN_OPPCOLNA =  $OPP_ADDRESS ;
	$PGN_OPPCOLNA =  "?" unless ($PGN_OPPCOLNA) ;
	
	if ($j > $NUM_WGAMES) {
	    $PGN_WHITE   = $PGN_OPPCOL ;
	    $PGN_BLACK   = $PGN_MYCOL ;
	    $PGN_WHITENA = $PGN_OPPCOLNA ;
	    $PGN_BLACKNA = $PGN_MYCOLNA ;
	} else {
	    $PGN_WHITE   = $PGN_MYCOL ;
	    $PGN_BLACK   = $PGN_OPPCOL ;
	    $PGN_WHITENA = $PGN_MYCOLNA ;
	    $PGN_BLACKNA = $PGN_OPPCOLNA ;
	}
	
    	## ######################################################### ##
	## If we only have one colour of game then allow command-line 
	## colour specs to override
	## ######################################################### ##
	
	if (! ($NUM_WGAMES && $NUM_BGAMES)) {
	    $PGN_WHITE   = $WHITE_FNAME if ($WHITE_FNAME) ;
	    $PGN_BLACK   = $BLACK_FNAME if ($BLACK_FNAME) ;
	    $PGN_WHITENA = $WHITENA     if ($WHITENA)     ;
	    $PGN_BLACKNA = $BLACKNA     if ($BLACKNA)     ;
	}
	
	$PGN_DATE = &get_date () ;
	$PGN_DATE = "?" unless ($PGN_DATE) ;

	if ($NUM_GAMES > 1) {
	    $SUFFIX = ".$j" ;
	} else {
	    $SUFFIX = "" ;
	}
	@game = ("[Event \"$PGN_EVENT\"]\n",
		 "[Site \"$PGN_SITE\"]\n",
		 "[Date \"$PGN_DATE\"]\n",
		 "[Round \"$PGN_ROUND\"]\n",
		 "[White \"$PGN_WHITE\"]\n",
		 "[Black \"$PGN_BLACK\"]\n",
		 "[Result \"*\"]\n",
		 "[WhiteNA \"$PGN_WHITENA\"]\n",
		 "[BlackNA \"$PGN_BLACKNA\"]\n",
		 "[Mode \"$PGN_MODE\"]\n",
		 "[CmailGameName \"$PGN_GAME$SUFFIX\"]\n\n*\n") ;
	if ($j > $NUM_WGAMES) {
	    open (GAMEOUTFILE, "> $PGN_GAME.game.out.$j") ;
	    print GAMEOUTFILE @game ;
	    close (GAMEOUTFILE) ;
	} else {
	    print GAMEFILE @game ;
	}
    }
    close (GAMEFILE) ;
}
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#


#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
## Get the password file gcos (full name) entry
sub get_pw_entry {
    local ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) =
	getpwuid ($<);
    ($name, $gcos) ;
}
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#


#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
## Get password file gcos (full name) entry
sub get_pw_gcos {
    local ($PW_GCOS) ;
    if (! $PW_GCOS) {
	($dummy, $PW_GCOS) = &get_pw_entry () ;
	$PW_GCOS =~ s/^\s*([^,()]+[^ ,()])[ ]*[,()].*$/$1/;
	if ($PW_GCOS =~ /^([^,()]+)\s+([^\s,()]+)$/) { ## Multi-word name
	    $PW_GCOS = $2 . ", " . $1 ;
	} elsif ($PW_GCOS !~ /^([^\s,()]+)/) { ## No sensible gcos entry 
	    $PW_GCOS = "" ;
	}			## Else leave it as one word
	&debug ("PW full name is \"$PW_GCOS\"\n");
    }
    return $PW_GCOS ;
}
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#


#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
## Get password file user name entry
sub get_pw_name {
    local ($PW_NAME) ;
    ($PW_NAME, $dummy) = &get_pw_entry () ;
    &debug ("PW name is $PW_NAME\n");

    return $PW_NAME ;
}
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#


#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
## Analyse the email message
sub analyse_email_message {
    local ($new_result, $delete_result, $unfinished, $finished)
	= (0, 0, 0, 0) ;
    local ($gamefile, @games) ;

    ## ################################################################# ##
    ## Slurp the mail message
    ## ################################################################# ##

    die "cmail: No games on standard input" 
	unless (@games = &get_games_from_file ("STDIN")) ;

    ## ################################################################# ##
    ## Is the message a real cmail message or just a chess game? $PGN_GAME
    ## will have been set by calling get_games_from_file if it's real
    ## ################################################################# ##

    print "Processing game message..." ;

    if ($PGN_GAME) {
	## ############################################################# ##
	## Restore the results file from the archive if
	## necessary. This is helpful if the user pipes in an old
	## message for take-back purposes or whatever.
	## ############################################################# ##

	if (! -f "$PGN_GAME.res") {
	    ## Find what date would have been used to create the archive
	    foreach (@games) {
		if (/\[Date\s"(.*)"\]/) {
		    $date = $1 ;
		    last ;	## Assume all dates are the same
		}
	    }
	    ## Restore results file from archive directory if it exists
	    if ($date && (-f "$ARCDIR/$PGN_GAME.$date.archive")) {
		if (system ("cp",
			    "$ARCDIR/$PGN_GAME.$date.archive",
			    "$PGN_GAME.res")) {
		    print stderr (  "\nWarning: couldn't restore results file"
				  . " from archive\n") ;
		} else {
		    print "restored results file from archive..." ;
		}
	    }
	}

	## ############################################################# ##
	## Find existing results, if any.
	## ############################################################# ##

	local (@results) = &get_games_from_file ("$PGN_GAME.res") ;

	## ############################################################# ##
	## Parse each game
	## ############################################################# ##

	foreach $game (@games) {
	    next unless ($game) ;

	    ($game_name, $game_num) = &get_game_name_and_number ($game) ;

	    $result = 0 ;
	    @game = split("\n", $game) ;
	    foreach (@game) {
		if (/^\[(Black|White)\s*"[?]"\]$/) {
		    $colour = $1;
		    $PW_GCOS = &get_pw_gcos () unless ($PW_GCOS) ;
		    $PW_GCOS = "$MY_NNAME" unless ($PW_GCOS) ;
		    s/".*"/"$PW_GCOS"/ ;
		    &debug ("Changed $colour tag to be $_") ;
		} elsif (/^\[((Black|White)NA)\s*"(.*)"\]$/) {
		    $NA = $3 ;
		    if ($NA eq "?") {
			if ($RETURN_ADDRESS) {
			    $NA = $RETURN_ADDRESS ;
			} else {
			    $NA = "??" ;
			}
			$_ = "[$1 \"$NA\"]" ;
			&debug ("Changed $1 tag.\n") ;
		    }
		    if ($2 eq "White") {
			$PGN_WHITENA = $NA ;
			&debug ("WhiteNA tag is \"$PGN_WHITENA\"\n") ;
		    } else {
			$PGN_BLACKNA = $NA ;
			&debug ("BlackNA tag is \"$PGN_BLACKNA\"\n") ;
		    }
		} elsif (/\[Result\s*"(.*)"\]$/) {
		    if ($1 ne "*") {
			$result = 1 ;
			$finished ++ ;
		    } else {
			$unfinished ++ ;
		    }
		} elsif (/^(.*[^\d]+)?\d+[.]\s*([^\s*]*\s+)?[^\s.*]+(\s*\d+[.]\s*)?[\s*]*$/) {
		    if ($2) {
			$to_play = "white" ;
		    } else {
			$to_play = "black" ;
		    }
		    &debug ("$to_play to play\n") ;
		}
	    }

	    ## ######################################################### ##
	    ## Reconstruct possibly edited game
	    ## ######################################################### ##

	    $game = join ("\n", @game) . "\n\n" ;

	    ## ######################################################### ##
	    ## Build up results array
	    ## ######################################################### ##

	    if ($result) {
		$results[$game_num] = $games[$game_num] ;
		$new_result = 1 ;
	    } elsif ($results[$game_num]) {
		## Deleting a result does actually make sense if the user
		## pipes in an old message for take-back purposes or whatever
		$results[$game_num] = "" ;
		$delete_result = 1 ;
	    }

	    ## ######################################################### ##
	    ## Remove old .out files
	    ## ######################################################### ##

	    unlink <$PGN_GAME.game.out.*> ;

	    ## ######################################################### ##
	    ## Write ongoing games to game file and append new results
	    ## ######################################################### ##

	    die   "cmail: Can't open file for writing:"
		. " \"$CMAILDIR/$PGN_GAME.game.in\"\n"
		unless open (gamefile, ">$PGN_GAME.game.in") ;
	    &debug (@games) ;
	    print gamefile @games ;
	    close (gamefile) ;
	}

	## ############################################################# ##
	## Print how many finished/unfinished games were found
	## ############################################################# ##

	printf ("%d unfinished %s and %d finished %s...",
		$unfinished, ($unfinished == 1) ? "game" : "games",
		$finished, ($finished == 1) ? "game" : "games") ;

	## ############################################################# ##
	## Write results back to results file if there were any results
	## in the input
	## ############################################################# ##
	
	if ($new_result || $delete_result) {
	    die (  "cmail: Can't open results file for writing:"
		 . "\"$CMAILDIR/$PGN_GAME.res\"\n")
		unless open (resfile, ">$PGN_GAME.res") ;
	    print resfile @results ;
	    close (resfile) ;
	}

	## ############################################################# ##
	## Archive results if there are no unfinished games
	## ############################################################# ##
	
	@ARCHIVE = @results unless ($unfinished) ;

	## ############################################################# ##
	## Figure out return address if not known
	## ############################################################# ##

	if (! $RETURN_ADDRESS) {
	    if ($to_play eq "black") {
		$RETURN_ADDRESS = $PGN_WHITENA unless ("$PGN_WHITENA" eq "?") ;
	    } else {
		$RETURN_ADDRESS = $PGN_BLACKNA unless ("$PGN_BLACKNA" eq "?") ;
	    }
	}

	## ############################################################# ##
	## Decide to include position diagrams in output if not already
	## decided and a position diagram was found in the input
	## ############################################################# ##

	if ("$OUTPUT_POS" eq "") {
	    if (grep (/$posdiag/, @games)) {
		$OUTPUT_POS = "y" ; # Output position only if it was input
	    } else {
		$OUTPUT_POS = "n" ;
	    }
	}
	$ENV{'CMAIL_OUTPUT_POS'} = $OUTPUT_POS ;

	## ############################################################# ##
	## Check that we have enough info about the players to continue
	## ############################################################# ##

	&find_game () ;
    } else {
	## ############################################################# ##
	## Set up xboard for viewing non-cmail PGN file
	## ############################################################# ##

	local ($file) = "unknown.cmail" ;
	print "done.\nDumping non-cmail file into $CMAILDIR/$file..." ;

	die "cmail: Can't open file for writing: \"$CMAILDIR/$file\"\n"
	    unless open (gamefile, ">$file") ;
	print gamefile @games ;
	close (gamefile) ;
    }

    print "done.\n" ;
}
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#


#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
## Sort two filenames by the numeric suffix
sub sort_by_numeric_suffix {
    $a =~ /[.](\d+)$/ ; local ($na) = $1 ;
    $b =~ /[.](\d+)$/ ; local ($nb) = $1 ;

    return ($na <=> $nb) ;
}
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#


#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
sub get_game_name_and_number {
    local ($game) = join ("\n", @_) ;

    local ($game_name, $game_num) ;

    die "CMailGame                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                