Index: /gsdl/trunk/bin/script/replace_srcdoc_with_html.pl
===================================================================
--- /gsdl/trunk/bin/script/replace_srcdoc_with_html.pl	(revision 15121)
+++ /gsdl/trunk/bin/script/replace_srcdoc_with_html.pl	(revision 15121)
@@ -0,0 +1,179 @@
+#!/usr/bin/perl -w
+
+
+BEGIN {
+    die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
+    unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
+    unshift (@INC, "$ENV{'GSDLHOME'}/perllib/plugins");
+}
+
+use strict;
+no strict 'subs'; # allow barewords (eg STDERR) as function arguments
+no strict 'refs'; # allow filehandles to be variables and vice versa
+
+use encodings;
+use printusage;
+use parse2;
+use FileHandle;
+
+my $arguments = 
+    [ 
+      { 'name' => "language",
+	'desc' => "{scripts.language}",
+	'type' => "string",
+	'reqd' => "no",
+        'hiddengli' => "yes" },
+      { 'name' => "plugin",
+	'desc' => "{srcreplace.plugin}",
+	'type' => "string",
+	'reqd' => "yes",
+	'hiddengli' => "yes"},
+      { 'name' => "verbosity",
+	'desc' => "{import.verbosity}",
+	'type' => "int",
+	'range' => "0,",
+	'deft' => "1",
+	'reqd' => "no",
+	'modegli' => "4" },
+      # Do not remove the following option, it's a flag for generating the xml of the options
+      # It WILL be used!
+      { 'name' => "xml", # run with -xml, the output generated should be valid XML.  Used from GLI
+	'desc' => "",
+	'type' => "flag",
+	'reqd' => "no",
+	'hiddengli' => "yes" }
+      ];
+	
+my $options = { 'name' => "replace_srcdoc_with_html.pl",
+		'desc' => "{srcreplace.desc}",
+		'args' => $arguments };
+
+	    
+sub main
+{
+    my ($language, $plugin, $verbosity);
+
+    my $xml = 0;
+
+    my $hashParsingResult = {};
+
+    # parse the options
+    my $intArgLeftinAfterParsing = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");
+
+    # If parse returns -1 then something has gone wrong
+    if ($intArgLeftinAfterParsing == -1)
+    {
+	&PrintUsage::print_txt_usage($options, "{srcreplace.params}");
+	die "\n";
+    }
+
+    foreach my $strVariable (keys %$hashParsingResult)
+    {
+	eval "\$$strVariable = \$hashParsingResult->{\"\$strVariable\"}";
+    }
+
+    # If $language has been specified, load the appropriate resource bundle
+    # (Otherwise, the default resource bundle will be loaded automatically)
+    if ($language && $language =~ /\S/) {
+	&gsprintf::load_language_specific_resource_bundle($language);
+    }
+
+    if ($xml) {
+        &PrintUsage::print_xml_usage($options);
+	print "\n";
+	return;
+    }
+
+    # There should be one arg left after parsing (the filename)
+    # Or the user may have specified -h, in which case we output the usage
+    if($intArgLeftinAfterParsing != 1 || (@ARGV && $ARGV[0] =~ /^\-+h/))
+    {
+	&PrintUsage::print_txt_usage($options, "{srcreplace.params}");
+	die "\n";
+    }
+
+    # The filename of the document to be replaced is the first value 
+    # that remains after the options have been parsed out
+    my $filename = $ARGV[0];
+    if (!defined $filename || $filename !~ /\w/) { 	
+	
+	&PrintUsage::print_txt_usage($options, "{srcreplace.params}");
+	print STDERR "You need to specify a filename";
+	die "\n";
+    }
+    # check that file exists
+    if (!-e $filename) {
+	print STDERR "File $filename doesn't exist...\n";
+	die "\n";
+    }
+    # check required options
+    if (!defined $plugin || $plugin !~ /\w/) {
+	&PrintUsage::print_txt_usage($options, "{srcreplace.params}");
+	print STDERR "You need to specify a plugin";
+	die "\n";
+    }
+
+    # ConvertToPlug.pm's subclasses should be available here through GLI,
+    # but in cmdline version, these should be supplied
+    my $plugobj;
+    require "$plugin.pm";
+    eval ("\$plugobj = new $plugin()");
+    die "$@" if $@;
+
+    # ...and initialize it
+    $plugobj->init(1, "STDERR", "STDERR");
+    
+    # find the import directory, where we want to create it in. This is where the file
+    # passed as parameter by GLI is located.
+   
+    # derive tmp filename from input filename
+    my ($tailname, $import_dir, $suffix)
+	= &File::Basename::fileparse($filename, "\\.[^\\.]+\$");
+
+    # Use the plugin's tmp_area_convert_file function to avoid duplicating code.
+    # This method returns the name of the output file. In the case of Word docs,
+    # if converted with windows_scripting a "filename_files" folder might have been
+    # created for associated files. Same situation when using wvware with gsConvert.pl.
+    # (When old gsConvert.pl was used, wvware created no separate directory, instead files
+    # associated with the html generated would be at the same level in the tmp folder
+    # where the output file was created.) Now it's the same no matter whether wvware
+    # or windows_scripting did the conversion of the Word doc to html.
+    my $output_filename = $plugobj->tmp_area_convert_file("html", $filename);    
+    # my $output_filename = $plugobj->tmp_area_convert_file($output_ext, $input_filename, $textref);
+    
+    if(!defined $output_filename || (!-e $output_filename)) { # if no output html file was created, then error message
+	print STDERR "***replace_srcdoc_with_html.pl: no output file created for $filename ***\n";
+	die "No html file created for $filename. Replacement did not take place\n"; # Program NEEDS to die here, 
+	# else the error that occurred is not transmitted to GLI and it thinks instead that execution was fine
+	#return 0; # error code 0 for false <- NO, needs to die, not return!
+    }
+    #else
+
+    # now, find out what to move:
+    # it may be a single file, or, if it is a word doc, it may also have an image folder
+    # which has the name "filename-without-extension_files"
+    my ($tmp_name, $tmp_dir, $ext) = &File::Basename::fileparse($output_filename, "\\.[^\\.]+\$");
+
+    if($import_dir ne $tmp_dir) { # output file is not in the same folder
+	my $assoc_folder = &util::filename_cat($tmp_dir, $tmp_name."_files");
+	
+	# use util to move it all to the import directory where the original file is located
+	#&util::mv($output_filename, &util::filename_cat($import_dir, $tmp_name.$ext)); # doesn't work
+	#&util::mv($output_filename, $import_dir); # doesn't work
+	&util::cp($output_filename, $import_dir);
+	&util::rm($output_filename);
+	if(defined $assoc_folder) { # move any associated folders too
+	    print STDERR "****Folder for associated files is $assoc_folder\n";
+	    #&util::mv($assoc_folder, $import_dir); # doesn't work for me
+	    &util::cp_r($assoc_folder, $import_dir);
+	    &util::rm_r($assoc_folder);
+	}
+    } # else output file was created in the same folder as the input file 
+    # this happens for txt files processed by TEXTPlug
+    # in this case, there will be no image folder AND no moving of output html file is necessary
+
+    # Now we can remove the source doc permanently (there are no assocdirs for source doc)
+    &util::rm($filename);
+    
+}
+&main(@ARGV);
