#!perl
use strict;
use LWP::UserAgent;
use XML::LibXML;
use XML::LibXML::XPathContext;
use FindBin;
use Cwd;

$|++;
my $USER = "admin";
my $PASS = "tadoadsty";
my $AUTH_URL = "http://$USER:$PASS\@fm.biblhertz.it/fmi/xml/fmresultset.xml";
my $URL = "http://fm.biblhertz.it/fmi/xml/fmresultset.xml";
my $Q_DB_NAMES = "?-dbnames";
my $Q_LAY_NAMES = "?-layoutnames&-db=";
my $Q_FINDALL = "?-findall&-db=";

my $BIN = $FindBin::Bin;
my $CWD = cwd();
chdir($BIN);

my $dbNames = fetchDbNames();
for my $db (@$dbNames) {
	my $layouts = fetchLayoutsForDb($db);
	for my $lay (@$layouts) {
		fetchData($db, $lay);
		}
	}
exit;

# ---------------------
sub fetchURL {
# ---------------------
	my ($url) = @_;
	my $ua = LWP::UserAgent->new();
	my $response = $ua->get($url);
	if ($response->is_success) {
		return $response->content;
	     }
	else {
		print "Response: ", $response->status_line, "\n";
		return undef;
	     }
}

# ---------------------
sub fetchURL {
# ---------------------
	my ($url) = @_;
	my $ua = LWP::UserAgent->new();
	my $response = $ua->get($url);
	if ($response->is_success) {
		return $response->content;
	     }
	else {
		print "Response: ", $response->status_line, "\n";
		return undef;
	     }
}

# ---------------------
sub fetchFile {
# ---------------------
	my ($url, $filename) = @_;
	my $ua = LWP::UserAgent->new();
	my $response = $ua->get($url, ':content_file' => $filename);
	if (!$response->is_success) {
		print "Response: ", $response->status_line, "\n";
		return undef;
	     }
}

# ---------------------
sub fetchDbNames {
# ---------------------
	my $xml = fetchURL("$URL$Q_DB_NAMES");
	my $parser = XML::LibXML->new();
	my $doc = $parser->parse_string($xml);
	my $xc = XML::LibXML::XPathContext->new($doc);
	$xc->registerNs('fm', 'http://www.filemaker.com/xml/fmresultset');
	my @names = grep { $_ =~ /^x/ } map { $_->nodeValue() } $xc->findnodes("//fm:data/text()")->get_nodelist();
	return [@names];
	}
	
# ---------------------
sub fetchLayoutsForDb {
# ---------------------
	my ($db) = @_;
	my $url = "$URL$Q_LAY_NAMES$db";
	# print "$url\n";
	my $xml = fetchURL($url);
	my $parser = XML::LibXML->new();
	my $doc = $parser->parse_string($xml);
	my $xc = XML::LibXML::XPathContext->new($doc);
	$xc->registerNs('fm', 'http://www.filemaker.com/xml/fmresultset');
	my @names = grep { $_ =~ /^export/ } map { $_->nodeValue() } $xc->findnodes("//fm:data/text()")->get_nodelist();
	# print "$db:\n  @names\n";
	return [@names];
	}

# ---------------------
sub fetchData {
# ---------------------
	my ($db, $layout) = @_;
	my $url = "$URL$Q_FINDALL$db&-lay=$layout";
	# print "$url\n";
	my $filename = "$db-$layout.xml"; 
	print "-> $filename\n";
	my $xml = fetchFile($url, $filename);
	return;
	# ----------------------
	# print "$url\n";
	my $parser = XML::LibXML->new();
	my $doc = $parser->parse_string($xml);
	my $xc = XML::LibXML::XPathContext->new($doc);
	$xc->registerNs('fm', 'http://www.filemaker.com/xml/fmresultset');
	$doc->toFile("$db-$layout.xml", 1);
	}


__FILE__
