#! /usr/bin/perl -w
# script to divide the subdomains of a complete run in half.
# Driven by system.pscf file.  Each component .ccf file is adjusted
#  but in a new directory that is a copy of the current one D with 
#  2 appended to D.

#copy current directory D into D2
chop($dir = `pwd`);
$cmd = "rm -r -f ".$dir."2";
system($cmd);
$cmd = "mkdir ".$dir."2";
system($cmd);
$cmd = "cp ".$dir."/* ".$dir."2/";
system($cmd);
$dir = $dir."2";  #name 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>;
}
while ($ccffile) {  #process each file name in the file
  chop($ccffile);
  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
  while ($line = <CCF>) {  #read each subdomain line in the file
    if ($line eq "\n") { #just copy a blank line
    print  NCCF"\n";
    } else {
    @subs = split(" ",$line);
    $N = $subs[2];
      $mid = ($subs[0] + $subs[1])/2;
      print NCCF $subs[0]." ".$mid." ".$N."\n";
      print NCCF $mid." ".$subs[1]." ".$N."\n";
    }
  }
  close(CCF);
  close(NCCF);
  $ccffile = <PSCF>;  #get next file until EOF
}
close(PSCF);
