#!/usr/bin/perl

## Directly turns the output of the USPS zone chart HTML output into
## a valid UPS-style lookup table.
##
##  usage: makezone <input.html  >450.csv
##

use Getopt::Std;

getopts('m:t');

$opt_m ||= 'Priority Book';

my @modes = grep /\S/, split /\s+/, $opt_m;

undef $/;
my $data = <>;

my $max = 1;

my $delim = $opt_t ? "\t" : ',';

print join $delim, 'DestZip', @modes, 'bmc';
print "\n";

sub process_row {
	my ($range, $zone) = @_;
	
	my $bmc = $zone =~ s/\s*\*\s*// ? 1 : 0;

	if($range !~ s/\.+/-/) {
		unless ($range =~ /^\d+$/) {
			warn "Bad zone range=$range, skipping.\n";
			return;
		}
		$range = "$range-$range";
	}
	my @cells = $range;
	for(@modes) {
		push @cells, $zone;
	}
	push @cells, $bmc;
	my $out = join $delim, @cells;
	$out .= "\n";
}

while($data =~ s{<tr.*?>(.*?)</tr>}{}is) {
	my $row = $1;
	my @cells;
	my $i = 0;
	while($row =~ s{<td.*?>(.*?)</td>}{}is) {
		my $col = $1;
		$col =~ s{</?\w.*?>}{}gs;
		$col =~ s/^\s+//;
		$col =~ s/\s+$//;
		push @cells, $col;
		if($i++ >= $max) {
			$line = process_row(@cells)
				and push @out, $line;
			@cells = ();
			$i = 0;
		}
	}
	if(@cells) {
		$line = process_row(@cells)
				and push @out, $line;
	}
}

print sort @out;
