#!/usr/bin/perl

# Copyright (c) 2004 Alan W. Mahoney. All rights reserved. 
# This program is free software.  You may modify and/or 
# distribute it under the same terms as Perl itself.  
# This copyright notice must remain attached to the file.  

# items.pl
# Pull item names and various id numbers from a well-stocked store
# and build an items file
# 22 July 2004

# this code could also form the basis of a shop price parser

my $VERSION = 0.5;
my $ID = '$Id: items.pl 67 2004-11-29 00:53:06Z alan $';

use KoLBot::Item;

if (@ARGV > 1) {
  print "usage: items.pl storesouce.html\n\n";
  print "  Prints the resulting item database\n";
  print "  If storeresource.html is omittied, items.html is assumed\n";
  print "  You may wish to redirect the output to a suitable file\n";
}

my $in_file = $ARGV[0] || 'items.html';

open INF,"<",$in_file or die "Input file $in_file doesn't exist";
my $content;
while(<INF>) {
  $content .= $_;
}
close INF;

$content =~ s/\n//g;

$content =~ m|<td><b>Price</b>.*?<tr>(.*)</tr><tr><td colspan=4 align=center>|;
$content = $1;

my @lines = split /<\/tr><tr>/,$content;

my @items;

for (@lines) {
  # number in store and price come after this stuff
  # price also occurs in the \d{9} part.
  m|value=(\d+)\d{9}>.*whichitem=(\d+)",".*<b>(.*)</b>|;
  push @items,new KoLBot::Item(name => $3,
			       id_number => $1,
			       desc_number => $2);
}

for (@items) {
  my $space = $_->{id} < 10 ? "  " : $_->{id} < 100 ? " " : "";
  print $_->{desc_number}, " ",$space,$_->{id_number}, ' ', $_->{name},"\n";
} 
