#!/usr/bin/perl

use strict;
use HTML::HTML5::Builder ':standard';
use RDF::Trine;
use RDF::QueryX::Lazy;

my $source = q<http://danbri.org/foaf.rdf>;
my $data   = RDF::Trine::Model->new;

RDF::Trine::Parser->parse_url_into_model($source => $data);

my ($who, $name);
my $result_name = RDF::QueryX::Lazy->new(<<SPARQL)->execute($data);
SELECT ?who ?name
WHERE {
        <$source> foaf:primaryTopic ?who .
        ?who foaf:name ?name .
}
SPARQL

if (my $result = $result_name->next)
{
        ($who, $name) = @{$result}{qw/who name/};
}

my $result_mboxes = RDF::QueryX::Lazy->new(<<SPARQL)->execute($data);
SELECT ?mbox
WHERE {
        <$source> foaf:primaryTopic ?who .
        ?who foaf:mbox ?mbox .
}
SPARQL

print "Content-Type: text/html\r\n\r\n";
print html(
        head(title($name->literal_value)),
        body(
                -about => $who->uri,
                -vocab => 'http://xmlns.com/foaf/0.1/',
                h1(-property => 'name', $name->literal_value),
                h2('Mailboxes'),
                ul(-rel => 'mbox',
                        map {
                                my $e = $_->{mbox}->uri;
                                li(a(-href=>$e, $e))
                        } $result_mboxes->get_all
                ),
        )
);