#!/usr/bin/perl

open DEMON5,$ARGV[0];

$state=0;
$buf2="";
while($l=<DEMON5>)
{
  chomp($l);
  next if($l eq "");
# print "Got: $state $l\n";
  if($l eq "No:  2 write(A);")
  {
    $state=1;
  }
  elsif($l =~ /^asize/)
  {
    next;
  }
  elsif($l =~ /^D:\s+(\d+)$/)
  {
    $D=$1;
  }
  elsif($l =~ /^r :=\s+(\d+)$/)
  {
    $x=$1;
    $D2 = $D*2;
    open(BC_OUT,"./bc.sh $D2 $x|");

    $buf1 = "";
    while($l=<BC_OUT>)
    {
      chomp($l);
      if($l =~ /^([0123456789.]+)\\$/)
      {

	$buf1 .= $1;
      }
      elsif($l =~ /^([0123456789.]+)$/)
      {
	$buf1 .= $1;
	$dec1 = index($buf1,".");
	$len1 = length($buf1);
      }
    }
    close(BC_OUT);
  }
  elsif(substr($l,0,3) eq "No:")
  {
    if($state==1)
    {
      $state=0;
      $matches = -1;
      $n = $len1;
      $len2 = length($buf2);
      $n = $len2 if($len2<$len1);
      for($i=0;$i<$n;$i++)
      {
	$c1 = substr($buf1,$i,1);
	$c2 = substr($buf2,$i,1);
	if($c1 ne $c2)
	{
	  $matches=$i;
	  last;
	}
      }
      $dec2 = index($buf2,".");
      $matches-- if($matches>$dec2);
      print "$D\t$x\t$matches\n";
      $buf2="";
    }
  }
  elsif($state==1)
  {
    $l =~ s/ //g;
    $buf2 .= $l;
#   print "buf2: $buf2\n";
  }
}


