#!/usr/bin/perl

use 5.010;
use strict;
use warnings;
use Data::Dumper;
#use XML::LibXML;
use XML::Simple;

my $filename = 'wp-sitemap.xml';

`wget -q https://kvernelandbil.no/wp-sitemap.xml -O wp-sitemap.xml`;

#my $dom = XML::LibXML->load_xml(location => "./wp-sitemap.xml");
#warn Dumper $dom;
#foreach my $loc ($dom->findnodes('/sitemapindex/sitemap')) {
#   say $loc->to_literal();
#}

my $xml = new XML::Simple;

my $data = $xml->XMLin("./wp-sitemap.xml");
# print Dumper($data);

foreach my $lok (@{$data->{sitemap}}) {
#	print Dumper $lok;
	print "\n\n";
	print qq!SEKSJON: !;
	print $lok->{loc};
	print qq!\n!;
	
	my $sec = $lok->{loc};
	my $filnavn = $sec;
	$filnavn =~ s/^.*\///;
	`wget -q $sec -O $filnavn`;
	$sec =~ s/.*\///;
	
	my $xml2 = new XML::Simple;
	my $data2 = $xml2->XMLin("./$sec");
	
#	print Dumper $data2;

#	if (! @{$data2->{url}} || ! scalar @{$data2->{url}}) { next; }
#	else { print qq!TEST: !; print scalar @{$data2->{url}}; print qq!\n!; }
	
	if (! ref $data2->{url}) { next; }
	
#	print ref $data2->{url};
#	print qq!\n!;

	foreach my $lok2 (@{$data2->{url}}) {
#		print "SUB: ";
		print $lok2->{loc};
		print "\n";
		}
	}
