Index: /main/trunk/greenstone2/bin/script/gsConvert.pl
===================================================================
--- /main/trunk/greenstone2/bin/script/gsConvert.pl	(revision 22428)
+++ /main/trunk/greenstone2/bin/script/gsConvert.pl	(revision 22429)
@@ -49,4 +49,6 @@
 }
 
+use strict;
+
 use parsargv;
 use util;
@@ -65,4 +67,5 @@
 my $pdf_allow_images_only;
 my $windows_scripting;
+my $openoffice_scripting;
 
 sub print_usage
@@ -72,10 +75,16 @@
     print STDERR "              or text using third-party programs.\n\n";
     print STDERR "  usage: $0 [options] filename\n";
-    print STDERR "  options:\n\t-type\tdoc|dot|pdf|ps|ppt|rtf|xls\t(input file type)\n";
+    if ($openoffice_scripting) {
+	print STDERR "  options:\n\t-type\tdoc|dot|docx|odf|pdf|ps|ppt|rtf|xls\t(input file type)\n";
+    }
+    else {
+	print STDERR "  options:\n\t-type\tdoc|dot|pdf|ps|ppt|rtf|xls\t(input file type)\n";
+    }
     print STDERR "\t-errlog\t<filename>\t(append err messages)\n";
     print STDERR "\t-output\tauto|html|text|pagedimage_jpg|pagedimage_gif|pagedimage_png\t(output file type)\n";
     print STDERR "\t-timeout\t<max cpu seconds>\t(ulimit on unix systems)\n";
     print STDERR "\t-use_strings\tuse strings to extract text if conversion fails\n";
-    print STDERR "\t-windows_scripting\tuse windows script when converting Microsoft Word and PPT via VB script\n";
+    print STDERR "\t-windows_scripting\tuse windows script (if available) when converting Microsoft Word and PPT via VB script\n";
+    print STDERR "\t-openoffice_scripting\tuse openoffice script (if available) when converting Microsoft Word and PPT via OpenOffice\n";
     print STDERR "\t-pdf_complex\tuse complex output when converting PDF to HTML\n";
     print STDERR "\t-pdf_nohidden\tDon't attempt to extract hidden text from PDF files\n";
@@ -96,13 +105,33 @@
     my ($input_type,$output_type,$verbose);
 
+    
+    # scan for -openoffice_scripting as it effects the permissible
+    # values for -type
+
+    foreach my $a (@ARGV) {
+	if ($a =~ m/^-openoffice_scripting$/) {
+	    $openoffice_scripting = 1;
+	    last;
+	}
+    }
+
+    my $parse_type;
+    if ($openoffice_scripting) {
+	$parse_type = 'type/(doc|dot|docx|odf|pdf|ps|ppt|rtf|xls)/';
+    }
+    else {
+	$parse_type = 'type/(doc|dot|pdf|ps|ppt|rtf|xls)/';
+    }
+
     # read command-line arguments
     if (!parsargv::parse(\@ARGV,
-			 'type/(doc|dot|pdf|ps|ppt|rtf|xls)/', \$input_type,
+			 $parse_type, \$input_type,
 			 '/errlog/.*/', \$faillogfile,
 			 'output/(auto|html|text|pagedimage).*/', \$output_type,
 			 'timeout/\d+/0',\$timeout,
 			 'verbose/\d+/0', \$verbose,
+			 'windows_scripting',\$windows_scripting,
+			 'openoffice_scripting',\$openoffice_scripting,
 			 'use_strings', \$use_strings,
-			 'windows_scripting',\$windows_scripting,
 			 'pdf_complex', \$pdf_complex,
 			 'pdf_ignore_images', \$pdf_ignore_images,
@@ -144,4 +173,8 @@
 	print STDERR "Error: No filename extension or input type defined\n";
 	exit(1);
+    } 
+    elsif ($openoffice_scripting && (($input_type eq "docx") || ($input_type eq "odf"))) {
+	print &convertDOC($input_filename, $output_filestem, $output_type);
+	print "\n";
     } 
     elsif ($input_type eq "doc" || $input_type eq "dot") {
@@ -158,5 +191,5 @@
     } 
     elsif ($input_type eq "ps") {
-	print &convertPS($input_filename, $output_filestem, $output_type);
+	print &convertPS($dirname, $input_filename, $output_filestem, $output_type);
 	print "\n";
     } 
@@ -195,5 +228,15 @@
 
 sub convertDOC {
-    ($input_filename, $output_filestem, $output_type) = @_;
+    my ($input_filename, $output_filestem, $output_type) = @_;
+
+    if (($openoffice_scripting) && ($input_filename =~ m/\.docx?$/i)) {
+	# Jump right in and process with Open Office
+        if (openoffice_doc_to_html($input_filename, $output_filestem)) {
+	    return "html";
+	}
+	else {
+	    return "fail";
+	}
+    }
 
     # Many .doc files are not in fact word documents!
@@ -212,5 +255,5 @@
 
 sub convertWord678 {
-    ($input_filename, $output_filestem, $output_type) = @_;
+    my ($input_filename, $output_filestem, $output_type) = @_;
 
     my $success = 0;
@@ -219,4 +262,7 @@
 	    $success = &native_doc_to_html($input_filename, $output_filestem);
 	}
+	elsif ($openoffice_scripting) {
+	    $success = &openoffice_doc_to_html($input_filename, $output_filestem);
+	}
 	else {
 	    $success = &doc_to_html($input_filename, $output_filestem);	   
@@ -233,5 +279,5 @@
 
 sub convertRTF {
-    ($input_filename, $output_filestem, $output_type) = @_;
+    my ($input_filename, $output_filestem, $output_type) = @_;
 
     my $success = 0;
@@ -242,4 +288,7 @@
 	if ($windows_scripting) {
 	    $success = &native_doc_to_html($input_filename, $output_filestem);
+	}
+	elsif ($openoffice_scripting) {
+	    $success = &openoffice_doc_to_html($input_filename, $output_filestem);
 	}
 	else {
@@ -261,5 +310,5 @@
 
 sub convertAnything {
-    ($input_filename, $output_filestem, $output_type) = @_;
+    my ($input_filename, $output_filestem, $output_type) = @_;
     
     my $success = 0;
@@ -324,5 +373,5 @@
 
 sub convertPS {
-    ($input_filename, $output_filestem, $output_type) = @_;
+    my ($dirname,$input_filename, $output_filestem, $output_type) = @_;
 
     my $success = 0;
@@ -365,9 +414,9 @@
 	$vbScript = "pptextract" if ($ENV{'GSDLOS'} =~ m/^windows$/i);
 			
-	$cmd = "";
+	my $cmd = "";
 	if ($timeout) {$cmd = "ulimit -t $timeout;";}
-	# if the converting directory has already existed
+	# if the converting directory already exists
 	if (-d $output_filestem) {
-	    print STDERR "**The conversion directory has existed\n";
+	    print STDERR "**The conversion directory already exists\n";
 	    return "item";
 	} else {
@@ -385,5 +434,5 @@
 	#if (!$output_type || ($output_type =~ m/html/i)) {
 	# formulate the command
-	$cmd = "";
+	my $cmd = "";
 	$cmd .= "perl -S ppttohtml.pl ";
 	$cmd .= " \"$input_filename\" \"$output_filestem.html\"";
@@ -418,5 +467,5 @@
     if (!$output_type || ($output_type =~ m/html/i)) {
 	# formulate the command
-	$cmd = "";
+	my $cmd = "";
 	$cmd .= "perl -S xlstohtml.pl ";
 	$cmd .= " \"$input_filename\" \"$output_filestem.html\"";
@@ -450,5 +499,5 @@
 # files or Word 5 files.  This function attempts to tell the difference.
 sub find_docfile_type {
-    ($input_filename) = @_;
+    my ($input_filename) = @_;
     
     open(CHK, "<$input_filename");
@@ -492,5 +541,5 @@
 # Attempt to convert a word document to html with the wv program
 sub doc_to_html {
-    ($input_filename, $output_filestem) = @_;
+    my ($input_filename, $output_filestem) = @_;
 
     my $wvWare = &util::filename_cat($ENV{'GSDLHOME'}, "bin", $ENV{'GSDLOS'}, "wvWare");
@@ -612,5 +661,5 @@
     if (-s "$output_filestem.html") { # if file has non-zero size (i.e. it has contents)
 	open(TMP, "$output_filestem.html");
-	$line = <TMP>;
+	my $line = <TMP>;
 	close(TMP);
 	if ($line && $line =~ m/DOCTYPE HTML/) {
@@ -750,5 +799,5 @@
 # Attempt to convert a word document to html with the word2html scripting program
 sub native_doc_to_html {
-    ($input_filename, $output_filestem) = @_;
+    my ($input_filename, $output_filestem) = @_;
 
     my $vbScript = &util::filename_cat($ENV{'GSDLHOME'}, "bin",
@@ -757,5 +806,7 @@
     $vbScript = "word2html" if ($ENV{'GSDLOS'} =~ m/^windows$/i);
     if (-e "$output_filestem.html") {
-	print STDERR "*** The conversion file has existed\n";
+	print STDERR "    The conversion file:\n";
+	print STDERR "      $output_filestem.html\n";
+	print STDERR "    ... already exists.  Skipping\n";
 	return 1;
     }
@@ -803,7 +854,7 @@
     if (-s "$output_filestem.html") {
 	open(TMP, "$output_filestem.html");
-	$line = <TMP>;
+	my $line = <TMP>;
 	close(TMP);
-	if ($line && $line =~ m/html/) {
+	if ($line && $line =~ m/html/i) {
 	    &util::rm("$output_filestem.err") if -e "$output_filestem.err";
 	    return 1;
@@ -825,11 +876,95 @@
 }
 
+# Attempt to convert a word document to html with JODConvert scripting program
+sub openoffice_doc_to_html {
+    my ($input_filename, $output_filestem) = @_;
+
+    if (-e "$output_filestem.html") {
+	print STDERR "    The conversion file:\n";
+	print STDERR "      $output_filestem.html\n";
+	print STDERR "    ... skipping\n";
+	return 1;
+    }
+
+    my $oo_script_dir = &util::filename_cat($ENV{'GEXT_OPENOFFICE'}, "bin", "script");
+    my $oo2html = &util::filename_cat($oo_script_dir,"oo2html");
+    if (!-e $oo2html) {
+	print STDERR "Error: Unable to find 'oo2html' in: \n";
+	print STDERR "       $oo_script_dir\n";
+	print STDERR "       Is the OpenOffice extension to Greenstone installed?\n";
+	return 0;
+    }
+
+    my $cmd = "";
+    if ($timeout) {$cmd = "ulimit -t $timeout;";}
+    $cmd .=  "$oo2html \"$input_filename\" \"$output_filestem.html\"";
+
+    # redirecting STDERR
+    $cmd .= " 2> \"$output_filestem.err\""
+	if ($ENV {'GSDLOS'} !~ m/^windows$/i || $is_winnt_2000);
+    
+    # execute the command
+    $!=0;
+    if (system($cmd)!=0)
+    {
+	print STDERR "Error executing oo2html converter: $!\n";
+	print STDERR "Command was: $cmd\n";
+
+	if (-s "$output_filestem.err") {
+	    open (ERRFILE, "<$output_filestem.err");
+	    
+	    my $write_to_fail_log=0;
+	    if ($faillogfile ne "" && defined(open(FAILLOG,">>$faillogfile")))
+	    {$write_to_fail_log=1;}
+
+	    my $line;
+	    while ($line=<ERRFILE>) {
+		if ($line =~ m/\w/) {
+		    print STDERR "$line";
+		    print FAILLOG "$line" if ($write_to_fail_log);
+		}
+		if ($line !~ m/startup error/) {next;}
+		print STDERR " (given an invalid .DOC file?)\n";
+		print FAILLOG " (given an invalid .DOC file?)\n"
+		if ($write_to_fail_log);
+		
+	    } # while ERRFILE
+	    close FAILLOG if ($write_to_fail_log);
+	}
+	return 0; # we can try any_to_text
+    }
+
+    # Was the conversion successful?
+    if (-s "$output_filestem.html") {
+	open(TMP, "$output_filestem.html");
+	my $line = <TMP>;
+	close(TMP);
+	if ($line && $line =~ m/html/i) {
+	    &util::rm("$output_filestem.err") if -e "$output_filestem.err";
+	    return 1;
+	}
+    }
+	
+    # If here, an error of some sort occurred
+    
+    &util::rm("$output_filestem.html") if -e "$output_filestem.html";
+    if (-e "$output_filestem.err") {
+	if ($faillogfile ne "" && defined(open(FAILLOG,">>$faillogfile"))) {
+	    open (ERRLOG,"$output_filestem.err");
+	    while (<ERRLOG>) {print FAILLOG $_;}
+	    close FAILLOG;
+	    close ERRLOG;
+	}
+	&util::rm("$output_filestem.err");
+    }
+    return 0;
+}
+
 # Attempt to convert an RTF document to html with rtftohtml
-
 sub rtf_to_html {
     my ($input_filename, $output_filestem) = @_;
 
     # formulate the command
-    $cmd = "";
+    my $cmd = "";
     if ($timeout) {$cmd = "ulimit -t $timeout;";}
     $cmd .= "rtftohtml";
@@ -903,7 +1038,7 @@
 	    my $line;
 	    while ($line=<TOC>) {
-		$line =~ s@</body></html>$@@ ; # only last line has this
+		$line =~ s@</body></html>$@@i ; # only last line has this
 		# make link relative
-		$line =~ s@href=\"[^\#]+@href=\"@;
+		$line =~ s@href=\"[^\#]+@href=\"@i;
 		print HTML $line;
 	    }
@@ -949,5 +1084,5 @@
     my ($dirname, $input_filename, $output_filestem) = @_;
 
-    $cmd = "";
+    my $cmd = "";
     if ($timeout) {$cmd = "ulimit -t $timeout;";}
     $cmd .= "perl -S pdftohtml.pl -zoom $pdf_zoom";
@@ -1021,5 +1156,5 @@
     }
 
-    $cmd = "";
+    my $cmd = "";
     if ($timeout) {$cmd = "ulimit -t $timeout;";}
     $output_type =~ s/.*\_(.*)/$1/i;
@@ -1316,5 +1451,5 @@
 
 sub any_to_html {
-    ($input_filename, $output_filestem) = @_;
+    my ($input_filename, $output_filestem) = @_;
 
     # First generate a text file
@@ -1354,5 +1489,5 @@
 
 sub any_to_text {
-    ($input_filename, $output_filestem) = @_;
+    my ($input_filename, $output_filestem) = @_;
 
     if (!$use_strings) {
