#!/usr/bin/perl
use Math::Round;

$cloc = "/tools/bin/cloc-1.62.pl";  # fix for you

$output_file = $ARGV[0];

# $output_file = "OUTPUT.txt";
# system("make > $output_file");

open(OUTPUT, "<", $output_file) || die "Could not open $output_file";

while ($line=<OUTPUT>) {
  my($name,$scale,$states,$time,$steps,$result);

  chomp($line);
  next unless ($line =~/^NAME:/);

  ($name) = ($line =~/^NAME:(.*)$/);

  $line=<OUTPUT>;
  chomp($line);
  ($cite) = ($line =~/^CITE:(.*)$/);

  $line=<OUTPUT>;
  chomp($line);
  ($scale) = ($line =~/^SCALE:(.*)$/);

  # get lines of code.  cloc needs to be inserted into Makefile

  while ($line=<OUTPUT>) {
    chomp($line);
    last unless ($line =~ /^$/);
  }

#  die "expected cloc output got:\n$line\n" unless ($line =~ /files,language,blank,comment,code/);
#  $line=<OUTPUT>;
#  chomp($line);
#  ($loc) = ($line =~ /^.*,.*,.*,.*,(\d+)\s*$/);
#  die "incorrect cloc output:\n$line\n" unless defined($loc);

  while ($line=<OUTPUT>) {
    chomp($line);
    if ($line =~ /time\s\(s\).*:/) {
      ($time)=($line =~ /time.*:\s*(\S+)\s*$/);
      last;
    }
  }
  

  while ($line=<OUTPUT>) {
    chomp($line);
    if ($line =~ /states/) {
      ($states)=($line =~ /states\s*:\s*(\d+)\s*$/);
      last;
    }
  }

  while ($line=<OUTPUT>) {
    chomp($line);
    if ($line =~ /transitions/) {
      ($steps)=($line =~ /transitions\s*:\s*(\d+)\s*$/);
      last;
    }
  }
 

  # print "line = $line\n" unless defined($time);

  while ($line=<OUTPUT>) {
    chomp($line);
    die "Could not find result for $name" if ($line =~ /^NAME:/);
    if ($line =~ /^The program MAY NOT be correct/) {
      $result="-";
      last;
    }
    if ($line =~ /^The standard properties hold/) {
      $result="+";
      last;
    }
  }
  
  die "no name" unless defined($name);
  die "no scale" unless defined($scale);
#  die "no loc" unless defined($loc);


  die "no time" unless defined($time);
  die "no steps" unless defined($steps);
  die "no result" unless defined($result);

  # for debugging:

  # print "name = $name\n";
  # print "cite = $cite\n";
  # print "type = $type\n";
  # print "scale = $scale\n";
  # print "loc = $loc\n";
  # print "prove = $prove\n";
  # print "mem = $mem\n";
  # print "time = $time\n";
  # print "states = $states\n"
  # print "transitions = $steps\n";
  # print "result = $result\n";
  # print "\n";

  $time=round($time);
  $time=1 if $time==0;

  print "  $name $cite & $result & $states & $steps & $time & $scale \\\\","\n";
}
