Index: gsdl/branches/gsdl-2.74/cgi-bin/gliserver.pl
===================================================================
--- gsdl/branches/gsdl-2.74/cgi-bin/gliserver.pl	(revision 14217)
+++ gsdl/branches/gsdl-2.74/cgi-bin/gliserver.pl	(revision 14270)
@@ -1,9 +1,30 @@
 #!perl -w
-
 # Need to specify the full path of Perl above
 
 
-use gsdlCGI;
 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;
+}
 
 
@@ -109,5 +130,4 @@
 sub authenticate_user
 {
-    
     my $gsdl_cgi = shift(@_);
     my $username = shift(@_);
@@ -244,7 +264,17 @@
     my $installation_status = "";
 
+    print STDOUT "Content-type:text/plain\n\n";
+
     # Check that Java is installed and accessible
     my $java = $gsdl_cgi->get_java_path();
     my $java_command = "$java -version 2>&1";
+
+    # IIS 6: redirecting output from STDERR to STDOUT just doesn't work, so we have to let it go
+    #   directly out to the page
+    if ($iis6_mode)
+    {
+	$java_command = "java -version";
+    }
+
     my $java_output = `$java_command`;
     my $java_status = $?;
@@ -265,8 +295,8 @@
 
     if ($installation_ok) {
-	$gsdl_cgi->generate_ok_message($installation_status . "\nInstallation OK!");
+	print STDOUT $installation_status . "\nInstallation OK!";
     }
     else {
-	$gsdl_cgi->generate_error($installation_status);
+	print STDOUT $installation_status;
     }
 }
@@ -563,5 +593,4 @@
     }
 
-    print STDOUT "Content-type:text/plain\n\n";
     foreach my $cgi_arg_name ($gsdl_cgi->param) {
 	my $cgi_arg_value = $gsdl_cgi->clean_param($cgi_arg_name) || "";
@@ -575,5 +604,15 @@
     }
 
+    print STDOUT "Content-type:text/plain\n\n";
+
     my $perl_command = "perl -S $script $perl_args 2>&1";
+
+    # IIS 6: redirecting output from STDERR to STDOUT just doesn't work, so we have to let it go
+    #   directly out to the page
+    if ($iis6_mode)
+    {
+	$perl_command = "perl -S $script $perl_args";
+    }
+
     my $perl_output = `$perl_command`;
     my $perl_status = $?;
@@ -582,7 +621,8 @@
     }
 
-    print STDOUT "Content-type:text/plain\n\n";
-    print STDOUT $perl_output;
-
+    if (defined($perl_output))
+    {
+	print STDOUT $perl_output;
+    }
 }
 
@@ -728,10 +768,19 @@
     }
 
+    print STDOUT "Content-type:text/plain\n\n";
+
     my $perl_command = "perl -S $script $perl_args 2>&1";
+
+    # IIS 6: redirecting output from STDERR to STDOUT just doesn't work, so we have to let it go
+    #   directly out to the page
+    if ($iis6_mode)
+    {
+	$perl_command = "perl -S $script $perl_args";
+    }
+
     if (!open(PIN, "$perl_command |")) {
 	$gsdl_cgi->generate_error("Unable to execute command: $perl_command");
     }
 
-    print STDOUT "Content-type:text/plain\n\n";
     while (defined (my $perl_output_line = <PIN>)) {
 	print STDOUT $perl_output_line;
@@ -799,10 +848,18 @@
 
     # Read the uploaded data and write it out to file
+    # We have to pass the size of the uploaded data in the "fs" argument because IIS 6 seems to be
+    #   completely incapable of working this out otherwise (causing the old code to crash)
     my $buf;
     my $num_bytes = 0;
+    my $num_bytes_remaining = $gsdl_cgi->clean_param("fs");
+    my $bytes_to_read = $num_bytes_remaining;
+    if ($bytes_to_read > 1024) { $bytes_to_read = 1024; }
     binmode(FOUT);
-    while (read(STDIN, $buf, 1024) > 0) {
+    while (read(STDIN, $buf, $bytes_to_read) > 0) {
 	print FOUT $buf;
 	$num_bytes += length($buf);
+	$num_bytes_remaining -= length($buf);
+	$bytes_to_read = $num_bytes_remaining;
+	if ($bytes_to_read > 1024) { $bytes_to_read = 1024; }
     }
     close(FOUT);
