#!/usr/bin/perl
use lib '.';
require "cgi-lib.pl";
ReadParse(*params);
$file = "/home/dave/lego/PeeronMyParts.txt";
print "Content-Type: text/html\n\n";

def_parts();

if(!open(PF,$file)) {
    print "Couldnt open $file!<br>\n";
    exit(0);
}
print "<form>\n";
print "Part #:<input type=text name=\"num\" value=\"$params{num}\">\n";
print "Color:<input type=text name=\"color\" value=\"$params{color}\">\n";
print "Part:<input type=text name=\"part\" value=\"$params{part}\">\n";
print "Type:<select name=type size=1>\n";
print "<option></option>\n";
foreach("brick","plate","baseplate","tile") {
    print "<option value=\"$_\"".(($params{type} eq $_) ? " selected" : "").">$_</option>\n";
}
print "</select>\n";
print "<input type=submit value=\"Go Already!\">\n";
print "</form>\n";
if(!(keys %params)) {
    exit(0);
}
print "<table border=1>\n";
$length = 0;
$volume = 0;
$area = 0;
$lx1 = 0;
$lx2 = 0;
$pieces = 0;
if($params{unique_colors}) { $uq = 1; }
while(<PF>) {
    next if(/^\s*Quantity\s*Part\s*Number\s*Color\s*Description\s*Notes\s*$/i);
    my($qty,$num,$color,$part,$notes) = split /\t/;
    my($skip) = 0;
    if($params{num} =~ /\S/ && $num ne $params{num}) { $skip = 1; }
    if($params{color} =~ /\S/ && lc $color ne lc $params{color}) { $skip = 1; }
    if($params{part} =~ /\S/ && $part !~ /\Q$params{part}\E/i) { $skip = 1; }
    if($params{type} eq "brick" && !$parts{$num}{brick}) { $skip = 1; }
    if($params{type} eq "plate" && !$parts{$num}{plate}) { $skip = 1; }
    if($params{type} eq "baseplate" && !$parts{$num}{baseplate}) { $skip = 1; }
    if($params{type} eq "tile" && !$parts{$num}{tile}) { $skip = 1; }
    if(!$skip) {
	$uqs{$color}{uniq}++;
	$uqs{$color}{qty}+=$qty;
	next if($uq);
	$pieces+=$qty;
	if($params{type} eq "brick") {
	    $length += $qty * $parts{$num}{long} * $parts{$num}{high};
	    $volume += $qty * $parts{$num}{long} * $parts{$num}{wide} * $parts{$num}{high};
	    $lx1 += $qty * $parts{$num}{long} * $parts{$num}{high} if($parts{$num}{wide} == 1);
	    $lx2 += $qty * $parts{$num}{long} * $parts{$num}{high} if($parts{$num}{wide} == 2);
	} elsif($params{type} =~ /^(plate|tile|baseplate)$/) {
	    $length += $qty * $parts{$num}{long};
	    $area += $qty * $parts{$num}{long} * $parts{$num}{wide};
	    $lx1 += $qty * $parts{$num}{long} if($parts{$num}{wide} == 1);
	    $lx2 += $qty * $parts{$num}{long} if($parts{$num}{wide} == 2);
	}
	print "<tr>";
	print "<td><img src=\"http://media.peeron.com/ldraw/images/7/$num\.png\"></td>\n";
	print "<td>$qty</td><td>";
	print "<a href=\"http://www.peeron.com/inv/parts/$num\">$num</a>\n";
	print "</td><td>$color</td><td>$part</td><td>$notes</td></tr>\n";
    }
}
print "</table>\n";
print "Pieces: $pieces<br>\n";
if($params{type} eq "brick") {
    print "Total Wall Area: $length<br>\n";
    print "Total 1x wall area: $lx1<br>\n";
    print "Total 2x wall area: $lx2<br>\n";
    print "Total volume: $volume<br>\n";
}
if($params{type} =~ /^(plate|tile|baseplate)$/) {
    print "Total length: $length<br>\n";
    print "Total 1x length: $lx1<br>\n";
    print "Total 2x length: $lx2<br>\n";
    print "Total area: $area<br>\n";
}
print "Colors:\n";
print "<table border=1>\n";
my($cn) = 1;
foreach my $c (sort keys %uqs) {
    print "<tr>";
    print "<td>".($cn++)."</td><td>";
    print "<a href=\"$ENV{SCRIPT_NAME}?color=$c&num=$params{num}&part=$params{part}&type=$params{type}\">$c</a>";
    print "</td><td>$uqs{$c}{uniq}</td><td>$uqs{$c}{qty}</td></tr>\n";
}
print "</table>\n";

sub def_parts {
    %parts = (
	      3005 => { brick=>1,long=> 1,wide=>1,high=>1,desc=>'Brick 1 x 1' },
	      3004 => { brick=>1,long=> 2,wide=>1,high=>1,desc=>'Brick 1 x 2' },
	      3622 => { brick=>1,long=> 3,wide=>1,high=>1,desc=>'Brick 1 x 3' },
	      3010 => { brick=>1,long=> 4,wide=>1,high=>1,desc=>'Brick 1 x 4' },
	      3009 => { brick=>1,long=> 6,wide=>1,high=>1,desc=>'Brick 1 x 6' },
	      3008 => { brick=>1,long=> 8,wide=>1,high=>1,desc=>'Brick 1 x 8' },
	      6111 => { brick=>1,long=>10,wide=>1,high=>1,desc=>'Brick 1 x 10' },
	      6112 => { brick=>1,long=>12,wide=>1,high=>1,desc=>'Brick 1 x 12' },
	      2465 => { brick=>1,long=>16,wide=>1,high=>1,desc=>'Brick 1 x 16' },
	      '3245b' => { brick=>1,long=> 2,wide=>1,high=>2,desc=>'Brick 1 x 2 x 2 (axle)' },
	      '3245a' => { brick=>1,long=> 2,wide=>1,high=>2,desc=>'Brick 1 x 2 x 2 (post)' },
	      2453 => { brick=>1,long=> 1,wide=>1,high=>5,desc=>'Brick 1 x 1 x 5' },
	      2454 => { brick=>1,long=> 2,wide=>1,high=>5,desc=>'Brick 1 x 2 x 5' },
	      3755 => { brick=>1,long=> 3,wide=>1,high=>5,desc=>'Brick 1 x 3 x 5' },
	      3754 => { brick=>1,long=> 6,wide=>1,high=>5,desc=>'Brick 1 x 6 x 5' },
	      3003 => { brick=>1,long=> 2,wide=>2,high=>1,desc=>'Brick 2 x 2' },
	      3002 => { brick=>1,long=> 3,wide=>2,high=>1,desc=>'Brick 2 x 3' },
	      3001 => { brick=>1,long=> 4,wide=>2,high=>1,desc=>'Brick 2 x 4' },
	      2456 => { brick=>1,long=> 6,wide=>2,high=>1,desc=>'Brick 2 x 6' },
	      3007 => { brick=>1,long=> 8,wide=>2,high=>1,desc=>'Brick 2 x 8' },
	      3006 => { brick=>1,long=>10,wide=>2,high=>1,desc=>'Brick 2 x 10' },
	      30145 => { brick=>1,long=> 2,wide=>2,high=>3,desc=>'Brick 2 x 2 x 3' },
	      30144 => { brick=>1,long=> 4,wide=>2,high=>3,desc=>'Brick 2 x 4 x 3' },
	      2356 => { brick=>1,long=> 6,wide=>4,high=>1,desc=>'Brick 4 x 6' },
	      6212 => { brick=>1,long=>10,wide=>4,high=>1,desc=>'Brick 4 x 10' },
	      4202 => { brick=>1,long=>12,wide=>4,high=>1,desc=>'Brick 4 x 12' },
	      30400 => { brick=>1,long=>18,wide=>4,high=>1,desc=>'Brick 4 x 18' },
	      4201 => { brick=>1,long=> 8,wide=>8,high=>1,desc=>'Brick 8 x 8' },
	      4204 => { brick=>1,long=>16,wide=>8,high=>1,desc=>'Brick 8 x 16' },
	      733  => { brick=>1,long=>10,wide=>10,high=>1,desc=>'Brick 10 x 10' },
	      700  => { brick=>1,long=>20,wide=>10,high=>1,desc=>'Brick 10 x 20' },
	      30072 => { brick=>1,long=>24,wide=>12,high=>1,desc=>'Brick 12 x 24' },
	      3024 => { plate=>1,long=> 1,wide=>1,desc=>'Plate 1 x 1' },
	      3023 => { plate=>1,long=> 2,wide=>1,desc=>'Plate 1 x 2' },
	      3623 => { plate=>1,long=> 3,wide=>1,desc=>'Plate 1 x 3' },
	      3710 => { plate=>1,long=> 4,wide=>1,desc=>'Plate 1 x 4' },
	      3666 => { plate=>1,long=> 6,wide=>1,desc=>'Plate 1 x 6' },
	      3460 => { plate=>1,long=> 8,wide=>1,desc=>'Plate 1 x 8' },
	      4477 => { plate=>1,long=>10,wide=>1,desc=>'Plate 1 x 10' },
	      3022 => { plate=>1,long=> 2,wide=>2,desc=>'Plate 2 x 2' },
	      3021 => { plate=>1,long=> 3,wide=>2,desc=>'Plate 2 x 3' },
	      3020 => { plate=>1,long=> 4,wide=>2,desc=>'Plate 2 x 4' },
	      3795 => { plate=>1,long=> 6,wide=>2,desc=>'Plate 2 x 6' },
	      3034 => { plate=>1,long=> 8,wide=>2,desc=>'Plate 2 x 8' },
	      3832 => { plate=>1,long=>10,wide=>2,desc=>'Plate 2 x 10' },
	      2445 => { plate=>1,long=>12,wide=>2,desc=>'Plate 2 x 12' },
	      4282 => { plate=>1,long=>16,wide=>2,desc=>'Plate 2 x 16' },
	      3031 => { plate=>1,long=> 4,wide=>4,desc=>'Plate 4 x 4' },
	      3032 => { plate=>1,long=> 6,wide=>4,desc=>'Plate 4 x 6' },
	      3035 => { plate=>1,long=> 8,wide=>4,desc=>'Plate 4 x 8' },
	      3030 => { plate=>1,long=>10,wide=>4,desc=>'Plate 4 x 10' },
	      3029 => { plate=>1,long=>12,wide=>4,desc=>'Plate 4 x 12' },
	      3958 => { plate=>1,long=> 6,wide=>6,desc=>'Plate 6 x 6' },
	      3036 => { plate=>1,long=> 8,wide=>6,desc=>'Plate 6 x 8' },
	      3033 => { plate=>1,long=>10,wide=>6,desc=>'Plate 6 x 10' },
	      3028 => { plate=>1,long=>12,wide=>6,desc=>'Plate 6 x 12' },
	      3456 => { plate=>1,long=>14,wide=>6,desc=>'Plate 6 x 14' },
	      3027 => { plate=>1,long=>16,wide=>6,desc=>'Plate 6 x 16' },
	      3026 => { plate=>1,long=>24,wide=>6,desc=>'Plate 6 x 24' },
	      41539 => { plate=>1,long=> 8,wide=>8,desc=>'Plate 8 x 8' },
	      728  => { plate=>1,long=>11,wide=>8,desc=>'Plate 8 x 11' },
	      3867 => { baseplate=>1,long=>16,wide=>16,desc=>'Baseplate 16 x 16' },
	      210  => { baseplate=>1,long=>22,wide=>16,desc=>'Baseplate 16 x 22' },
	      3334 => { baseplate=>1,long=>24,wide=>16,desc=>'Baseplate 16 x 24' },
	      3857 => { baseplate=>1,long=>32,wide=>16,desc=>'Baseplate 16 x 32 (square)' },
	      3811 => { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32' },
	      4186 => { baseplate=>1,long=>48,wide=>48,desc=>'Baseplate 48 x 48' },
	      3865 => { baseplate=>1,long=>16,wide=>8,desc=>'Baseplate 8 x 16' },
	      3497 => { baseplate=>1,long=>24,wide=>8,desc=>'Baseplate 8 x 24' },
	      4187 => { baseplate=>1,long=>32,wide=>8,desc=>'Baseplate 8 x 32' },
	      '30225p02' => { baseplate=>1,long=>16,wide=>16,desc=>'Baseplate 16 x 16 (driveway)' },
	      '3857px2' => { baseplate=>1,long=>32,wide=>16,desc=>'Baseplate 16 x 32 (river, dots)' },
	      '6024px1' => { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (canyon, underwater)' },
	      '6024px4' => { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (canyon, dirt)' },
	      '6024px2' => { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (canyon, river)' },
	      '2552px5' => { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (crapp, black rocks)' },
	      '2552px6' => { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (crapp, grey rocks)' },
	      '2552px1' => { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (crapp, ice)' },
	      '6092px4' => { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (rps, ogel)' },
	      '6092px3' => { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (rps, desert)' },
	      '44343px2'=> { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (6 runway)' },
	      '44336px3'=> { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (6 straight runway)' },
	      '44336px2'=> { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (7 road, race pit)' },
	      '2359px1' => { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (7 curve river)' },
	      '2359px3' => { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (7 curve river lagoon)' },
	      '2359p01' => { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (7 curve road)' },
	      '2359px2' => { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (7 curve dirt road)' },
	      80547     => { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (7 straight road)' },
	      '30225cx1'=> { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (7 road, crosswalk)' },
	      30279     => { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (8 straight road)' },
	      30281     => { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (8 curve road)' },
	      '610px1'  => { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (8 road, crosswalk)' },
	      '612p01'  => { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (8 T road)' },
	      '6099p03' => { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (yellow landing)' },
	      '6099px2' => { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (yellow landing 2)' },
	      '608p03'  => { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (yellow T road)' },
	      '309px1'  => { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (gas station road)' },
	      '6100p01' => { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (3 driveways road)' },
	      3947      => { baseplate=>1,long=>32,wide=>32,desc=>'Baseplate 32 x 32 (crater)' },
	      '6261px1' => { baseplate=>1,long=>48,wide=>32,desc=>'Baseplate 32 x 48 (rps mountain)' },
	      '33214px1'=> { baseplate=>1,long=>48,wide=>48,desc=>'Baseplate 48 x 48 (belville rps)' },
	      '30271px4'=> { baseplate=>1,long=>48,wide=>32,desc=>'Baseplate 32 x 48 (raised, yellow,dkgray stones)' },
	      '30271px3'=> { baseplate=>1,long=>48,wide=>32,desc=>'Baseplate 32 x 48 (raised, mountain)' },
	      '30271px2'=> { baseplate=>1,long=>48,wide=>32,desc=>'Baseplate 32 x 48 (raised, rocks)' },
	      '44510px3'=> { baseplate=>1,long=>48,wide=>32,desc=>'Baseplate 32 x 48 (raised beach)' },
	      '30271px5'=> { baseplate=>1,long=>48,wide=>32,desc=>'Baseplate 32 x 48 (raised stone, grass)' },
	      
	      '3070a'   => { tile=>1,long=>1,wide=>1,desc=>'Tile 1 x 1 without Groove' },
	      '3070b'   => { tile=>1,long=>1,wide=>1,desc=>'Tile 1 x 1 with Groove' },
	      '3069a'   => { tile=>1,long=>2,wide=>1,desc=>'Tile 1 x 2 without Groove' },
	      '3069b'   => { tile=>1,long=>2,wide=>1,desc=>'Tile 1 x 2 with Groove' },
	      '2431'    => { tile=>1,long=>4,wide=>1,desc=>'Tile 1 x 4' },
	      '6636'    => { tile=>1,long=>6,wide=>1,desc=>'Tile 1 x 6' },
	      '4162'    => { tile=>1,long=>8,wide=>1,desc=>'Tile 1 x 8' },
	      '3068a'   => { tile=>1,long=>2,wide=>2,desc=>'Tile 2 x 2 without Groove' },
	      '3068b'   => { tile=>1,long=>2,wide=>2,desc=>'Tile 2 x 2 with Groove' },


	      );
}

