#!/usr/bin/perl -w
use strict;

#use Dumpvalue;
#my $d= new Dumpvalue;
$|=1;
my $bb= BoeteBase->new("http://www.openbaarministerie.nl/cgi-bin/boetebase/boetebase.pl");


for my $top ("Binnenvisserij", "Drank/Horeca", "Milieu", "Verkeer op de weg", "Verkeer te water", "Drugs", "Overige") {
    $bb->walkPages($top);
}

exit(0);

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

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

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

    $ua->cookie_jar(HTTP::Cookies->new());

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

# get page from boetebase, params =
#    {stap=>n, veldnrN=>"...", ...}
sub getPage {
    my $self= shift;
    my $params= shift;

    my $uri= URI->new($self->{url});
    $uri->query_form(%$params);
    my $rq= GET $uri;

    my $rp= $self->{ua}->request($rq);

    return $rp->content;
}

# returns form in page.
sub parseForm {
    my $self= shift;
    my $html= shift;

    $html =~ s/\s+/ /g;

    my %form;
    for my $inputtag ($html =~ /(<input(?:\s*\w+\s*=\s*(?:'[^']*'|"[^"]*"|[^ >]*))*>)/gsi) {
        my $input= $self->parseInput($inputtag);
        push @{$form{$input->{name}}}, $input->{value};
    }
    return \%form;
}

sub parseInput {
    my $self= shift;
    my $inputtag= shift;

    my %input;
    for my $keyval ($inputtag =~ /(\w+\s*=\s*(?:'[^']+'|"[^"]+"|[^ '">]+))/gsi) {
        my $attribute= $self->parseAttribute($keyval);
        $input{$attribute->{key}}= $attribute->{value};
    }

    return \%input;
}

sub parseAttribute {
    my $self= shift;
    my $keyval= shift;
    if (my ($key, $val)= $keyval =~ /(\w+)\s*=\s*('[^']+'|"[^"]+"|[^ '">]+)/si) {
        $val =~ s/^['"]//;
        $val =~ s/['"]$//;
        return {key=>$key, value=>$val};
    }
    return undef;
}

sub parseText {
    my $self= shift;
    my $page= shift;

    $page =~ s/<br>/\n/g;
    $page =~ s/(?:<\s*\w+(?:\s*\w+\s*=\s*(?:'[^']*'|"[^"]*"|[^ >]*))*>)+/ /g;

    my @lines;
    for my $line (split /\r\n|\r|\n/, $page) {
        if ($line !~ /^\s+$/) {
            push @lines, $line;
        }
    }

    return join("\n", @lines);
}

sub pathToParams {
    my $self= shift;
    my @path= @_;
    
    my %params= (stap=>scalar @path);
    for my $i (0..$#path) {
        $params{"veldnr".($i+1)}= $path[$i];
    }

    return \%params;
}

sub walkPages {
    my $self= shift;
    my @path= @_;

    print "getting ", join(":", @path), "\n";
    print "-" x 77, "\n";

    my $params= $self->pathToParams(@path);

    #print "params\n\n";
    #$d->dumpValue($params);
    #print "-" x 77, "\n";

    my $page= $bb->getPage($params);

    my $text= $bb->parseText($page);

    print "$text\n";
    print "-" x 77, "\n";

    my $form= $bb->parseForm($page);

    #print "form\n\n";
    #$d->dumpValue($form);
    #print "-" x 77, "\n";

    for my $veld (@{$form->{"veldnr$form->{stap}[0]"}}) {
        $bb->walkPages(@path, $veld);
    }
}

