#!/usr/bin/perl

#--------------------------------------------------------------------------------
# This scripts corrects detailed transcript files generated by GS prior to ver. 6.034
# (June 2024), which incorrectly computed objectCnt. In these older versions, the value
# of objectCnt was incorrectly decremented after successful picks (which, of course,
# don't actually remove pieces from the board).
#--------------------------------------------------------------------------------

use strict;

my $here = `pwd`;
$here =~ s/\s+$//;

($here =~ /\/detailed-transcripts$/ ) or die "This script is supposed to be run in a directory named 'detailed-transcripts'. Wrong directory: $here\n";

my $boardsDir = "../boards";

(-d $boardsDir) or die "Expected to find this directory, but did not: $boardsDir\n";

my $toDir = "../tmp";

(-d $toDir) or mkdir($toDir);
(-d $toDir) or die "Output directory does not exist and cannot be created: $toDir\n";

my @allFiles  = glob("*.detailed-transcripts.csv"); 

my $cnt = 0;
foreach my $fname (@allFiles) {
    &doFile($fname);
    $cnt ++;
}

print "Copied/modified $cnt files from $here to $toDir\n";

die;


sub doFile($) {
    my ($fname) = @_;

    my $s = $fname;
    $s =~ s/\.detailed-transcripts.csv/.boards.csv/;
    my $boardsFile = "$boardsDir/$s";
    my $outFile = "$toDir/$fname";

#    % cut -d , -f 2 ../boards/pbk2024MAR15b.boards.csv | uniq -c
#   1 #playerId
#     20 20240315-104832-ZS8PEI

    my %initSizes = ();

#    print "Process $fname --> $outFile\n";
#    print "Boards from $boardsFile\n";
    foreach my $line (`cut -d , -f 2 $boardsFile|uniq -c`) {
	$line =~ s/^\s*//;
	$line =~ s/\s$//;
	my ($cnt, $epi) = split(/\s+/, $line);
#	print "$epi: $cnt\n";
	$initSizes{$epi} = $cnt;
    }

# print "File=$fname\n";

    open(F, "<$fname");
    my @lines=<F>;
    close(F);

    open(G ,">$outFile") or "Cannot write to $outFile\n";
    
#  cut -d , -f 15- vm-2024-11-11-a.detailed-transcripts.csv
#playerId,trialListId,seriesNo,ruleId,episodeNo,episodeId,moveNo,timestamp,reactionTime,objectType,objectId,y,x,bucketId,by,bx,code,objectCnt
# 1 ...       11 12 13 14                   15 16 17 18
# playerID ....objectId, y,x,bucketId,  by,bx,code,objectCnt
# 0 ... ,..... 10 11 12 13                  14 15 16 17

    my $correctObjectCnt=0;


    my $prevEpi = "";
    
    for(my $lineNo = 0; $lineNo <= $#lines; $lineNo++) {
	my $line = $lines[$lineNo];
	$line =~ s/\s+$//;
	
	my @z = split( /,/, $line);
	if ($lineNo==0) {
	    ($z[17] eq 'objectCnt') or die "Unexpected header: $line";
	    ($z[10] eq 'objectId') or die "Unexpected header: $line";
	    ($z[5] eq 'episodeId') or die "Unexpected header: $line";
	    print G "$line\n";
	    next;
	}

	my $epi = $z[5];
	if ($epi ne $prevEpi) {
	    $correctObjectCnt = $initSizes{$epi};
	    (defined $correctObjectCnt) and  ($correctObjectCnt>0) or die "Don't know init size for epi=$epi\n";
	    $prevEpi = $epi;
	}
       
	my ($o, $y, $x, $b, $by,$bx,$code,$objectCnt) = @z[10..17];

	if ($code==0 && $b ne '') {	    #-- successful move
	    $correctObjectCnt --;
	}

	my $s = $line;
	$s =~ s/,-?[0-9]+$/,$correctObjectCnt/;

	if ($s ne $line) {
#	    print "[$lineNo] Was: $line\n[$lineNo] Now: $s\n";
	}
	$line = $s;

	print G "$line\n";
    }

    close(G) or die "Cannot close $outFile\n";

    #-- copy the time stamp of the file
    my ($READTIME, $WRITETIME) = (stat($fname))[8,9];
    utime($READTIME, $WRITETIME, $outFile);
    # die "Done $fname";
}
