#!/usr/bin/perl -w

###########################################################################
#
# pharos-imageis-query.pl --
# 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 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.
#
###########################################################################

BEGIN {
    die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
    die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
    die "PHAROSIS_SRC_HOME not set\n" unless defined $ENV{'PHAROSIS_SRC_HOME'};
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
}


use strict;
no strict 'refs'; # allow filehandles to be variables and vice versa
no strict 'subs'; # allow barewords (eg STDERR) as function arguments

use util;

sub main
{
    
    my $ARGC = scalar(@ARGV);
    
    
    if ($ARGC != 2) {
	my ($progname) = ($0 =~ m/^.*[\/|\\](.*?)$/);
	print STDERR "Usage: $progname collection doc-id\n";
	exit 1;
    }

    my ($col,$id) = @ARGV;
            
    my $docid = $id;
    if ( $col ne "" ) {
	$docid="$col:$docid";
    }
    
    my $tmp_dir = &util::get_toplevel_tmp_dir();
    $tmp_dir = &util::filename_cat($tmp_dir,"pharos-imageis");

    print "Generating query_id.xml\n";
    
    my $query_template_filename
	= &util::filename_cat($ENV{'PHAROSIS_SRC_HOME'},"resources","templates","query_id.xml");
    my $query_this_filename = &util::filename_cat($tmp_dir,"query_id.xml");
    
    if (open(FIN,"<$query_template_filename")) {
	
	my $query_this_file_content = "";
	
	my $line;
	while (defined ($line=<FIN>)) {
	    $line =~ s@\*\*docid\*\*@$docid@g;
	    
	    $query_this_file_content .= $line;
	}
	
	close(FIN);
	
	if (open(FOUT,">$query_this_filename")) {
	    print FOUT $query_this_file_content;
	    close(FOUT);
	}
	else {
	    print STDERR "Error: failed to write out $query_this_filename\n";
	    print STDERR "$!\n";
	}
    }
    else {
	print STDERR "Error: failed to read $query_template_filename\n";
	print STDERR "$!\n";
    }
    
    
    my $jar_filename = &util::filename_cat($ENV{'PHAROSIS_SRC_HOME'},"pharos-imageis-lib.jar");
    my $classpath = &util::filename_cat($ENV{'PHAROSIS_SRC_HOME'},"classes");

    my $java_cmd = "java -classpath \"$classpath\":\"$jar_filename\" pharosimageis.PharosImageIS \"$query_this_filename\"";


    print "Querying $docid\n";
    
    chdir($tmp_dir);
    my $status = system($java_cmd);
    
    if ($status == 0 ) {
	&util::rm($query_this_filename);
    }
    else {
	print STDERR "Error: Failed to run:\n";
	print STDERR "$!\n";
	exit 1;
    }

    my $result_filename = &util::filename_cat($tmp_dir,"image_cbs_result.xml");
    
    if ( ! -f $result_filename ) {
	print STDERR "\n";
	print STDERR "Error: Failed to generate $result_filename.xml\n";
	print STDERR "       Check Tomcat's catalina.out for server-side error messages\n";
	print STDERR "\n";
	exit 1;
    }
    elsif ( -s $result_filename == 0) {
	print STDERR "\n";
	print STDERR "Warning: Server returned zero sized file image_cbs_result.xml\n";
	print STDERR "         Check that the doc-id used is valid\n";
	print STDERR "\n";
	&util::rm($result_filename);
    }
    else {
	
	if (open(FIN,"<$result_filename")) {
	
	    my $line;
	    while (defined ($line=<FIN>)) {
		print $line;
	    }
	    
	    close(FIN);
	}
	else {
	    print STDERR "Error: failed to read $result_filename\n";
	    print STDERR "$!\n";
	}

	print "\n";
	&util::rm($result_filename);
    }
}
 

main();




