#!/usr/bin/perl
# Make a log 

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 Getopt::Std;
use Gen qw(genlog);

getopts("ec:h:");

$opt_h = "top" if defined($opt_c) && !defined($opt_h);

sub make_log
{
   my($startx, $endx, $radius, $material, $name) = @_;
   return if $startx == $endx;

   print <<EOF;
$material cylinder $name
0
0
EOF
print "7 $startx 0 0 $endx 0 0 $radius\n\n";

# Cap the ends
print "${material} ring $name", "cap1\n";
print "0\n0\n";
print "8 $startx 0 0 -1 0 0 0 $radius\n\n";

print "${material} ring $name", "cap2\n";
print "0\n0\n";
print "8 $endx 0 0 1 0 0 0 $radius\n\n";

}

if($#ARGV < 3) {
   die "Usage: genlog [-c top|bottom] [-h cut_ht] [ (material name length) | -e mat name sx sy sz ex ey ez ] diameter\n";
}

$material = $ARGV[0];
$name = $ARGV[1];

if($opt_e) {
   my @pts = @ARGV[2..7];
   my $rad = $ARGV[8] / 2.0;
   my ($cut_side, $cut_ht) = ($opt_c, $opt_h);
   genlog($material, $name, \@pts, $rad, 1, $cut_ht, $cut_side);
} else {
   $width = $ARGV[2] * 12;
   $diam = $ARGV[3];
   $rad = $diam / 2;
   make_log(0, $width, $rad, $material, $name);
}
