#!/usr/bin/perl
# Author:	David Ljung Madison <DaveSource.com>
# See License:	http://MarginalHacks.com/License

$NETSCAPE_WRAPPER="/usr/local/bin/netscape";

my $DEFAULT_CITY="San Francisco";
my $DEFAULT_STATE="Ca";

$MAP_URL="http://www.mapblast.com/mblast/map.mb?loc=us\\&CMD=GEO";

my $zip  = pop(@ARGV) if ($ARGV[-1] =~ /^\d{5}(-\d+)?$/);
my $addr = shift(@ARGV);
$addr.=" ".shift(@ARGV) if ($addr =~ /^\d+$/);
$addr.=" ".shift(@ARGV) if ($ARGV[0] =~ /^(st(reet)?|av(e(nue)?)?)$/);
my $state= pop(@ARGV) if ($ARGV[-1] =~ /^[a-z]{2}$/i);
my $city = pop(@ARGV);
$city=pop(@ARGV)." $city" if ($ARGV[-1] =~ /^(san(ta)?|l[ao]s)$/i);
$city=pop(@ARGV)." $city" if (lc($city) eq "city");
# Rest goes into address
$addr.=" ".join(' ',@ARGV);

# Remove trailing commas
$addr =~ s/,$//;
$city =~ s/,$//;

my $AD2="&AD2=${addr}";
my $AD3="&AD3=".($city ? $city : $DEFAULT_CITY)." ".
                ($state ? $state : $DEFAULT_STATE).($zip ? " $zip" : "");
my $query=$AD2;
$query.=$AD3 if ($AD3);

# HTMLize the query
$query =~ s/ /+/g;
$query =~ s/,/%2C/g;

# System call will use &
$query =~ s/&/\\&/g;

#print("$NETSCAPE_WRAPPER ${MAP_URL}${query}\n");
system("$NETSCAPE_WRAPPER ${MAP_URL}${query}");
