#!/usr/bin/perl

use LWP::UserAgent;
use XML::Simple;
use Data::Dumper;

my $path = "/tmp/vcl_wiki_content";
my $startindex = 0;
my $downloadcnt = 25;

if(! -e "$path/$filename") {
	mkdir $path
}

my $browser = LWP::UserAgent->new(
	ssl_opts => {verify_hostname => 0}
);
my $apiurl = "https://cwiki.apache.org/confluence/rest/prototype/1/search/site?type=page&spaceKey=VCL&startIndex=$startindex&pageSize=$downloadcnt";

sub getContent {
	my ($key, $id, $title) = @_;
	my $filename = $title;
	$filename =~ s/\s/-/g;
	$filename =~ s/"//g;
	$filename =~ s/'//g;
	$filename = lc($filename) . ".cwiki";
	$filename =~ s/\(//g;
	$filename =~ s/\)//g;
	$filename =~ s/&//g;
	#print "filename: $path/$filename\n";
	if(-e "$path/$filename") {
		print "skipping $filename\n";
		return;
	}
	$contenturl = "https://cwiki.apache.org/confluence/rest/prototype/1/content/$id";
	my $response = $browser->get($contenturl);
	if(! $response->is_success) {
		return;
	}

	# TODO check content for macro including children
	# <ac:macro ac:name="children"><ac:parameter ac:name="all">true</ac:parameter><ac:parameter ac:name="style">h3</ac:parameter></ac:macro>
	# and create a page from the child pages

	my $data = XMLin($response->content);
	#print $data->{'title'} . "\n";
	my $title = $data->{'title'};
	$title =~ s/"//g;
	$title =~ s/'//g;
	open(FILE1, ">$path/$filename") || die "failed to open $path/$filename";
	print FILE1 $data->{'body'}->{'content'} . "\n" || die "failed to write to $path/$filename";
	close(FILE1);
	print "running './convert_cwiki_markup.pl $key \"$title\" $path/$filename'\n";
	my $out = `./convert_cwiki_markup.pl $key "$title" $path/$filename`;
	print "convert output: $out\n";
	#print $data->{'body'}->{'content'} . "\n";
	#print "==========================================================\n";
	sleep 10;
}

my $response = $browser->get($apiurl);
die 'Error getting content' unless $response->is_success;

my $data = XMLin($response->content);
foreach $key (keys %{$data->{'result'}}) {
	#print "Title: " . $data->{'result'}->{$key}->{'title'} . "\n";
	foreach $link (@{$data->{'result'}->{$key}->{'link'}}) {
		if(my @id = $link->{'href'} =~ /confluence\/rest\/prototype\/1\/content\/([0-9]+)$/) {
			#print $id[0] . ": " . $link->{'href'} . "\n";
			getContent($key, $id[0], $data->{'result'}->{$key}->{'title'});
		}
	}
}

=head1 LICENSE

           Licensed to the Apache Software Foundation (ASF) under one
           or more contributor license agreements.  See the NOTICE file
           distributed with this work for additional information
           regarding copyright ownership.  The ASF licenses this file
           to you under the Apache License, Version 2.0 (the
           "License"); you may not use this file except in compliance
           with the License.  You may obtain a copy of the License at

             http://www.apache.org/licenses/LICENSE-2.0

           Unless required by applicable law or agreed to in writing,
           software distributed under the License is distributed on an
           "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
           KIND, either express or implied.  See the License for the
           specific language governing permissions and limitations
           under the License.
