#!/usr/bin/perl -w
###################################################################
# Program: fixup-book.pl
#    Desc:
# 
#  This script should be run on a raw book file that is
#  formatted as follows...
#
#       Nc3 Nf6 Nf3 g6 d4 d6 e4 Bg7 Be2 o-o h3 c5 dc
#       Nc3 Nf6 Nf3 g6 e4 d6 d4 Bg7 Be2 o-o Bf4 Nc6 d5 e5 
#       Nc3 Nf6 d3 d5 Nf3 g6 Bg5 Bg7 e4 de de o-o Be2 Nc6 
#          
#  This script truncates lines that are over MAX moves long 
#  (MAX can be set when the program is invoked.  It also 
#  completely eliminates lines that are less than MIN 
#  number of moves.
#          
# History:
#   18Aug99     THL    1.0  Initial Version
###################################################################

use strict;     # Force explicit declaration of variables.
use FileHandle;

# Save the name of the program.
my ($Prg)= $0;
my ($Inputfile, $Outputfile);
my ($Min, $Max) = (4, 20);

main:
{
    my (@file, @line, $i);
    
    # Do not buffer the output of STDOUT.
    STDOUT->autoflush(1);

    process_commandline();

    open(INFILE, "<$Inputfile") || 
	die "Unable to open input file $Inputfile\n";
    chomp(@file = <INFILE>);
    close(INFILE);

    open(OUTFILE, ">$Outputfile") || 
	die "Unable to open output file $Outputfile\n";
    
    foreach (@file) {
	$_ =~ s/o-o/O-O/g;
	$_ =~ s/0-0/O-O/g;
	$_ =~ s/-o/-O/g;
	$_ =~ s/-0/-O/g;
	@line = split;
	if (!defined($line[0])) { next; }
	if ($#line < $Min) { next; }
	for ($i=0; $i <= $#line; $i++) {
	    if ($i >= $Max) { last; }
	    if (0 == $i) {
	        print OUTFILE "\n$line[$i]";
	    } else {
		print OUTFILE " $line[$i]";
	    }
	}
    }
    close(OUTFILE);

    print "\nDone!\n\n";
}

###################################################################
# Subroutine definitions
###################################################################
sub process_commandline
{
    my ($usage);
    
    $usage = 
"\nUsage: $Prg [-max #] [-min #] -input <Inputfile> -output <Outputfile>

  This script should be run on a raw book file that is
  formatted as follows...

       Nc3 Nf6 Nf3 g6 d4 d6 e4 Bg7 Be2 o-o h3 c5 dc ...
       Nc3 Nf6 Nf3 g6 e4 d6 d4 Bg7 Be2 o-o Bf4 Nc6 d5 e5 ...
       Nc3 Nf6 d3 d5 Nf3 g6 Bg5 Bg7 e4 de de o-o Be2 Nc6 ...

  etc.
          
  This script truncates lines that are over MAX moves long 
  (MAX can be set when the program is invoked.  It also 
  completely eliminates lines that are less than MIN 
  number of moves.
\n";

    if ($#ARGV < 3) {
	print "$usage";
	exit(0);
    }
    while (@ARGV) {
        $_ = shift(@ARGV);
        /-h/ && do {
            print "$usage";
            exit(0);
        };    
	/-max/ && do {
	    $_ = shift(@ARGV);
	    $Max = $_;
        };
	/-min/ && do {
	    $_ = shift(@ARGV);
	    $Min = $_;
        };
	/-input/ && do {
	    $_ = shift(@ARGV);
	    if (-e $_) {
		$Inputfile = $_;
            }
        }; 
	/-output/ && do {
	    $_ = shift(@ARGV);
	    $Outputfile = $_;
        }; 
    }
    if (!defined($Inputfile) || !defined($Outputfile)) {
        print $usage;
        exit 0;
    }
    print "\n";
    print "Inputfile = $Inputfile\n";
    print "Outputfile= $Outputfile\n";
    print "Max length= $Max\n";
    print "Min length= $Min\n";
}





