#! /usr/bin/perl -w
#
# ccft2ccfc  <xxx.ccf>
#
# Convert a .ccft measured file for a component with state into a .ccfc file
#
# The formats are only slightly different; perhaps .ccft should be eliminated...
#
die "Must have .ccf file parameter" unless (defined($ARGV[0]));

open CCF, "<$ARGV[0]" or die "File $ARGV[0] missing";
open CCFT, "<$ARGV[0]t" or die "File $ARGV[0]t missing";
open CCFC, ">$ARGV[0]c" or die "File $ARGV[0]c ?";
$k = 0;
$inputs = 1;
$subd = <CCF>;
L:
while ($subd = <CCF>) {
  @subs = split(" ",$subd);
  if ($subd eq "\n") {
    $inputs = 0;
    $s1 = 0;
    next L;
  }
  if ($inputs) {
    $LBi[$k] = $subs[0];
    $UBi[$k++] = $subs[1];
  } else {
    $LBs[$s1] = $subs[0];
    $UBs[$s1++] = $subs[1];
  }
} #end L loop
#debug
#print "$k $s1\n";
close CCF;
$line = <CCFT>;
@lane = split(" ",$line);
print CCFC "theory 1 $k $s1 $lane[3]\n";
for ($idex=0; $idex<$k; $idex++) {
  print CCFC "$LBi[$idex] $UBi[$idex]\n";
}
for ($idex=0; $idex<$s1; $idex++) {
  print CCFC "$LBs[$idex] $UBs[$idex]\n";
}
while ($line = <CCFT>) {
  chop($line);
  @lane = split(" ",$line);
  for ($n4=0; defined($lane[$n4]); $n4 += 7) {
    print CCFC "$lane[$n4] $lane[$n4+1] $lane[$n4+2] $lane[$n4+3]\n";
  }
}
close CCFT;
close CCFC;
