#!/usr/bin/perl

# .pl - 

use lib ("$ENV{SMARTVOTER_PL}");
require "countyabb.pl";

use Getopt::Std; getopts('ht'); 

if ($opt_h) { print <<"END"; exit(0); }
Usage: 
    Options:
END

require HTTP::Request;
use LWP::UserAgent;
$ua = LWP::UserAgent->new;
#$request = HTTP::Request->new($opt_h?"HEAD":"GET"=>$url);
#$response = $ua->request($request);
#$content = $response->content;
#$response = $ua->get($url);
#$response = $ua->mirror($url,$filename);
#$response = $ua->head($url);


use POSIX;
$date =  POSIX::strftime("%Y%m%d",localtime(time));

$response = $ua->get("http://swdb.berkeley.edu/d10/g10_geo_conv.html");
foreach $_ (split(/<\/tr>/,$response->content)) {
    next unless ($cn,$cname,$urls)=m`<th>County (\d+)<br />\((.*?)\)</th>.*?<a href='([^<>]+.dbf)'>`;
    $cn=~s/^0(\d\d)/$1/;
    $cname=~s/Mendecino/Mendocino/;
    $stcty = countyname2abb("$cname County, CA");
    $fips = countyfips($stcty);
    ($f) = $urls=~m`/([^/]+)$`;
    print "$stcty: $cn $cname=$f\n";
    next if $opt_t;
    $url = "http://swdb.berkeley.edu$urls";
    $response = $ua->mirror($url,$f);
    print "Fetch $url\n";
    next unless -s $f > 1000;
    system("dbf2tsv.pl $f|gzip -c >rg_pctblk$fips.tsv.gz");
}



1;
