#!/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

use strict;

# Set this to 1 to work around IIS 6 craziness
my $iis6_mode = 0;


# IIS 6: for some reason, IIS runs this script with the working directory set to the Greenstone
#   directory rather than the cgi-bin directory, causing lots of stuff to fail
if ($iis6_mode)
{
    # Change into cgi-bin directory
    chdir("cgi-bin");
}


# We use require and an eval here (instead of "use") to catch any errors loading the module (for IIS)
eval("require \"gsdlCGI.pm\"");
if ($@)
{
    print STDOUT "Content-type:text/plain\n\n";
    print STDOUT "ERROR: $@\n";
    exit 0;
}

sub generate_html_form
{
	# First create an overlay frame containing CL links.

	# Then create frames with document links and images.

	my ($isGSDL2,$site,$collect,$cl) = @_;

	my $html_form = <<EOT;
<html>
	<head>
		<title>Generate Collection Space for $collect</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/gsajax-min.js"></script>
		<script type="text/javascript" src="ext/html-to-expeditee/js/html-to-expeditee.js"></script>

	</head>
	<body>
	<form class="ui-widget">
	
		<p style="font-size: 16px; width: 550px; text-align: justify;">Generate a Collection Space to make it easier to view documents in GlamED/Expeditee. Before running this script you must have already converted the Greenstone collection to an Expeditee frameset. If you have <b>NOT</b> done this, click <a href="html-to-expeditee.pl?site=$site&collect=$collect&cl=CL1" target="_blank">here</a></p>
	
		Generate a Collection Space for <input type="text" class="ui-corner-all" style="padding: 4px;" name="collect" value="$collect" id="collect"/> using the classifier <input type="text" class="ui-corner-all" style="padding: 4px;" name="cl" value="$cl" id="cl"/>
		
		<input value="$site" name="site" id="site" type="hidden"> <input value="Go" id="go" class="ui-button ui-widget ui-state-default ui-corner-all" type="submit">
	</form>
	
	<script type="text/javascript">
		var clLinks = [];
		var numClLinks;
	
		\$(function(){
			\$('#progress').progressbar();
			
			\$('#go').button().click(function(){
				var collect = document.getElementById("collect").value;
				
				if(collect.match(/^\\s*\$/)){
					alert("No collection specified");
					return false;
				}
				
				var cl = document.getElementById("cl").value;
				if(cl.match(/^\\s*\$/)){
					alert("No classifier specified");
					return false;
				}
				
				if(cl.match(/^\\d+\$/)){
					cl = "CL" + cl;
				}
				
				
				
				
			return false;
		});
		
		function pageLoaded(){
		
			var iframe = document.getElementById("iframe");
			
			if(iframe.src){
				if(iframe.style.display != "block"){
					iframe.height = "90%";
					iframe.style.display = "block";
				}
				
				var iframeDoc;
				
				if(iframe.contentDocument){
					/* FF */
					iframeDoc = iframe.contentDocument;
				}else if(iframe.contentWindow){
					/* IE */
					iframeDoc = iframe.contentWindow.document;
				}
				
				var frameNum = (numClPages - clPages.length);
				var progressPercent = frameNum/numClPages * 100;
				
				\$(function(){
					\$('#progressbar').progressbar({ value: 0 });
				});
				
				var gsContent = iframeDoc.getElementById("gs_content");
				
				//Need to change htmlToExpeditee code to account for links
				var expFrameTre = htmlToExpeditee(gsContent);
				
				var collect = document.getElementById("collect").value;
				var site = document.getElementById("site").value;
				
				var url = "cgi-bin/generate-collection-space.pl";
				var params = "c=" + collect;
				
				if(site.match(/\\w/)){
					params += "&site=" + site;
				}
				
				params += "&a=generate-frame&fn=" + frameNum;
				params += "&json=" + escape(expFrame);
				
				var clHtml = urlPostSync(url,params);
				
				if(!clHtml.match(/html-to-expeditee saved frame/)){
					alert("Error processing url: " + url);s
				}
				
				if(clPages.length > 0){
					var clPage = clPages.shift();
					
					var cl = document.getElementById("cl").value;
					
					var gs2 = $isGSDL2;
					
					var url;
					
					var url;
					
					if(gs2){
						url = "library.cgi";
						url += "?c=" + collect + "&a=d&d=" + docOID;
					}else{
						url = "library";
						url += "?c=" + collect + "&a=d&d=" + docOID;
					}
					
					iframe.src = url;
				}else{
				
					var progressbar = document.getElementById("progressbar");
					progressbar.style.display = "none";
					
					iframe.style.display = "none";
					delete iframe.src;
				}
				
				
			}
			
			
		}
	</script>

	<div id="progressbar" width="100%" style="display: none; margin: 10px; height: 10px;"></div>
	
	<div id="workingTraverse" style="display: none"></div>
	
	<hr style="margin: 10px;">
	
	<iframe width="100%" id="iframe" style="display: none;" onload="pageLoaded()"></iframe>
</body>
</html>	
EOT
	
	print "Content-type:text/html\n\n";
	print $html_form;
}

sub main
{
	my $gsdl_cgi = new gsdlCGI();

    # Load the Greenstone modules that we need to use
    $gsdl_cgi->setup_gsdl();

    my $gsdlhome = $ENV{'GSDLHOME'};    
    $gsdl_cgi->checked_chdir($gsdlhome);

    require cgiactions::CollectionSpaceAction;
	
	$gsdl_cgi->parse_cgi_args();

	# We don't want the gsdlCGI module to return errors and warnings in XML
    $gsdl_cgi->{'xml'} = 0;
	
	my $fn = $gsdl_cgi->clean_param("fn");
	
	if(defined $fn){
		#Generate a frame.
		my $action = new CollectionSpaceAction($gsdl_cgi,$iis6_mode);
		$action->do_action();
	}else{
		my $collect = $gsdl_cgi->clean_param("collect");
		my $cl = $gsdl_cgi->clean_param("cl");
	
		#Establish collect_dir using defining 'site' along the way if GS3
		my $site = undef;
		my $isGSDL2 = undef;
	
		if($gsdl_cgi->greenstone_version() == 2){
			$isGSDL2 = 1;
		}else{
			$isGSDL2 = 0;
		
			#GS3 makes use of 'site'
			$site = $gsdl_cgi->clean_param("site");
		
			if(!defined $site){
				$gsdl_cgi->generate_error("No site specified.");
			}
		}
	
		generate_html_form($isGSDL2,$site,$collect,$cl);
	}
}

&main();