#! /usr/bin/perl -w
#ccfItoF    convert the current directory's worth of .ccf files from integer
# subdomains to float subdomains, creating a new directory with the same
# name but `f' appended.  Intervals [a,b], [b+1,c], ..., [x,y]
# are converted to [a,b+1), [b+1,c), [x,y).
# The system.pscf file is used to find all involved compoents.

#copy current directory D into D."f"
chop($dir = `pwd`);
$cmd = "rm -r -f ".$dir."f";
system($cmd);
$cmd = "mkdir ".$dir."f";
system($cmd);
$cmd = "cp ".$dir."/* ".$dir."f/";
system($cmd);
$dir = $dir."f";  #name is now the new directory

open(PSCF, $dir."/system.pscf") || die "can't open system.pscf";
$line = <PSCF>;  #discard Polish line
if (-x "ident.bin") {
  $ccffile = "ident.ccf\n";  
}
else {
  $ccffile = <PSCF>;
}
FILE: while ($ccffile) {  #process each file name in the file
  chop($ccffile);
  if ($ccffile eq "theory") { #ignore
    $ccffile = <PSCF>;  #get next file 
    next FILE;
  }
  open(CCF, $ccffile) || die "can't open .ccf file ", $ccffile;
  open(NCCF, ">".$dir."/".$ccffile) || die "failed to open new .ccf filep";
  $line = <CCF>; 
  print NCCF $line;  #copy the name
  $line = <CCF>;
  while ($line) {  #fix each subdomain line in the file
    @subs = split(" ",$line);
    $N = $subs[2];
    $end = $subs[1];
    $line = <CCF>;
    if ($line) {$end += 1;}
    printf NCCF "%.15e %.15e %d\n", $subs[0],$end,$N;
  }
  close(CCF);
  close(NCCF);
  $ccffile = <PSCF>;  #get next file 
}
close(PSCF);
