#!/usr/local/bin/perl5 
#© Ariadna Font Llitjos, 2006
# email: aria@cs.cmu.edu

#./PreProcessLogFiles.pl  InLogFile
# output: LogFile.processed

# Example: ./PreProcessLogFiles.pl ../IOFiles/2006-2-13-17-13-55-8336/1
# output: ../IOFiles/2006-2-13-17-13-55-8336/1.processed

# Reads in one log file at a time, and if detects spurious correction (more
# one submit field, it only stores the last one and prints it out to a file


$InFileName = $ARGV[0];
open(IN, "< $InFileName") || die "cannot open $InFileName for reading: $!\n";

# To Read the log file one paragraph at a time
$/ = ""; # set the input record separator $/ to the empty string ""
my $sub = 0;

while (<IN>) {
#    my @paragraph = ();
    @paragraph = (split /\n/, $_);
    foreach $line (@paragraph) {
	if ( $line =~ /submit[ \t]+=[ \t]+.*/ ){
	    $sub++;
	}
    }
}
# After done with the file, if there were more than one submit fields,
# print out the last one only
if ($sub > 1){
    $OutFileName = $InFileName . ".processed";
    open(OUT, "> $OutFileName") || die "cannot open $OutFileName for writting: $!\n";
    foreach $line (@paragraph) {
	print OUT $line, "\n";
    }	    
}







