#!/usr/local/bin/perl -I../pm -w
 
#-----------------------------------------------------------------------

=head1 NAME

B<query> - query the Jumping Spider index

=head1 SYNOPSIS

  query
    [-help] |
    [-version] |
    [-verbose]
    [-databaseName databaseName]
    [-databaseMode GDBM | DBM | BSD]
    keyword1 ... keywordN

=head1 DESCRIPTION

B<query> is a simple script to query the L<JumpingSpider>.

=head1 EXAMPLE USAGE

Query the ACM index.

 perl -I../lib query 
    -verbose \
    -databaseName databaseName \
    -databaseMode DBM \
    database papers

See also L<JumpingSpider>.

=cut

#-----------------------------------------------------------------------

use strict;
require 5.002;

use Getopt::Long;
use IO::Pipe;
use English;
use JumpingSpider;

#-----------------------------------------------------------------------

=head1 OPTIONS

=over 4

=item -help

Display a short help message with a reminder of supported
command-line options.

=item -version

Display the version of Robot.

=item -verbose

Enable verbose reporting.

=item -databaseName databaseName

The name of the database, overides the default name
in L<JumpingSpider::Constants>.

=item -databaseMode databaseMode

The mode of the database, overides the default mode
in L<JumpingSpider::Constants>.

=back

=cut

#-----------------------------------------------------------------------

use vars qw($VERSION);

my $VERSION       = '1.00';
my $SHOW_VERSION  = 0;
my $VERBOSE       = 0;
my $HELP          = 0;
my $COMMAND_NAME  = 'query';

#-----------------------------------------------------------------------
# Parse the command line
#-----------------------------------------------------------------------
&ParseCommandLine();

# Open the necessary tables
print "$COMMAND_NAME: Opening database tables.\n" if $VERBOSE;
my $Globals = new JumpingSpider::Globals();

#-----------------------------------------------------------------------
# The query words come in on the command line 
#-----------------------------------------------------------------------
my @keywords = @ARGV;
print "$COMMAND_NAME: Querying " . join(' -> ', @keywords) . ".\n" if $VERBOSE;
my $urls = &JumpingSpider::Query::query(\@keywords, 
                        $Globals->{'indexTable'},
                        $Globals->{'WWWReachableTable'});

#-----------------------------------------------------------------------
# Print the results.
#-----------------------------------------------------------------------
print "$COMMAND_NAME: Printing results.\n" if $VERBOSE;
my $id;
my $titleTable = $Globals->{'titleTable'};
foreach $id (@$urls) {
  my $t = $titleTable->retrieveTuple($id);
  if ($t) {
    print $t->getValueAsString() . "\n";
    }
  else {
    print "Title unknown." . "\n";
    }
  }

#-----------------------------------------------------------------------
# clean up
#-----------------------------------------------------------------------
$Globals->close();
print "$COMMAND_NAME: Cleaned up and exiting.\n" if $VERBOSE;

#------------------------------------------------------------------------
# ParseCommandLine() - handle command line
#------------------------------------------------------------------------
sub ParseCommandLine {
  my @switches = (
    'databaseMode=s', \$JumpingSpider::Constants::databaseMode,
    'databaseName=s', \$JumpingSpider::Constants::databaseName,
    'help',           \$HELP,
    'verbose',        \$VERBOSE,
    'version',        \$SHOW_VERSION,
    );

  &GetOptions(@switches) || die "use -help switch to display brief help\n";

  if ($SHOW_VERSION) {
    print "This is $COMMAND_NAME, version $VERSION\n";
    exit 0;
    }

  if ($HELP) {
    print <<HelpEnd;
    $COMMAND_NAME, v$VERSION - create the Jumping Spider title table

    Usage: $COMMAND_NAME
                         [-help] |
                         [-version] |
                         [-verbose]
                         [-databaseName databaseName]
                         [-databaseMode GDBM | DBM | BSD]
                         keyword1 ... keywordN

        -help            : display this message
        -verbose         : display verbose information as running
        -databaseName name : name of the database
        -databaseMode mode : mode for the database

HelpEnd
    exit 0;
    }

}

