#! /usr/bin/perl -w
# split0 <file> [2]
# script to divide the subdomains of a .ccf <file> (with or w/o state) in half.
# The new file goes to <file>.split
# State (if any) is not divided unless the optional [2] is there

die "Can't find .ccf file ", $ARGV[0] unless (-e $ARGV[0]);
$ccffile = $ARGV[0];
$doState = defined($ARGV[1]);
open(CCF, "<$ccffile");
open(NCCF,">$ccffile.split");
 $line = <CCF>; # code name
print NCCF $line;
$inStates = 0;
  while ($line = <CCF>) {  #read each subdomain line in the file
    if ($line eq "\n" || ($inStates && !$doState)) { #just copy a blank line or state line not done
      $inStates = 1;
      print  NCCF $line;
    } 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);
