#!/usr/bin/perl
use strict;
use warnings;

########################################
###                                  ###
###  IMPORT Twiki to Mindtouch       ###
###                                  ###
########################################
# copyright 2009 Tim Hunt

use HTTP::Request::Common qw{GET POST};

use Dream; # This is the local module with the goodness in it

## Make an object to keep track of all the goodness
my $deki = Dream->new();

##  Set the user and password for the uploads
$deki->user('user');
$deki->pass('password');

##  The site we want to work on
$deki->mode('https://'); # Our mindtouch is 'secure' -- maybe ;)
$deki->site('mindtouch.example.com'); 
$deki->api_url('/@api/deki');
$deki->timezone('Australia/Melbourne');
## Set the Deki parent:( this is the path to all the uploaded files)
$deki->parent('Sections/Observation_Systems/Radar_Engineering');


## Set up a UserAgent with Cookie Jar
$deki->make_agent();

## Here we will go to the TWiki web and get the pages we want to scrape.
$deki->twiki_host('http://twiki.example.com');
$deki->twiki_root('MYWEB'); # the 'web' that we want
$deki->twiki_api('/twiki/bin/view');
$deki->twiki_web('/'.$deki->twiki_root());
$deki->twiki_list('/WebTopicList');
## What is the prefix of the pages we want?
$deki->first_page('MyGroupName');
## where will we promote this page to?
#  (should be similar to the end of $deki->parent above)
$deki->new_home('My Group Name');

$deki->get_page_list();

## Change this line to match the print template for your Twiki
my $twiki_template = '?skin=print.pattern';
my $home_page = 0;
foreach my $page ( @{$deki->twiki_pages()} )
{
	$deki->do_once(0);
	next unless defined $page;
	
	if ($home_page == 0 && $page eq $deki->first_page() )
	{
		$home_page = 1;
		$deki->do_once(1);
	}
##########################################################################
# Now look for revisions:
# - some twiki History counts up in 1 from 1 m{History:.*r(\d+)}
# - Others count up from Revision ...
#### Skipping revisions! Only grab the top page (newest revision)!
##########################################################################
	#my $twiki_page = HTTP::Request->new(
	#			GET => $deki->twiki_host().$twiki_api.$twiki_web.'/'.$page.$twiki_template
	#);
	#my $twiki_content = $deki->agent->request($twiki_page);
	#$twiki_content    = $twiki_content->{_content};
	#$twiki_content    = $deki->html_to_fragment($twiki_content);
	#my ($revisions) = $twiki_content =~ m{History:.*r(\d+)};
	#my ($revisions) = $twiki_content =~ m{Revision.*r1.(\d+)};
	#if (not defined $revisions)
	#{
	#	$revisions = 1;
	#}
#	my $loop_counter = 0;
#	while ( $loop_counter < $revisions) ## change this to a for $foo (1..$revisions) for the ISG web
#	{
#		$loop_counter++; ## delete for ISG web
#		my $revision = $loop_counter;
		my $page_to_get = $deki->twiki_host().$deki->twiki_api().$deki->twiki_web().'/'.$page.$twiki_template;
		print "Uploading $page \n ";
		$deki->grab_and_upload($page_to_get,$page);
		
#	}
}

