#!/usr/bin/perl -w
# Make a log wall centered at the origin 

BEGIN {
   # Look for the library
   @pos = split /:/,$ENV{PATH};

   $0 =~ s!/[^/]*$!!;
   if(length($0) > 2) {
      push @pos, $0;
   }

   foreach (@pos)
   {
      s/bin$/lib/;
      if(-f "$_/Gen.pm") {
         $libdir = $_ ;
         last;
      }
   } 

   die "Couldn't find the Architecture library (Gen.pm).\nI checked in @pos using your PATH variable (replacing bin with lib) and this executable's path.\n" if !defined($libdir);

   push @INC, "$libdir"; 
}

use Gen;
use Getopt::Std;

getopts("r");

$genlog = \&Gen::genlog;

die "Usage: genlogwall material name width height logdiam [holedata]\n" if $#ARGV < 4;

$material = $ARGV[0];
$name = $ARGV[1];
$width = $ARGV[2] * 12;
$height = $ARGV[3] * 12;
$diam = $ARGV[4];
$rad = $diam / 2;

$nholes=0;
@holes = ();
if(defined($ARGV[5])) {
   $holefile = $ARGV[5];
   open HOLES, "<$holefile" or warn "Couldn't find hole data file: $holefile.\n";

   $nholes = 0;
   while($data = <HOLES>) {
      next if $data =~ /^[ \t]*#/;
      next if $data !~ /[0-9]/;
      chomp $data;
      ($hole_data, $hole_fill) = split(/:/, $data);
      @vals = split(/\s+/, $hole_data);
      $vals{bottom} = $vals[0] * 12;
      $vals{top} = $vals[1] * 12;
      $vals{start} = $vals[2] * 12;
      $vals{width} = $vals[3] * 12;
      $vals{fill} = $hole_fill;
      $holes[$nholes++] = { %vals };
   }

   close HOLES;
   # Sort the holes in increasing x.
   @holes = sort { $a->{start} <=> $b->{start} } @holes;
}

$z = $rad;

$n = $height / (2*$rad);

if($n != int($n)) {
   warn "Height not divisible by log size for $name ($holefile). Wall will be slightly tall.\n";
   $n = int($n) + 1;
}

{
my($bottom, $top);

# Fill the holes with glass or doors

my ($tw, $th, $tt);
for($hole = 0; $hole < $nholes; $hole++) {
   if(defined($holes[$hole]{fill}) && ($holes[$hole]{fill} =~ /^w/)) {
      $th = $holes[$hole]{top} - $holes[$hole]{bottom};
      $tw = $holes[$hole]{width};
      $tt = $diam / 2;
      system("genframe -b ypine ${name}_win.${hole} $tw $th $diam 1 | xform -t $holes[$hole]{start} -$tt $holes[$hole]{bottom}");
      system("genwindow ypine window_illum ${name}_win.${hole} ". ($tw-2). " ". ($th-2) . " 1 3 3 | xform -t ". ($holes[$hole]{start} + 1) . " 1 " . ($holes[$hole]{bottom} + 1));
   }
   if(defined($holes[$hole]{fill}) && ($holes[$hole]{fill} =~ /^d/)) {
      $th = $holes[$hole]{top} - $holes[$hole]{bottom};
      $tw = $holes[$hole]{width};
      $tt = $diam / 2;
      system("genframe ypine ${name}_dfrm.${hole} $tw $th $diam 1 | xform -t $holes[$hole]{start} -$tt $holes[$hole]{bottom}");
      system("gendoor paint brass ${name}_door.${hole} ". ($tw-2). " ". ($th-1) . " 2 | xform -t ". ($holes[$hole]{start} + 1) . " 0 " . ($holes[$hole]{bottom}));
   }
}

for($log = 1; $log <= $n; $log++) {
   # Find intersecting holes
   $x = 0;
   for($hole = 0; $hole < $nholes; $hole++) {

      $bottomhole = $holes[$hole]{bottom};
      $tophole = $holes[$hole]{top};
      $bottomlog = $z - $rad;
      $toplog = $z + $rad;

      if( ($bottomhole < $toplog && $tophole > $bottomlog) ||
          ($bottomhole < $toplog && $bottomhole > $bottomlog) ||
          ($tophole < $toplog && $tophole > $bottomlog))
      { # Hole breaks log
         &{$genlog}($material, "$name.$log", 
                [ $x, 0, $z, $holes[$hole]{start}, 0, $z ],
                $rad) if($holes[$hole]{start} - .2 > $x);
         if($bottomhole > $bottomlog && $bottomhole < $toplog) {
            &{$genlog}($material, "$name.tcut.$log", 
                [ $holes[$hole]{start}, 0, $z,
                  $holes[$hole]{start} + $holes[$hole]{width}, 0, $z ],
                $rad, 0, $bottomhole-$z);
         } 
         if($tophole > $bottomlog && $tophole < $toplog) {
            &{$genlog}($material, "$name.bcut.$log", 
                [ $holes[$hole]{start}, 0, $z,
                  $holes[$hole]{start} + $holes[$hole]{width}, 0, $z ],
                $rad, 0, $tophole-$z, "bottom");
         }
         $x = $holes[$hole]{start} + $holes[$hole]{width};
      }
   }
   &{$genlog}($material, "$name.$log", [ $x, 0, $z, $width, 0, $z ], 
          $rad) if $x < $width;
   $z += $diam;
}

}
