#!/usr/bin/perl -w

###########################################################################
#
# lucene_query.pl -- perl wrapper to initiate query using Lucene
# 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'};
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib/plugins");
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib/classify");
}

use util;

sub full_indexdir 
{
    my ($col_name,$indexdir) = @_;

    my $full_indexdir
	= &util::filename_cat($ENV{'GSDLHOME'},"collect",$col_name,"index",
			      $indexdir);

    return $full_indexdir;
}


sub open_java_lucene
{
    my ($full_indexdir, $out_file) = @_;
    my $bin_java = &util::filename_cat($ENV{'GSDLHOME'},"bin","java");
    my $classpath = &util::filename_cat($bin_java,"LuceneWrap.jar");

    my $java_lucene = "java -classpath \"$classpath\" GS2LuceneQuery";
    my $java_cmd = "$java_lucene";

    if (defined($out_file)) {
	$out_file = " > \"".$out_file. "\"";
    } else {
	$out_file = "";
    }
    if (!open (PIPEOUT, "| $java_cmd \"$full_indexdir\" $out_file")) {
	die "$PROGNAME - couldn't run $java_cmd\n";
    }

}

sub close_java_lucene
{
    close(PIPEOUT);
}

sub main
{
    my (@argv) = @_;
    my $argc = scalar(@argv);

    my $full_indexdir;
    my $query = undef;
    my $out_file = undef;
    if ($argc==1) {
	$full_indexdir = $argv[0];
    }
    elsif ($argc==2) {
	$full_indexdir = $argv[0];
	$query = $argv[1];
    }
    elsif  ($argc==3) {
	$full_indexdir = $argv[0];
	$query = $argv[1];
	$out_file = $argv[2];
    }
    else {
	print STDERR "Usage: $PROGNAME full-index-dir [query] [outfile]\n";
	exit 1;
    }
    open_java_lucene($full_indexdir, $out_file);

    if (defined $query) {
	print PIPEOUT "$query\n";
    }
    else {
	while (defined (my $line = <STDIN>)) {
	    print PIPEOUT $line;
	}
    }

    close_java_lucene();
}

$PROGNAME = $0;
$PROGNAME =~ s/^.*\/(.*)$/$1/;

&main(@ARGV);

