#!/usr/local/bin/perl

if ($ARGV[0] =~ m/\.gz/) {
    open(LTC, "gunzip -c $ARGV[0] |") or die $!;
} else {
    open(LTC, "$ARGV[0]") or die $!;
}

$arccount = 0;

($fixed) = ($ARGV[0] =~ m/^(.+?)\.gz$/);

open(FIX, "> $fixed") or die $!;

while ($line = <LTC>) {
    if ($line =~ m/^\((\d+)\s+(\d+)\s+\"([^\"]*)\"\s+(\S+)\s+\"([^\"]*)\"\s+\"(.*?)\"\)/) {
	($start, $end, $tgt, $oldscore, $src, $trace) = 
	    ($line =~ m/^\((\d+)\s+(\d+)\s+\"([^\"]*)\"\s+(\S+)\s+\"([^\"]*)\"\s+\"\@?(.*?)\"\)/);
	$arccount++;
	if ($trace =~ m/^\(/) {
	    $trace =~ m/^[\(]+([^,]+),\d+/;
	    $trace = "\@" . $1;
	} else {
	    $trace = "\@$trace";
	}
	if (!defined($seen{"$start $end $tgt"})) {
	    print FIX "($start $end \"$tgt\" 1 \"$src\" \"$trace\")\n";
	    $seen{"$start $end $tgt"} = 1;
	}
    } else {
	print FIX $line;
    }
    if ($line =~ m/^\)/) {
	%seen = ();
    }
}


close(LTC);
close(FIX);

print "Arc count $arccount\n";
