#!/usr/bin/perl
# 2006-08-22 // entstanden aus beformat2.pl
# konvertiert: (durch beformat2.pl formatiertes) begaze-file --> eda-lesbares
#              begaze-file
#
# assert: input-file sollte durch beformat1.pl vorbehandelt sein.
#
# In jedem set:
# 1. image-filename bestimmen
# 2. image-onset finden; entspricht der MSG "run_exp_subject: showing trial"
#     -> User Event "DISPLAY_ON BMP\005eli.bmp" an diesem Zeitpunkt einfuegen
# 3. image-offset finden: MSG='run_exp_subject: --> got correct reaction' |
#     'run_exp_subject: --> got timeout'
#    -> User Event 'DISPLAY_OFF BMP\005eli.bmp' dort einfuegen
#
#
#

use Term::ReadKey;
use POSIX; #fuer strtol

#Variablen

my $done=0;


%IMG_MAPPING = (
		   0 => "vo",
		   1 => "li",
		   2 => "re",
		   3 => "hi",
		   4 => "vo",
		   5 => "li",
		   6 => "re",
		   7 => "hi",
		  );

#subs

sub showusage
{
    print STDERR "usage: $0 BEGAZE_FILE OUTPUT_FILE VPN KPN\n";
    exit 1;
}

###########################
#
#main
#
############################

#if ( "User Event	10	122	run_exp_subject: showing trial " !~ "^User Event\t([0-9]+)\t([0-9]+)\trun_exp_subject: showing trial")   
#      {
#	print STDERR "testX\n";
#      }
#exit 0;

showusage() if ( $#ARGV ne 3 );

#print STDERR "0: $ARGV[0]   1:$ARGV[1]";

#Open inputfiles
open(BEGAZE, "< $ARGV[0]")
    or die "beformat.pl: Couldn't open $ARGV[0]!\n";
open(OUT, "> $ARGV[1]")
    or die "beformat.pl: Couldn't open $ARGV[1]!\n";
my $vpn = strtol($ARGV[2]);
my $kpn = strtol($ARGV[3]);

$done=0;
my $pos_setstart;
LOOP: while (! $done)
  {
    #Jump to next "^Set" in Begaze-file
    chomp($line=<BEGAZE>);
    while ($line !~ /^Set/ )
      {
	print OUT "$line\n";
	chomp($line=<BEGAZE>);
      }

  SET_FOUND:
    $setline = $line;  #setline erst schreiben, wenn sicher, dass dieses
       #set nicht geskippt wird.
#    print OUT "$line\n";
#    print "setline: $line\n";
#    $aktset = $line =~ /Set\t([0-9]+)/; #setnumber extrahieren
    if ($line =~ /Set\t([0-9]+)/)
      {
	$aktset = $1; #setnumber extrahieren
      }

    print "Processing set $aktset\n";
    
    $pos_setstart = tell BEGAZE;  #Anfangsposition dieses sets merken

#User Event	9	2050	save_result: Nr: 0; ImgIndex: 2    

    #image-index in saveresult-message finden
    chomp($line=<BEGAZE>);
    while ($line !~ /^User Event\t[0-9]+\t[0-9]+\tsave_result: Nr: [0-9]+; ImgIndex: ([0-9]+)/ && $line !~ /^Set/)
      {
	chomp($line=<BEGAZE>);
      }
    if ($line =~ /^Set/)
      {
	print STDERR "  No save_result-event for setnum $aktset. Skipping this set!\n";
#	seek BEGAZE, $pos_setstart, 0;
#	next LOOP;
	goto SET_FOUND;
      }
    #dateinamen bestimmen
    if ($line =~ /^User Event\t[0-9]+\t[0-9]+\tsave_result: Nr: [0-9]+; ImgIndex: ([0-9]+)/)
      {
	$aktimgnum = $1;
      }
#    print  "  found aktimgnum=$aktimgnum\n";
    
    $side = $IMG_MAPPING{$aktimgnum};
    if ($aktimgnum < 4) {$filename = sprintf "jpg/%03de%s.bmp", $vpn,$side;}
    else { $filename = sprintf "jpg/%03dk%s.bmp", $kpn,$side;}

    print  "  Found image-file $filename\n";

    #zum Anfang des set zurueckspringen:
    seek BEGAZE, $pos_setstart, 0;

    #image-onset finden (ab hier alle gelesenen Zeilen wieder rausschreiben
    print OUT "$setline\n";
    chomp($line=<BEGAZE>);
    while ($line !~ "^User Event\t([0-9]+)\t([0-9]+)\trun_exp_subject: showing trial" && $line !~ "^Set")
#User Event	10	122	run_exp_subject: showing trial
      {
	print OUT "$line\n";
	chomp($line=<BEGAZE>);
      }
    if ($line =~ /^Set/)
      {
	print OUT "$line\n";
	print STDERR "  No showing-trial-event for setnum $aktset. Skipping this set.\n";
#	seek BEGAZE, $pos_setstart, 0;
#	next LOOP;
	goto SET_FOUND;
      }
#    print "found run_exp_trial: $line\n";
    ($setnum, $time) = $line =~ "^User Event\t([0-9]+)\t([0-9]+)\trun_exp_subject: showing trial";
    print OUT "$line\n";
    $newline="User Event\t" . "$setnum" . "\t" . "$time" . "\t" . "DISPLAY_ON $filename" . "\n";
    print OUT "$newline";
    print "  inserting 'DISPLAY_ON $filename'\n";

    #image-offset finden
    chomp($line=<BEGAZE>);
    while ($line !~ "^User Event\t([0-9]+)\t([0-9]+)\trun_exp_subject: --> got (timeout|correct reaction|wrong reaction)")
      {
	print OUT "$line\n";
	chomp($line=<BEGAZE>);
      }
    ($setnum, $time) = $line =~ "^User Event\t([0-9]+)\t([0-9]+)\trun_exp_subject: --> got (timeout|correct reaction|wrong reaction)";
    print OUT "$line\n";
    $newline="User Event\t" . "$setnum" . "\t" . "$time" . "\t" . "DISPLAY_OFF $filename" . "\n";
    print OUT "$newline";
    print "  inserting 'DISPLAY_OFF $filename'\n";
    
    #zum naechsten set springen
    chomp($line=<BEGAZE>);
    while ($line !~ /^Set\t([0-9]+)/ && defined($line))
      {
	print OUT "$line\n";
	chomp($line=<BEGAZE>);
      }
    if (!defined($line))  # wenn wegen EOF abgebrochen
      {
	last;  #fertig!
      }
    goto SET_FOUND;

}

close(BEGAZE);
close(OUT);

exit 0;

#EOF
