###########################################################################
#
# PharosImageIndexer - helper plugin that does image conversion using ImageMagick
#
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# Copyright (C) 2008 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 PharosImageIndexer;

use PrintInfo;


use strict;
no strict 'refs'; # allow filehandles to be variables and viceversa

use gsprintf 'gsprintf';

BEGIN {
    @PharosImageIndexer::ISA = ('PrintInfo');
}

my $arguments = [
		 ];

my $options = { 'name' => "PharosImageIndexer",
		'desc' => "{PharosImageIndexer.desc}",
		'abstract' => "yes",
		'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 $self = new PrintInfo($pluginlist, $inputargs, $hashArgOptLists, 1);


    return bless $self, $class;

}

# needs to be called after BasePlugin init, so that outhandle is set up.
sub init {
    my $self = shift(@_);
    
    # Check that Pharos is installed and available on the path 
    my $pharos_available = 1;
    my $result = `pharos-imageis-identify.pl 2>&1`;
    my $return_value = $?;
    if ( ($ENV{'GSDLOS'} eq "windows" && $return_value == 256) || $return_value == -1) {  # Linux and Windows return different values for "program not found"
        $pharos_available = 0;
    }
    
    $self->{'pharos_available'} = $pharos_available;
    
    if ($self->{'pharos_available'} == 0) {
	my $outhandle = $self->{'outhandle'};
	&gsprintf($outhandle, "PharosImageIndexer: Pharos not installed\n");
    }

}
	
sub begin {

}

sub remove_one {
    my $self = shift (@_);
    
    my ($file, $oids, $archivedir) = @_;
    print STDERR "in pharos remove_one, $file, $oids\n";

    if (!$self->{'pharos_available'}) {
	print STDERR "pharos not available, can't remove\n";
	return;
    }

    my $doc_id = @$oids[0]; # do we need to do all the ids??
    return unless defined $doc_id;
    my $collection = &util::get_current_collection_name();
    my $cmd = "perl -S pharos-imageis-delete.pl $collection $doc_id.jpg 2>&1";
    print STDERR "Running command $cmd\n";
    my $result = `$cmd`;
    my $return_value = $?;
    my $outhandle = $self->{'outhandle'};
    if ($return_value != 0) {
	print $outhandle "Pharos Indexing failed to delete $doc_id from database for collection $collection\n";
    }
    print $outhandle "$result\n";

 }
sub remove_all {
    my $self = shift (@_);
    my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
    
    if (!$self->{'pharos_available'}) {
	print STDERR "pharos not available, not doing a remove_all\n";
	return;
    }
    # need to delete everything from the derby database.
    my $collection = &util::get_current_collection_name();
    my $cmd = "perl -S pharos-imageis-delete.pl $collection '*.jpg' 2>&1";
    print STDERR "Running command $cmd\n";
    my $result = `$cmd`;
    my $return_value = $?;
    my $outhandle = $self->{'outhandle'};
    if ($return_value != 0) {
	print $outhandle "Pharos Indexing failed to clear database for collection $collection\n";
    }
    print $outhandle "$result\n";
    
}

sub pharos_index_image {
    my $self = shift(@_);

    if ( !$self->{'pharos_available'}) {
	print STDERR "pharos not available, can't index the image\n";
	return;
    }
    # pass in the jpg thumbnail, and the doc obj
    my ($filename_full_path, $doc_obj, $docid) = @_;
    print STDERR "pharos indexing $filename_full_path\n";

    my $collection = &util::get_current_collection_name();

    if (!defined $docid) {
	$docid = $doc_obj->get_OID();
    }

    my $cmd = "perl -S pharos-imageis-add.pl $collection $filename_full_path $docid 2>&1";
    print STDERR "Running command $cmd\n";
    my $result = `$cmd`;

    my $return_value = $?;
    my $outhandle = $self->{'outhandle'};
    if ($return_value != 0) {
	print $outhandle "Pharos Indexing failed for $filename_full_path, $docid\n";
    }
    print $outhandle "$result\n";
    
}


1;	
