#!/usr/bin/perl -w
use strict;
$|=1;
use Dumpvalue;
my $d= Dumpvalue->new();

my $svr= CiaServer->new("http://www.cia.gov/cia/publications/factbook");

my $book= Factbook->new($svr);
$d->dumpValue($book);

exit 0;

package CiaServer;

use HTTP::Request::Common qw(POST GET);
use LWP::Simple;
use LWP::UserAgent;
use HTTP::Cookies;

sub new {
    my ($class, $baseurl)= @_;

    my $ua= LWP::UserAgent->new();

#    $ua->agent("Mozilla/4.76 [en] (X11; U; Linux 2.4.9 i686)");
#    $ua->cookie_jar(HTTP::Cookies->new());
#    $ua->env_proxy();

    return bless {
        ua=>$ua,
        baseurl=>$baseurl,
    }, $class;
}


sub getContentMenu {
    my ($self)= @_;
    my $idx= "$self->{baseurl}/menugeo.html";

    my $rq= GET $idx;
    my $rp= $self->{ua}->request($rq) or die "error reading menu";

    return $rp->content;
}

sub getCountryPage {
    my ($self, $country)= @_;
    my $idx= "$self->{baseurl}/geos/$country.html";

    my $rq= GET $idx;
    my $rp= $self->{ua}->request($rq) or die "error reading country";

    return $rp->content;
}


package Factbook;

use HTML::TreeBuilder;

sub new {
    my ($class, $htmlsvr)= @_;

    my $self= bless {
        htmlsvr=>$htmlsvr,
    }, $class;

    $self->{index}= $self->parseContents($htmlsvr->getContentMenu());

    return $self;
}

sub parseContents {
    my ($self, $html)= @_;

    my %countries;

    my $h = HTML::TreeBuilder->new();
    $h->parse($html);
    $h->eof();
    # tree-builder address: 0.1.$letter.1.$country.0
    for my $ahref ($h->find_by_tag_name("a")) {
        if ($ahref->address() =~ /^0\.1\.\d+\.1\.\d+\.0$/) {
            my $url=$ahref->{href};
            my $name=join " ", $ahref->content_list();
            my ($cn)= ($url =~ m{geos/(\w+)\.htm}) or die "invalid url: $url\n";
            $countries{$cn}= {url=>$url, name=>$name};
        }
    }
    return \%countries;
}
