#!/usr/local/bin/perl
open(INPUT, "Purk2M9s.p");

#.p specifics
$currcompt = "none";
$relative  = 1;
$parent = $currcompt;
$xpos{$parent} = 0.0;
$ypos{$parent} = 0.0;
$zpos{$parent} = 0.0;
$size{$parent} = 1.0;

while(<INPUT>) { 
  $line = $_;
  chop; split;
  @lc = @_;
  $_ = $lc[0];

 SWITCH: {
    if (m{//}) { 
      last SWITCH; 
    }
    if (m{\*compt}) { 
      $currcompt = $lc[1];
      print "# Compartment now a copy of $currcompt \n";
      last SWITCH; 
    }
    if (m{\*relative}) { 
      $relative = 1;
      print "# RELATIVE coordinates now \n";
      last SWITCH; 
    }
    if (m{\*absolute}) { 
      $relative = 0;
      print "# ABSOLUTE coordinates now \n";
      last SWITCH; 
    }
    if (m{\*}) { 
      print "# Dunno about $_ commands\n";
      last SWITCH;
    }
    if ($#lc >= 5){
      #print "creating compartment: $lc[0] ($lc[2],$lc[3],$lc[4]) sz $lc[5] \n";
      if ($lc[1] ne ".") { $parent = $lc[1]; }
      $xpos{$lc[0]} = $lc[2];
      $ypos{$lc[0]} = $lc[3];
      $zpos{$lc[0]} = $lc[4];
      $size{$lc[0]} = $lc[5];
      if ($relative==1) {
	$xpos{$lc[0]} += $xpos{$parent};
	$ypos{$lc[0]} += $ypos{$parent};
	$zpos{$lc[0]} += $zpos{$parent};
      }
      # print "DEF $lc[0] COMPT {  endpoints [ $xpos{$parent} $ypos{$parent} $zpos{$parent}, ";
      print "COMPT {  endpoints [ $xpos{$parent} $ypos{$parent} $zpos{$parent}, ";
      print "   $xpos{$lc[0]} $ypos{$lc[0]} $zpos{$lc[0]} ] ";
      print " \t xsection    [0 0, 0 $size{$lc[0]}, $size{$lc[0]} $size{$lc[0]}, $size{$lc[0]} 0, 0 0]  } \n";
 
      last SWITCH;
    }
 
  }
}

 
