#!/usr/bin/perl

$maxtrack = $ARGV[1];
$clock = $ARGV[2];
$clock10 = $clock/10;
$start = $ARGV[3];

open IN,$ARGV[0] or die;
open PLOTS,">>plots.gnu" or die;
open LY,">/tmp/ly.dat" or die;
#open LY2,">/tmp/ly2.dat" or die;
open GROUP,">/tmp/group.dat" or die;

$nly=0;
$startly=0;
$lastgroup=-1;

$lastcmd="xx";
while($l = <IN>)
{
  chomp($l);
  if($l =~ /^(LY|LK|SK|IL command|Execute)/)
  {
    $cmd = $1;
#   print "cmd: $cmd\n";
    if($cmd eq "IL command")
    {
      if($lastcmd ne "IL command")
      {
	@f = split /\s+/,$l;
#       print "f2: ".$f[2]."\n";
	$x1 = $f[2]/$clock - $start;
	print PLOTS "set arrow from $x1,0 to $x1,$maxtrack nohead lc rgb 'blue'\n";
      }
    }
    elsif($cmd eq "LY")
    {
      @f = split /\s+/,$l;
      $cl = $f[4];
      $nly = $nly+1;
#printf LY2 "%d %d %s %d\n",$cl,$f[1],$f[2],$lastgroup;
      if($startly == 0)
      {
	$startly=$cl;
      }
    }
    elsif($cmd eq "Execute")
    {
      if($l =~ /clock: (\d+) /)
      {
	$cl = $1;
	if(($cl-$startly)>$clock10)
	{
	  printf LY "%.8f %d\n",$cl/$clock-$start,$nly*10;
	  $nly=0;
	  $startly=$cl;
	}
      }
    }
    $lastcmd=$cmd;
  }
  elsif($l =~ /^   s:.*tg: (\d+) /)
  {
    $group = $1;
    if($group != $lastgroup)
    {
      printf GROUP "%.8f %d\n",$cl/$clock-$start,$group;
      $lastgroup=$group;
    }
  }
}





