#!/usr/bin/perl
use lib '.';
print "Content-Type: text/html\n\n";
print "<font color=red><b>\n";
use Data::Dumper;
$url = "http://www.dallasdesigns.org/ISDbuild/ISDparts.htm";
$text = `lynx -source $url`;

%colortrans = (
	       black => 0,
	       blue => 1,
	       red => 4,
	       gray => 7,
	       dkgray => 8,
	       white => 15,
	       tan => 19,
	       trltblue => 41,
	       clear => 47,
	       trorange => 57,
	       dkred => 320,
	       );

while($text =~ s/<TR bgColor=#(dddddd|eeeeee)>\s*<TD>\s*(\d+)\s*<\/TD>\s*<TD>\s*([A-Za-z0-9\ ]*)\s*<\/TD>\s*<TD>\s*(\S+[A-Za-z0-9\/\\\&\-_\%\;\(\)\:\'\.\?\ ]*\S+)\s*<\/TD>\s*<TD>(.*?)<\/TD>\s*<TD>.*?<\/TD>\s*<TD>\s*([0-9\+\-]*|loose)\s*<\/TD>\s*<\/TR>//is) {
      my($qty,$color,$desc,$img,$bag) = ($2,$3,$4,$5,$6);
      $color =~ s/\s+//g;
      $color = lc $color;
      if($img =~ /\/([A-Za-z0-9]+)\.(jpe?g|gif|png)/i) {
	  $partnum = $1;
      } elsif($img =~ /\/([A-Za-z0-9]+)\.[0-9]+\.(jpe?g|gif|png)/i) {
	  $partnum = $1;
      } else {
	  $partnum = '?';
	  print "Ack! Couldnt find part number for $desc!<br>\n";
      }
      if($img =~ /src=\"images\/(\w+\.?\w+\.jpg)\"/) {
	  $parts{$partnum}{$color}{image} = $1;
      }
      $parts{$partnum}{$color}{qty} = $qty;
      $parts{$partnum}{$color}{totqty} = $qty;
      $parts{$partnum}{desc} = $desc;
      $allcolors{$color}++;
      my(@bags);
      if($bag =~ /^\s*(\d+)\s*$/) {
	  @bags = ($1);
      } elsif($bag =~ /^\s*(\d+)\s*\+\s*(\d+)\s*$/) {
	  @bags = ($1,$2);
      } elsif($bag =~ /^\s*(\d+)\s*\-\s*(\d+)\s*$/) {
	  my($b1,$b2) = ($1,$2);
	  for($i=$b1;$i<=$b2;$i++) {
	      push @bags,$i;
	  }
      } else {
	  print "PANIC! Part $desc ($partnum) has invalid bag: \"$bag\"<br>\n";
      }
      $bi = 0;
      for($i=0;$i<$qty;$i++) {
	  $parts{$partnum}{$color}{bags}{$bags[$bi]}++;
	  $allbags{$bags[$bi]}{parts}{$partnum}{$color}++;
	  $allbags{$bags[$bi]}{totparts}{$partnum}{$color}++;
	  $bi++;
	  if($bi >= @bags) { $bi = 0; }
      }
  }

%sections = (
	     frame => { url => "ISDbypage2-29.htm", name => "Frame", },
	     framelegs => { url => "ISDbypage30-39.htm", name => "Frame Legs", },
	     engines => { url => "ISDbypage40-57.htm", name => "Engines", },
	     bottomdeck => { url => "ISDbypage58-75.htm", name => "Bottom Hull", },
	     decksub => { url => "ISDbypage100-102.htm", name => "Rear Hull", },
	     topdeck => { url => "ISDbypage105-123.htm", name => "Top Hull", },
	     ssl1 => { url => "ISDbypage151-188.htm", name => "Lower SuperStructure", },
	     ssl2a => { url => "ISDbypage189-196.htm", name => "Bridge Support", },
	     ssl2b => { url => "ISDbypage197-201.htm", name => "Bridge Frame", },
	     ssl2c => { url => "ISDbypage202-207.htm", name => "Bridge Hull", },
	     ssl2d => { url => "ISDbypage208-209.htm", name => "Bridge Topper", },
	     ssl2e => { url => "ISDbypage210-219.htm", name => "Upper Superstructure", },
	     rbr => { url => "ISDbypage221-222.htm", name => "Blockade Runner", },
	     sign => { url => "ISDbypage223-225.htm", name => "Sign", },
	     );
foreach $section (keys %sections) {
    my $url = "http://www.dallasdesigns.org/ISDbuild/$sections{$section}{url}";
    my $text = `lynx -source $url`;
    $text =~ s/<font.*?>//sig;
    $text =~ s/<\/font>//sig;
    $text =~ s/<center>//sig;
    $text =~ s/<\/center>//sig;
    $text =~ s/<br>//sig;
    while($text =~ s/<TR.*?>\s*<TD.*?\>\s*(\d+)\s*<\/TD>\s*<TD.*?\>\s*([A-Za-z0-9\ ]*)\s*<\/TD>\s*<TD.*?>\s*(\S[A-Za-z0-9\/\\\&\-_\%\;\(\)\:\'\.\?\#\ ]*\S)\s*<\/TD>\s*<TD.*?>(.*?)<\/TD>\s*<TD.*?>(.*?)<\/TD>\s*<TD.*?>\s*([0-9\+\-]*|loose)\s*<\/TD>\s*<\/TR>//is) {
	  my($qty,$color,$desc,$img,$note,$bag) = ($1,$2,$3,$4,$5,$6);
	  if($img =~ /\/([A-Za-z0-9]+)\.(jpe?g|gif|png)/i) {
	      $partnum = $1;
	  } elsif($img =~ /\/([A-Za-z0-9]+)\.[0-9]+\.(jpe?g|gif|png)/i) {
	      $partnum = $1;
	  } else {
	      $partnum = '?';
	      print "Ack! No part number found for $section, ($qty,$color,$desc,$img,$note,$bag)!<br>\n";
	  }
	  $color =~ s/\s+//g;
	  $color = lc $color;
	  my $flag = 0;
	  $note =~ s/<font.*?>//g;
	  $note =~ s/<\/font>//g;
	  while($note =~ s/step\s*\d+(\s*[\/\\]\s*\d+)?\s*\-\s*qty\s*(\d+)//is) {
	      $sections{$section}{parts}{$partnum}{$color} += $2;
	      $parts{$partnum}{$color}{sections}{$section} += $2;
	      $sections{$section}{totparts}{$partnum}{$color} += $2;
	      $parts{$partnum}{$color}{totsections}{$section} += $2;
	      $flag = 1;
	  }
	  if(!$flag) { print "Ack! $section, $desc didn't use the piece in any steps!<br>\n"; }
      }
    #print "Section $section: ".Dumper($sections{$section});
}
print "</b></font><br>\n";
if($ENV{QUERY_STRING} =~ /bag/i) {
    foreach my $bag (sort {$a <=> $b} (keys %allbags)) {
	my %secneeds = ();
	foreach $section (sort keys %sections) {
	    foreach $partnum (sort keys %{$sections{$section}{parts}}) {
		foreach $color (sort keys %{$sections{$section}{parts}{$partnum}}) {
		    next if(!$sections{$section}{parts}{$partnum}{$color});
		    if($allbags{$bag}{parts}{$partnum}{$color}) {
			if($allbags{$bag}{parts}{$partnum}{$color} < $sections{$section}{parts}{$partnum}{$color}) {
			    $secneeds{$section} += $allbags{$bag}{parts}{$partnum}{$color};
			    $sections{$section}{parts}{$partnum}{$color} -= $allbags{$bag}{parts}{$partnum}{$color};
			    $allbags{$bag}{parts}{$partnum}{$color} = 0;
			} else {
			    $allbags{$bag}{parts}{$partnum}{$color} -= $sections{$section}{parts}{$partnum}{$color};
			    $secneeds{$section} += $sections{$section}{parts}{$partnum}{$color};
			    $sections{$section}{parts}{$partnum}{$color} = 0;
			}
		    }
		}
	    }
	}
	my($tot) = 0;
	foreach $section (keys %secneeds) {
	    $tot += $secneeds{$section};
	}
	print "<font color=blue><b>Bag $bag:</b></font><br>\n";
	foreach $section (sort {$secneeds{$b} <=> $secneeds{$a}} (keys %secneeds)) {
	    my $per = sprintf("%.2f",($secneeds{$section}/$tot)*100);
	    print "  $per\% - $sections{$section}{name}<br>\n";
	}
    }
} else {
    foreach my $partnum (sort {$parts{$a}{desc} cmp $parts{$b}{desc} } (keys %parts)) {
	next if(!$partnum);
	foreach my $color (sort (keys %{$parts{$partnum}})) {
	    my %secneeds = ();
	    next if($color eq "desc");
	    foreach $section (sort keys %sections) {
		next if(!$sections{$section}{parts}{$partnum}{$color});
		if($parts{$partnum}{$color}{qty}) {
		    if($parts{$partnum}{$color}{qty} < $sections{$section}{parts}{$partnum}{$color}) {
			$secneeds{$section} += $parts{$partnum}{$color}{qty};
			$sections{$section}{parts}{$partnum}{$color} -= $parts{$partnum}{$color}{qty};
			$parts{$partnum}{$color}{qty} = 0;
			print "<font color=red><b>Ack! Ran out of $parts{$partnum}{desc}!</b></font><br>\n";
		    } else {
			$parts{$partnum}{$color}{qty} -= $sections{$section}{parts}{$partnum}{$color};
			$secneeds{$section} += $sections{$section}{parts}{$partnum}{$color};
			$sections{$section}{parts}{$partnum}{$color} = 0;
		    }
		}
	    }
	    if($parts{$partnum}{$color}{qty}) {
		print "<font color=red><b>$parts{$partnum}{desc} $color has $parts{$partnum}{$color}{qty} extra!</b></font><br>\n";
	    }
	    my($tot) = 0;
	    foreach $section (keys %secneeds) {
		$tot += $secneeds{$section};
	    }
	    print "<table border=1>\n";
	    print "<tr><td colspan=3><font color=blue><b>Part: $color $parts{$partnum}{desc} ($parts{$partnum}{$color}{totqty} total):</b></font></td></tr>\n";
	    print "<tr><td colspan=3>\n";
	    if($parts{$partnum}{$color}{image}) {
		print "<img width=50\% src=\"http://peeron.com/pics/inv/custpics/$parts{$partnum}{$color}{image}\">\n";
	    } else {
		print "<img src=\"http://ldraw.org/library/official/images/$colortrans{$color}/$partnum.png\">\n";
	    }
	    print "</td></tr>\n";
	    print "<tr><td>Qty.</td><td>\%</td><td>Section</td></tr>\n";
	    foreach $section (sort {$secneeds{$b} <=> $secneeds{$a}} (keys %secneeds)) {
		my $per = sprintf("%.2f",($secneeds{$section}/$tot)*100);
		print "<tr><td>$sections{$section}{totparts}{$partnum}{$color}</td>\n";
		print "<td>$per\%</td><td>$sections{$section}{name}</td></tr>\n";
	    }
	    print "</table>\n";
	}
    }
}
