###########################################################################
#
# UnknownConverterPlugin.pm -- plugin that runs the provided cmdline cmd 
# to launch an custom unknown external conversion application that will
# convert from some custom unknown format to one of txt, html or xml.
#
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# Copyright (C) 1999-2005 New Zealand Digital Library Project
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
###########################################################################

package UnknownConverterPlugin;

use strict; 
no strict 'subs';
no strict 'refs'; # allow filehandles to be variables and viceversa

use ConvertBinaryFile;
use UnknownPlugin;

# TO DO:
# - error messages and other display strings need to go into strings.properties
# - Have a TEMPDIR placeholder in the command, which, if present, gets replaced with the usual tempdir location
# of a collection, and in which case we have to clean up intermediate files generated in there at the end?
# Add a check that the generated file or files generated in the output dir match the convert_to option selected
# before trying to process them
# Add option that says where output comes from: stdout of the process, file that gets generated, folder.
# At present, a file or folder of files is assumed.
# Need to look in there for files with extension process_ext.
# Do we also need a html_multi option to convert_to? Support html_multi as output?
# Then a folder of html files is generated per document? 
# OR Flag that indicates whether an html file + associated folder (such as of images) gets generated. And name of assoc folder. Such output gets generated for instance when a doc file is replaced by its html version.

sub BEGIN {
    @UnknownConverterPlugin::ISA = ('UnknownPlugin', 'ConvertBinaryFile');
}

my $convert_to_list =
    [ {	'name' => "text",
	'desc' => "{ConvertBinaryFile.convert_to.text}" },
      {	'name' => "html",
	'desc' => "{ConvertBinaryFile.convert_to.html}" },
      { 'name' => "pagedimg_jpg",
	'desc' => "{ConvertBinaryFile.convert_to.pagedimg_jpg}" },
      { 'name' => "pagedimg_gif",
	'desc' => "{ConvertBinaryFile.convert_to.pagedimg_gif}" },
      { 'name' => "pagedimg_png",
	'desc' => "{ConvertBinaryFile.convert_to.pagedimg_png}" }
      ];

my $arguments =
    [ { 'name' => "exec_cmd",
	'desc' => "{UnknownConverterPlugin.exec_cmd}",
	'type' => "string",
	'deft' => "",
	'reqd' => "yes" },
      { 'name' => "convert_to",
	'desc' => "{ConvertBinaryFile.convert_to}",
	'type' => "enum",
	'reqd' => "yes",
	'list' => $convert_to_list, 
	'deft' => "text" } ];

my $options = { 'name'     => "UnknownConverterPlugin",
		'desc'     => "{UnknownConverterPlugin.desc}",
		'abstract' => "no",
		'inherits' => "yes",
		'args'     => $arguments };


sub new {
    my ($class) = shift (@_);
    my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
    push(@$pluginlist, $class);

    push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
    push(@{$hashArgOptLists->{"OptList"}},$options);

    my $unknown_converter_self = new UnknownPlugin($pluginlist, $inputargs, $hashArgOptLists);
    my $cbf_self = new ConvertBinaryFile($pluginlist, $inputargs, $hashArgOptLists);
    
    # Need to feed the superclass plugins to merge_inheritance() below in the order that the
    # superclass plugins were declared in the ISA listing earlier in this file:
    my $self = BaseImporter::merge_inheritance($unknown_converter_self, $cbf_self);

    $self = bless $self, $class;

my $outhandle = $self->{'outhandle'};
    print STDERR "\n\n**** convert_to is |" . $self->{'convert_to'} . "|\n\n";
    if(!defined $self->{'convert_to'}) {
	$self->{'convert_to'} = "text"; # why do I have to set a value for convert_to here, when a default's already set at the start of this file???????
    }

    # Convert_To set up, including secondary_plugins for processing the text or html generated
    # set convert_to_plugin and convert_to_ext
    $self->set_standard_convert_settings();

    my $secondary_plugin_name = $self->{'convert_to_plugin'};
    my $secondary_plugin_options = $self->{'secondary_plugin_options'};

    if (!defined $secondary_plugin_options->{$secondary_plugin_name}) {
	$secondary_plugin_options->{$secondary_plugin_name} = [];
    }
    my $specific_options = $secondary_plugin_options->{$secondary_plugin_name};

    # using defaults for secondary plugins, taken from RTFPlugin
    push(@$specific_options, "-file_rename_method", "none");
    push(@$specific_options, "-extract_language") if $self->{'extract_language'};
    if ($secondary_plugin_name eq "TextPlugin") {
	push(@$specific_options, "-input_encoding", "utf8");
    }
    elsif ($secondary_plugin_name eq "HTMLPlugin") {
	push(@$specific_options, "-description_tags") if $self->{'description_tags'};
	push(@$specific_options, "-processing_tmp_files");
    }
    elsif ($secondary_plugin_name eq "PagedImagePlugin") {
	push(@$specific_options, "-screenviewsize", "1000");
	push(@$specific_options, "-enable_cache");
	push(@$specific_options, "-processing_tmp_files");
    }

    # bless again, copied from PDFPlugin, PowerPointPlugin
    $self = bless $self, $class;
    $self->load_secondary_plugins($class,$secondary_plugin_options,$hashArgOptLists);
    return $self;
}

# Called by UnknownPlugin::process()
# Overriding here to ensure that the NoText flag (metadata) and dummy text are not set,
# since, unlike UnknownPlugin, this plugin has a chance of extracting text from the unknown file format
sub add_dummy_text {
    my $self = shift(@_);
}

# Are init, begin and deinit necessary (will they not get called automatically)?
# Copied here from PDFPlugin, PowerPointPlugin
# https://stackoverflow.com/questions/42885207/why-doesnt-class-supernew-call-the-constructors-of-all-parent-classes-when
# "$class->SUPER::new always calls A::new because A comes before B in @ISA. See method resolution order in perlobj: ..."
# https://stackoverflow.com/questions/15414696/when-using-multiple-inheritance-in-perl-is-there-a-way-to-indicate-which-super-f
sub init {
    my $self = shift (@_);

    # ConvertBinaryFile init
    $self->ConvertBinaryFile::init(@_);
}

sub begin {
    my $self = shift (@_);

    $self->ConvertBinaryFile::begin(@_);

}

sub deinit {
    my $self = shift (@_);
    
    $self->ConvertBinaryFile::deinit(@_);

}

# overridden to run the custom conversion command here in place of gsConvert.pl called by ConvertBinaryFile.pm
sub tmp_area_convert_file {
    # should we first hardlink the output files/folder to tmp area, so we won't be working across drives?

    my $self = shift (@_);
    my ($output_ext, $input_filename, $textref) = @_;

    my $plugin_name = $self->{'plugin_type'}; # inherited from BaseImporter

    #### COPIED FROM ConvertBinaryFile::tmp_area_convert_file()
    my $outhandle = $self->{'outhandle'};
    my $convert_to = $self->{'convert_to'};
    my $failhandle = $self->{'failhandle'};
    my $convert_to_ext = $self->{'convert_to_ext'}; #set by ConvertBinaryFile::set_standard_convert_settings()
    

    my $upgraded_input_filename = &util::upgrade_if_dos_filename($input_filename);

    # derive tmp filename from input filename
    my ($tailname, $dirname, $suffix)
	= &File::Basename::fileparse($upgraded_input_filename, "\\.[^\\.]+\$");

    # softlink to collection tmp dir
    my $tmp_dirname = &util::get_timestamped_tmp_folder();
    if (defined $tmp_dirname) {
	$self->{'tmp_dir'} = $tmp_dirname;
    } else {
	$tmp_dirname = $dirname;
    }
    
#    # convert to utf-8 otherwise we have problems with the doc.xml file later on
#    my $utf8_tailname = (&unicode::check_is_utf8($tailname)) ? $tailname : $self->filepath_to_utf8($tailname);

    # make sure filename to be used can be stored OK in a UTF-8 compliant doc.xml file
     my $utf8_tailname = &unicode::raw_filename_to_utf8_url_encoded($tailname);


    # URLEncode this since htmls with images where the html filename is utf8 don't seem
    # to work on Windows (IE or Firefox), as browsers are looking for filesystem-encoded
    # files on the filesystem.
    $utf8_tailname = &util::rename_file($utf8_tailname, $self->{'file_rename_method'}, "without_suffix");

    my $lc_suffix = lc($suffix);
    my $tmp_filename = &FileUtils::filenameConcatenate($tmp_dirname, "$utf8_tailname$lc_suffix");
    
    # If gsdl is remote, we're given relative path to input file, of the form import/utf8_tailname.suffix
    # But we can't softlink to relative paths. Therefore, we need to ensure that
    # the input_filename is the absolute path, see http://perldoc.perl.org/File/Spec.html
    my $ensure_path_absolute = 1; # true
    &FileUtils::softLink($input_filename, $tmp_filename, $ensure_path_absolute);
    my $verbosity = $self->{'verbosity'};
    if ($verbosity > 0) {
	print $outhandle "Converting $tailname$suffix to $convert_to format with extension $convert_to_ext\n";
    }

    my $errlog = &FileUtils::filenameConcatenate($tmp_dirname, "err.log");
    
 
    my $output_type=$self->{'convert_to'};

    # store the *actual* output type and return the output filename
    # it's possible we requested conversion to html, but only to text succeeded
    #$self->{'convert_to_ext'} = $output_type;
    if ($output_type =~ /html/i) {
	$self->{'converted_to'} = "HTML";
    } elsif ($output_type =~ /te?xt/i) {
	$self->{'converted_to'} = "Text";
    } elsif ($output_type =~ /item/i || $output_type =~ /^pagedimg/){
	$self->{'converted_to'} = "PagedImage";
    }
    
    my $output_filename = $tmp_filename;
    my $output_dirname;
    if ($output_type =~ /item/i || $output_type =~ /^pagedimg/) {
	# running under windows
	if ($ENV{'GSDLOS'} =~ /^windows$/i) {
	    $output_dirname = $tmp_dirname . "\\$utf8_tailname\\" . $utf8_tailname;
	} else {
	    $output_dirname = $tmp_dirname . "\/$utf8_tailname\/" . $utf8_tailname;
	}
	$output_filename .= ".item";
    } else {
	$output_filename =~ s/$lc_suffix$/.$output_type/;
    }

    #### END COPIED FROM ConvertBinaryFile::tmp_area_convert_file()

    # Execute the conversion command and get the type of the result,
    # making sure the converter gives us the appropriate output type

    # On Linux: if the program isn't installed, $? tends to come back with 127, in any case neither 0 nor 1.
    # On Windows: echo %ERRORLEVEL% ends up as 9009 if the program is not installed.
    # If running the command returns 0, let's assume success and so the act of running the command
    # should produce either a text file or output to stdout.

    my $cmd = $self->{'exec_cmd'};
    if(!$cmd) { # empty string for instance
	print $outhandle "$plugin_name Conversion error: a command to execute is required, cmd provided is |$cmd|\n";
	return "";
    }

    # HARDCODING CMD FOR NOW
    #$cmd ="/Scratch/ak19/gs3-svn-15Nov2016/packages/jre/bin/java -cp \"/Scratch/ak19/gs3-svn-15Nov2016/gs2build/ext/pdf-box/lib/java/pdfbox-app.jar\" -Dline.separator=\"<br />\" org.apache.pdfbox.ExtractText -html \"/Scratch/ak19/tutorial_sample_files/pdfbox/A9-access-best-practices.pdf\" \"/Scratch/ak19/gs3-svn-15Nov2016/pdf-tmp/1.html\"";

    #$cmd ="/Scratch/ak19/gs3-svn-15Nov2016/packages/jre/bin/java -cp \"/Scratch/ak19/gs3-svn-15Nov2016/gs2build/ext/pdf-box/lib/java/pdfbox-app.jar\" -Dline.separator=\"<br />\" org.apache.pdfbox.ExtractText -html %INPUT_FILE %OUTPUT";

    # replace occurrences of placeholders in cmd string
    #$cmd =~ s@\"@\\"@g;
    $cmd =~ s@%INPUT_FILE@\"$input_filename\"@g;
    if(defined $output_dirname) {
	$cmd =~ s@%OUTPUT@\"$output_dirname\"@g;
    } else {
	$cmd =~ s@%OUTPUT@\"$output_filename\"@g;
    }

    # Some debugging
    if ($self->{'verbosity'} > 2) {
	print STDERR "$plugin_name: executing conversion cmd \n|$cmd|\n";
	print STDERR "   on infile |$input_filename|\n";
	print STDERR "   to produce expected $output_filename\n";
    }

    # Run the command at last
    my $status = system($cmd);

    if($status == 127 || $status == 9009) { # means the cmd isn't recognised on Unix and Windows, respectively
	print $outhandle "$plugin_name Conversion error: cmd unrecognised, may not be installed (got $status when running $cmd)\n";
	return "";
    }

    if($status != 0) {
	print $outhandle "$plugin_name Conversion error: conversion failed with exit value $status\n";
	return "";
    }

    # remove symbolic link to original file
    &FileUtils::removeFiles($tmp_filename);


    if(defined $output_dirname && -d $output_dirname) {
	print $outhandle "$plugin_name Conversion error: Output directory $output_dirname doesn't exist\n";
	return "";
    }
    elsif (!-e $output_filename) {
	print $outhandle "$plugin_name Conversion error: Output file $output_filename doesn't exist\n";
	return "";
    }

    # else, conversion success
    
    # if multiple images were generated by running the conversion
    if ($self->{'convert_to'} =~ /^pagedimg/) {
	my $item_filename = $self->generate_item_file($output_filename);

	if (!-e $item_filename) {
	    print $outhandle "$plugin_name Conversion error: Item file $item_filename was not generated\n";
	    return "";
	}	
	$output_filename = $item_filename;
    }

    $self->{'output_dirname'} = $output_dirname;
    $self->{'output_filename'} = $output_filename;
    
    return $output_filename;
}

# Copied from PowerPointPlugin, with some modifications
# override default read in some situations, as the conversion of ppt to html results in many files, and we want them all to be processed.
sub read {
    my $self = shift (@_);  
    my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
    
    # can we process this file??
    my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
    
    return undef unless $self->can_process_this_file($filename_full_path);
    
    my $is_output_dir = (defined $self->{'output_dirname'}) ? 1 : 0;

    # we are only doing something special if we have a directory of html files
    #if ($is_output_dir || $self->{'convert_to'} ne "html") {
    if ($self->{'convert_to'} ne "html_multi") {
	return $self->BaseImporter::read(@_); # no read in ConvertBinaryFile.pm
    }
    my $outhandle = $self->{'outhandle'};
    print STDERR "<Processing n='$file' p='$self->{'plugin_type'}'>\n" if ($gli);
    print $outhandle "$self->{'plugin_type'} processing $file\n"
	    if $self->{'verbosity'} > 1;

    my $conv_filename = $self->tmp_area_convert_file("html", $filename_full_path); # uses our overridden version
    if ("$conv_filename" eq "") {return -1;} # had an error, will be passed down pipeline 
    if (! -e "$conv_filename") {return -1;} 

    my ($tailname, $html_dirname, $suffix)
	= &File::Basename::fileparse($conv_filename, "\\.[^\\.]+\$");

    my $collect_file = &util::filename_within_collection($filename_full_path);
    my $dirname_within_collection = &util::filename_within_collection($html_dirname);
    my $secondary_plugin = $self->{'secondary_plugins'}->{"HTMLPlugin"};

    my @dir;
    if (!opendir (DIR, $html_dirname)) {
	print $outhandle "PowerPointPlugin: Couldn't read directory $html_dirname\n";
	# just process the original file
	@dir = ("$tailname.$suffix");
	
    } else {
	@dir = readdir (DIR);
	closedir (DIR);
    }

    foreach my $file (@dir) {
	next unless $file =~ /\.html$/;
	
	my ($rv, $doc_obj) = 
	    $secondary_plugin->read_into_doc_obj ($pluginfo,"", &util::filename_cat($html_dirname,$file), $block_hash, {}, $processor, $maxdocs, $total_count, $gli);
	if ((!defined $rv) || ($rv<1)) {
	    # wasn't processed
	    return $rv;
	}

	# next block copied from ConvertBinaryFile
	# from here ...
	# Override previous gsdlsourcefilename set by secondary plugin
	
	$doc_obj->set_source_filename ($collect_file, $self->{'file_rename_method'}); 
	## set_source_filename does not set the doc_obj source_path which is used in archives dbs for incremental
	# build. so set it manually.
	$doc_obj->set_source_path($filename_full_path);
	$doc_obj->set_converted_filename(&util::filename_cat($dirname_within_collection, $file));
	
	my $plugin_filename_encoding = $self->{'filename_encoding'};
    my $filename_encoding = $self->deduce_filename_encoding($file,$metadata,$plugin_filename_encoding);
	$self->set_Source_metadata($doc_obj, $filename_full_path,$filename_encoding);
        
	$doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
	$doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "FileSize", (-s $filename_full_path));

	
	my ($tailname, $dirname, $suffix)
	    = &File::Basename::fileparse($filename_full_path, "\\.[^\\.]+\$");
	$doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "FilenameRoot", $tailname);
	

	my $topsection = $doc_obj->get_top_section();
	$self->add_associated_files($doc_obj, $filename_full_path);
	
	# extra_metadata is already called by sec plugin in process??
	$self->extra_metadata($doc_obj, $topsection, $metadata); # do we need this here??
	# do any automatic metadata extraction
	$self->auto_extract_metadata ($doc_obj);
	
	# have we found a Title??
	$self->title_fallback($doc_obj,$topsection,$filename_no_path);
	
	# use the one generated by HTMLPlugin, otherwise they all end up with same id.
	#$self->add_OID($doc_obj);
	# to here...

	# process it
	$processor->process($doc_obj);
	undef $doc_obj;
    }
    $self->{'num_processed'} ++;

    # deleted some commented out code here that exists in PowerPointPlugin

    # for UnknownConverterPlugin, don't delete any temp files that the conversion may have created?
    # as we don't know where it was created. No. Now creating in tmp.
    $self->clean_up_after_doc_obj_processing();


    # if process_status == 1, then the file has been processed.
    return 1;

}

# use the read_into_doc_obj inherited from ConvertBinaryFile "to call secondary plugin stuff"
sub read_into_doc_obj {
    my $self = shift (@_);
    $self->ConvertBinaryFile::read_into_doc_obj(@_);
}

sub process {
    my $self = shift (@_);
    $self->UnknownPlugin::process(@_);
}


1;
