#!/cygdrive/c/strawberry/perl/bin/perl -w

# Need to specify the full path of Perl above, e.g. for Windows something like
#!C:\\Perl32\\bin\\perl -w

##
# This is used for exporting individual Greenstone
# documents to Expeditee. To export entire collections,
# use html-to-expeditee.pl
##
use strict;
my $iis6_mode = 0;

if($iis6_mode){
	#Change into cgi-bin directory
	chdir("cgi-bin");
}

eval("require \"gsdlCGI.pm\"");
if ($@)
{
    print STDOUT "Content-type:text/plain\n\n";
    print STDOUT "ERROR: $@\n";
    exit 0;
}

sub display_output
{
	my($docID,$c,$assocfilepath,$site,$fn,$found) = @_;
	
	my $iframeURL = "library/collection/$c/document/$docID"."?p.showAssocFilePath=1";
	
	#Render an html page to display & export item.
	my $html = <<EOT;
<html>
	<head>
		<title>Export Item to Expeditee</title>
		<base href=".."/>
		
		<link type="text/css" href="ext/html-to-expeditee/jquery/css/le-frog/jquery-ui-1.8.16.custom.css" rel="stylesheet"/>
		<script type="text/javascript" src="ext/html-to-expeditee/jquery/js/jquery-1.6.2.min.js"></script>
		<script type="text/javascript" src="ext/html-to-expeditee/jquery/js/jquery-ui-1.8.16.custom.min.js"></script>
		<script type="text/javascript" src="ext/html-to-expeditee/js/html-to-expeditee.js"></script>
		<script type="text/javascript" src="ext/html-to-expeditee/js/gsajax-min.js"></script>
		
		<style type="text/css">
		body{
			font-family: Arial;
			font-size: 16px;
			color: #000;
		}
		</style>
	<script type="text/javascript">
	function load(){
	
		var iframe = document.getElementById("iframe");
		if(iframe.style.display != "block"){
			iframe.height = "90%";
			iframe.style.display = "block";
		}
		
		var iframeDoc;
		if( iframe.contentDocument){
			/* Firefox & Chrome */
			iframeDoc = iframe.contentDocument;
		}else if(iframe.contentWindow){
			/* IE */
			iframeDoc = iframe.contentWindow.document;
		}
		
		var gsContent = iframeDoc.getElementById("gs_content");
						
		//hide rightSidebar div
		var rightSidebar = iframeDoc.getElementById("rightSidebar");
		if(rightSidebar !== undefined){
			rightSidebar.style.display = "none";
		}
		
		var expFrameTree = htmlToExpeditee(gsContent);
		var expFrame = JSON.stringify(expFrameTree);
		
		var site = "$site";
		var url = "cgi-bin/export-individual-expeditee.pl";
		var params = "c=" + "$c";
		
		if(site.match(/\\w/)){
			params += "&site=" + site;
		}
		params += "&a=generate-frame&fn=" + "$fn";
		params += "&found=" + "$found";
		params += "&json=" + escape(expFrame);
		
		var request = urlPostSync(url,params);
		
		alert("Document has been successfully exported to Expeditee. It can be found in the collection's export directory at: $fn.exp");
	}
	</script>
	</head>
	<body>
	
	<p id="msg">Exporting following document to Expeditee... </p>
	
	<hr style="margin: 10px;"/>
	
	<!-- Load up an iframe to display URL in-->
	<div align="center"><iframe id="iframe" width="1000px" height="90%" src="$iframeURL" onload="load();"></iframe</div>
	
	</body>
</html>
EOT
	print "Content-type:text/html\n\n";
	print $html;
}

##For debugging.
sub check{

	my($check1,$check2,$check3,$check4,$check5) = @_;

	print "Content-type:text/html\n\n";
	print $check1;
	print $check2;
	print $check3;
	print $check4;
	print $check5;
}

sub main
{
	my $gsdl_cgi = new gsdlCGI();
	
	#Load needed GSDL modules
	$gsdl_cgi->setup_gsdl();
	
	my $gsdlhome = $ENV{'GSDLHOME'};
	$gsdl_cgi->checked_chdir($gsdlhome);
	
	#require cgiactions::HtmlToExpediteeAction;
	require cgiactions::ExpediteeExportAction;
	$gsdl_cgi->parse_cgi_args();
	
	$gsdl_cgi->{'xml'} = 0;
	
	my $fn = $gsdl_cgi->param("fn");

	if(defined $fn){
		
		my $site = $gsdl_cgi->clean_param("site");
		my $c = $gsdl_cgi->clean_param("c");
		my $a = $gsdl_cgi-> clean_param("a");
		my $json = $gsdl_cgi->clean_param("json");
		my $found = $gsdl_cgi->clean_param("found");
		#check($site,$c,$a,$json,$found);
		
		my $action = new ExpediteeExportAction($gsdl_cgi,$iis6_mode);
		$action->do_action();
		
		#my $action = new HtmlToExpediteeAction($gsdl_cgi,$iis6_mode);
		#$action->do_action();
	}else{
		
		printf("fn not defined\n");
		my $docID = $gsdl_cgi->clean_param("docID");
		my $c = $gsdl_cgi->clean_param("c");
		my $assocfilepath = $gsdl_cgi->clean_param("assocfilepath");
		my $site = $gsdl_cgi->clean_param("site");
		
		if(!defined $site){
			$gsdl_cgi->generate_error("No site specified.");
		}
		
		#Open export directory
		my $collect_dir = $gsdl_cgi->get_collection_dir($site);
		my $export_dir = &util::filename_cat($collect_dir,$c,"export");
		
		my $frameInf = "$export_dir\\frame.inf";
		my $fn = "";
		my @files;
		
		#check if export directory exists otherwise create it.
		if(-d $export_dir){
			opendir DIR, $export_dir or die "cannot open dir $export_dir: $!";
			@files = readdir DIR;
			closedir DIR;
		}else{
			print "Reached!\n";
			&util::mk_dir($export_dir);
			
			my $zeroFrame = "0.exp";
			open(ZERO_FRAME,">>$export_dir\\$zeroFrame") || die "$!";
			close ZERO_FRAME;
			
			opendir DIR, $export_dir or die "can't open dir $export_dir: $!\n";
			@files = readdir DIR;
			closedir DIR;
		}
		
		my $found = 0;
		for my $file(@files){
			if($file =~ m/\d/){
				
				my $fullFile = "$export_dir\\$file";
				open FILE, $fullFile or die $!;
				my @lines = <FILE>;
				
				for my $line(@lines){
					if($line eq "T \@assocfilepath $assocfilepath\n"){
						$fn = $fullFile;
						$found = 1;
					}
				}
				close(FILE);
			}
		}
		
		unless($found){
			#use frame.inf file to get file number
			if(-e $frameInf){
				
				open FRAME_INF, $frameInf or die $!;
				my @lines = <FRAME_INF>;
				
				for my $line(@lines){
					$fn = $line;
				}
			}else{
				
				print "$frameInf not found\n";

				$fn = 0;
			}
		}
		
		#now get frame number out of file name
		my @getFrameNumArray = split('\\\\',$fn);
		@getFrameNumArray = reverse(@getFrameNumArray);
		$fn = $getFrameNumArray[0];
		
		$fn =~ s/[^0-9]//g;
		unless($found){
			$fn = $fn + 1;
		}
		#unless($found){
		# $fn = $fn + 1;
		#}
		
		print "fn = ".$fn."\n";
		
	display_output($docID,$c,$assocfilepath,$site,$fn,$found);
	}
	
}

&main();