#! /usr/bin/perl -w
$Goption = 0;
$Foption = 0;
$allOPT = "";
OPT: for ($i = 0;;$i++) { #over all options
  unless (defined($ARGV[$i])) {last OPT;}
  $allOPT .= " ".$ARGV[$i];
  if ($ARGV[$i] eq "-G") {
    $Goption = 1;
  }
  if ($ARGV[$i] eq "-F") {
    $Foption = 1;
  }
} #end OPT
#debug
#print $allOPT;

#process system configuration file
$num_comp = 0;
$polish = 0;
$sys_desc = "system.pscf" ;
open(SYSTEM, $sys_desc ) || die "could not open the file ", $sys_desc ;
$polish = <SYSTEM>; #polish line
#TBD count how many comps needed to check later?

#store component .ccf names
@components = ();
while ($comp_name = <SYSTEM>) { #read to end
  chop($comp_name);
  $components[$num_comp] = $comp_name;
  $num_comp++
}
close SYSTEM ;
# $num_comp components in the system

#debug
#foreach $comp (@components) {print $comp."\n"; }

#for each component process its file and execute test
foreach $comp (@components) {
  open(CCF, $comp) || die "could not open the file ", $comp;
  # get the name of the executable
  chop($prog = <CCF>) ;
  #die "component ", $prog, " can't be executed." unless (-x $prog);  #see below
  $comptheory = $comp."t";  #assuming name ends in .ccf
  @statvec = stat($comp); #for ccf file
  $ccfileTime = $statvec[9];
  @statvec = stat($prog); #for executable file
  $binfileTime = $statvec[9];  

  #read & check .ccf file
  $stateless = 1;  #assume  
  $Wrong = 0;  #if TRUE, found something wrong with .ccf file
  # Wrong==2 means a problem that precludes some further checking
  unless ($prog =~ /^theory/ ||-x $prog) {
    warn "Component file ",$prog," in ",$comp, " is not executable\n";
    #should do better than this -- actually try to execute it...
    $Wrong = 1;
  }
  warn "  Component config file ",$comp," with code file ",$prog,":\n";
  $FirstInterval = 1;
  $num_isubd = 0;
  $num_ssubd = 0;
  while ($line = <CCF>) {
    chomp($line);
    @bits = split(" ",$line);
    #debug
    #print $bits[2];
    if ($stateless) {
      $num_isubd++;
    } else {
      $num_ssubd++;
    }
    if ($FirstInterval) { #left-most
      $FirstInterval = 0;
      $Left = $bits[0];
      $Right = $bits[0];
    }
    if ($line eq "") {
      unless ($stateless) { #another blank!
        warn "  contains blank line(s)\n";
        $Wrong = 2;
      }
      $stateless = 0;
      $FirstInterval = 1; #again
      $iinterval = "[$Left,$Right)";
      $num_isubd--; #subtract the blank line
    }
    elsif ($bits[0] > $bits[1]) {
      warn "  Line ",qq{$line}," has empty interval [",$bits[0],",",$bits[1],")\n";
      $Wrong = 2;  #but keep going...
    } 
    elsif ($prog !~ /^theory/ && ((not defined($bits[2])) || $bits[2] == 0)) {
      warn "  Sampling count missing \n";
      $Wrong = 1;
    }
    if ($Wrong < 2) { #OK to make check for interval coverage
      unless ($FirstInterval || $Right == $bits[0]) {
        warn "  Intervals overlap or are not contiguous\n";
        $Wrong = 2;
      } 
    }
    $Right = $bits[1];
  } #end while going through one file
  close (CCF);
  $ccftGood = 1; #assume there is already a good .ccft file
  if (-e $comptheory) {
    open (CCFT, $comptheory);
    chop($prop_line = <CCFT>); #examine the code line
    #@ccft_codes = split(" ",$prop_line);
    @statvec = stat($comptheory); #for theory ccf file
    $ccftGood = 0 unless ($statvec[9] > $ccfileTime && $statvec[9] > $binfileTime);
    close (CCFT);
  } else {
    $ccftGood = 0;
  }
  if ($stateless) {
    warn "  stateless with $num_isubd input subdomains on [$Left, $Right) \n";
  } else {
    warn "  $num_ssubd state subdomains on [$Left,$Right) and $num_isubd input subdomains on $iinterval \n";
  }

  if ($ccftGood) { #there is an OK .ccft file
    warn "  measurement file ",$comptheory," exists: ",$prop_line,"\n";
  } else {
    if ($Wrong) {
      warn "  Config file errors; nothing generated for this component\n";
    } else {
      #TBD
      warn "  ",$comptheory, " not found or out-of-date, generating measurement file...\n";
      if ($stateless) {
#debug
#print "COMPF -C -V $allOPT -S $comp";
        system "COMPF -C -V $allOPT -S $comp";
      } else {
        system "COMPS -R -S $allOPT $comp";
      }
    }
  }
  print "\n";
} #end loop over components

#Create "ident" ccft file in any case
  open (I, ">ident.ccft");
  if ($Foption) { #reliability
    print I "theory LC r 99\n";
    print I "-1000000 1000000 1 0 0 1\n";  #[-10^6,10^6), slope 1, intercept 0, reliability 1, no error
  } else { #run time
    print I "theory L \n";
    print I "-1000000 1000000 1 0 0 0\n";  #[-10^6,10^6), slope 1, intercept 0, 0 run time, no error
  }
  close(I);
