#! /usr/bin/perl -w -X
# XqtC <.ccf file> 
#
# Runs a "theory" file for a concurrent component
#
# Messages must go to a log, (xxx.errlog for file xxx.ccf)
#  since any output is intercepted
#
use component;

sub logdie {
# arg is a list of messages
# return all values of -9999 and append messages to the file
#
$logf = $base.".errlog";
unless (-e $logf) { #create log file
  open(L,">$logf");
  close(L);
}
open(L,">>$logf");
foreach $m (@_) {
  print L $m;
}
print L "\n";
close(L);

#progress list
# 1st input always read
goto "P$progress";
P1: print "0\n";
P2: print STDERR "-1\n";
P3: $X = <STDIN>;
P4: print "0\n";
P5: print STDERR "-1\n";
P6: print STDERR "-1\n";
exit;
}

$| = 1;  #must flush all outputs immediately
chop($X = <STDIN>);  #read first input

$progress = 1;  #how many of the 7 actions have been done
$base = "anon";
unless (defined($ARGV[0])) {
  logdie "A .ccf file must be given";
}
$ccf = $ARGV[0];
@fn = split(/\./,$ccf);
$base = $fn[0];
logdie "Parameter file ", $ccf, " must be a .ccf file" unless($fn[1] eq "ccf");

$ccfcfile = $ccf."c";
open(CCFC, "<$ccfcfile") || logdie "No 'theory' file to execute";
#get position of the data
$head = <CCFC>;
@hed = split(' ',$head); #concurrent, 1, isubs, ssubs
$dataskip = $hed[2] + $hed[3];
$numisubs = $hed[2];
close CCFC;

open(CCF, $ccf) || logdie "given file ",$ccf," not found";
$line = <CCF>;  #skip header
$iline = 0; 
$found = 0;
IL: while ($line = <CCF>) {
  if ($line eq "\n") {  #blank line, end of 1st input subdomains
    last IL;
  }
  @vals = split(" ",$line);
  if ($X >= $vals[0] && $X < $vals[1]) { #in this input subdomain
    $found = 1;
    last IL;
  }
  $iline++;
}
unless ($found) {
  close CCF;
  logdie "input ",$X," out of subdomain range";
}
#get first output from theory file
open(CCFC,"<$ccfcfile");  #exists from above
for ($r=0; $r<$dataskip+2+$iline; $r++) { #skip down to correct data line
  $line = <CCFC>;
}
#print "1st: isub=$iline, rec=$r; line- $line";  #debug
@vals = split(" ",$line); #format:  count, last out, 1st out, run1, run2, run4
$N = $vals[0];
$Y = $vals[2];
$R1 = $vals[3];
logdie "No test information for subdomain holding input ",$X unless ($N != 0);
close CCFC;
print "$Y\n";  #write to parallel
print STDERR "$R1\n";
$X2 = <STDIN>;  #read what parallel wrote
$progress = 4;

#continue to reading .ccf file
#skip past blank line
SI: while (chop($line = <CCF>)) {
  if ($line eq "") {
    last SI;
  }
}
$sline = 0;  
$found = 0;
SL: while ($line = <CCF>) {
  @vals = split(" ",$line);
  if ($X2 >= $vals[0] && $X2 < $vals[1]) { #in this parallel input subdomain
    $found = 1;
    last SL;
  }
  $sline++;
}
close(CCF);
logdie "parallel input ",$X2," out of subdomain range" unless ($found);

open(CCFC, "<".$ccfcfile); #checked for existence above
#find the values
$dataskip += 2 + $sline*$numisubs + $iline;
for ($r=0; $r<$dataskip; $r++) { #skip down to correct data line
  $line = <CCFC>;
}
#print "2nd: isub=$iline, ssub=$sline, rec=$r; line- $line";  #debug
@vals = split(" ",$line); #format:  count, last out, 1st out, run1, run2, run4
$N = $vals[0];
$Y = $vals[1];
$R3 = $vals[4];
$R4 = $vals[5];
logdie "No test information for subdomain holding parallel input ", $X2 unless ($N != 0);
close(CCFC);
$progress = 7;
print "$Y\n"; #final output
print STDERR "$R3\n";
print STDERR "$R4\n";
