package OOConvertBinaryFile;

use ConvertBinaryFile;


use gsprintf 'gsprintf';
# @ISA dynamically configured to be either OpenOfficeConverter or ConvertBinaryFile

# do not initialise these variables
my $openoffice_ext_installed;
my $openoffice_ext_working;
sub BEGIN {
    eval("require OpenOfficeConverter");
    if ($@) {
	# Useful debugging statement if there is a syntax error in OpenOfficeConverter:
	# print STDERR "$@\n";
	$openoffice_ext_installed = 0;
	$openoffice_ext_working = 0;
    }
    else {
	# Successfully found
	$openoffice_ext_installed = 1;
	# now check whether it can run soffice
	if ($OpenOfficeConverter::openoffice_conversion_available) {
	    $openoffice_ext_working = 1;
	    
	} else {
	    $openoffice_ext_working = 0;
	}
    }

    if ($openoffice_ext_working) {
	@OOConvertBinaryFile::ISA = ('ConvertBinaryFile', 'OpenOfficeConverter');
    } else {
	@OOConvertBinaryFile::ISA = ('ConvertBinaryFile');
    }
}

my $opt_openoffice_args = 
    [ { 'name' => "openoffice_scripting",
	'desc' => "{OpenOfficeConverter.openoffice_scripting}",
	'type' => "flag",
	'reqd' => "no" } ];

my $arguments = [];

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

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

    if ($openoffice_ext_installed) {
	print STDERR "$class: OpenOffice Extension to Greenstone detected\n";
	if ($openoffice_ext_working) {
	    print STDERR "... and it appears to be working\n";
	} else {
	    print STDERR "... but it appears to be broken\n";
	    &gsprintf(STDERR, "OpenOfficeConverter: {OpenOfficeConverter.noconversionavailable} ({OpenOfficeConverter.$OpenOfficeConverter::no_openoffice_conversion_reason})\n");
	}
    }

    if ($openoffice_ext_working) {
	push(@$arguments,@$opt_openoffice_args);
    }

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

    if ($openoffice_ext_working) {
	
	#$self = new OpenOfficeConverter($pluginlist, $inputargs, $hashArgOptLists);
	my $ooc_self = new OpenOfficeConverter($pluginlist, $inputargs, $hashArgOptLists);
	my $cbf_self = new ConvertBinaryFile($pluginlist, $inputargs, $hashArgOptLists);
	$self = BasePlugin::merge_inheritance($ooc_self, $cbf_self);
	$self->{'openoffice_available'} = 1;
    }
    else {
	$self = new ConvertBinaryFile($pluginlist, $inputargs, $hashArgOptLists);
	$self->{'openoffice_available'} = 0;
   }

#    if ($self->{'info_only'}) {
	# don't worry about any options etc
	return bless $self, $class;
#    }
}

sub init {
    my $self = shift (@_);
    my ($verbosity, $outhandle, $failhandle) = @_;
    
    $self->SUPER::init($verbosity,$outhandle,$failhandle);
    if ($openoffice_ext_working) {
	$self->OpenOfficeConverter::init();
    }
}

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

    if ($openoffice_ext_working) {
	$self->OpenOfficeConverter::begin(@_);
    }
    $self->SUPER::begin(@_);
}

sub deinit {
    # called only once, after all plugin passes have been done
    my ($self) = @_;
    
    if ($openoffice_ext_working) {
	$self->OpenOfficeConverter::deinit();
    }
    $self->SUPER::deinit();
}

# if we are using open office, we use OpenOfficeConverter's convert method, otherwise fall back to ConvertBinaryFile method.
sub tmp_area_convert_file {
    my $self = shift (@_);
    my ($output_ext, $input_filename, $textref) = @_;

    if ($openoffice_ext_working && $self->{'openoffice_scripting'}) {
	my ($result, $result_str, $new_filename) = $self->OpenOfficeConverter::convert($input_filename, $output_ext);
	if ($result != 0) {
	    return $new_filename;
	}
	my $outhandle=$self->{'outhandle'};
	print $outhandle "Open Office Conversion error\n";
	print $outhandle $result_str;
	return "";
    }
    else {
	return $self->ConvertBinaryFile::tmp_area_convert_file(@_);
    }
}
