#! /usr/bin/perl -w
# COMP [options]
#
#  Top-level component-measuring script.  Uses "system.pscf" to collect
#  names of all component .ccf files, and passes them one at a time to
#  measurement scripts which make the corresponding .ccfc files (if not 
#  already done).  In addition to the named options below, any arguments
#  are passed along, so all options of the called scripts are also useful.

$Voption = 0;  #verbose, print more messages
$Zoption = 0; #clear all .ccfc and theory files to force measurement w/o incremental
$Foption = 0; #reliability, default not

$allOPT = "";
OPT: for ($i = 0;;$i++) { #over all options
  unless (defined($ARGV[$i])) {last OPT;}
  $allOPT .= " ".$ARGV[$i];
  if ($ARGV[$i] eq "-V") {
    $Voption = 1;
  }
  if ($ARGV[$i] eq "-Z") {
    $Zoption = 1;
  }
  if ($ARGV[$i] eq "-F") {
    $Foption = 1;
  }
} #end OPT
#print $allOPT; #debug

#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

#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

if ($Zoption) {
  unlink glob("*.ccfc");
  unlink glob("*.ccft");
  unlink glob("theory*");
  #system ('rm -f theory*');
  if ($Voption) {
    warn "No existing measurement files or incremental processing will be used\n";
  }
}

#foreach $comp (@components) {print $comp."\n"; }   #debug
#for each component process its file and execute test
foreach $comp (@components) {
  $stateless = 1;  #assume
  $concurrent = 0;
  open(C,"<$comp") or die "Component configuration file $comp missing";
  $line = <C>;
  @lines = split(' ',$line);
  if (defined($lines[1])) { #code is typed
    if ($lines[1] eq "state") {
      $stateless = 0;
    } elsif ($lines[1] eq "concurrent") {
      $concurrent = 1;
      $stateless = 0;
    }
  }
  close(C);
#print "COMP? $allOPT -S $comp\n";  #debug
  if ($stateless) {
    system "perl COMPFt $allOPT -S $comp";
  } else {
    if ($concurrent) {
      system "perl COMPSt $allOPT -S $comp";
    } else {
      system "perl COMPSt -R $allOPT -S $comp";
    }
  }
} #end loop over components

#Create "ident" stateless 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);
