#!/usr/bin/perl -w
use strict;
use lib './lib';

# swishedsearch : an example SWISH::API::Remote client
#

sub Usage {
	return "swishedsearch: -w searchword [-index=index]\n" . 
		"  [--swished=yourserverasdf.com/swished]\n" .
		"  [-b=beginat] [-p=prop1,prop] [--debug]\n" . 
		"  * Example to test a swished server at 'http://asdj24.com/swished'\n" .
		"    'swishedsearch -w search --swished=http://asdj24.com/swished'\n\n";
}

use Getopt::Long;

my $swished = 'http://nosuchserverkjf.com/swished';
my $index = "DEFAULT";
my $w = 'unix';	# what to search for
my ($p, $b) = ("swishrank,swishdocpath,swishtitle,swishdocsize", 0);
#my @props = qw( swishrank swishdocpath swishtitle swishdocsize );
my $max = 10;	# max to fetch
my $debug = 0;
my $help = 0;

GetOptions( "swished=s" => \$swished,	# base url to swished server
			"index=s" => \$index,		# name of index
			"w=s" => \$w,				# what to search for
			"b=i" => \$b,				# begin at
			"m=i" => \$max,				# max to fetch
			"debug" => \$debug,			# debug mode
			"help" => \$help,			# show Usage() help 
			"p=s" => \$p ) 				# list of props to fetch
		|| die "swishedsearch: Trouble parsing command-line options.\n" . Usage();

if ($help) {
	print Usage();
	exit(0);
}


use lib './lib';

use SWISH::API::Remote;
my $remote = SWISH::API::Remote->new( $swished, $index, { DEBUG=>$debug}); 
my $results = $remote->Execute( $w , {BEGIN=>$b, PROPERTIES=>$p, MAX=>$max} );

if ($results->Error()) {
	warn $results->ErrorString();
}
printf("Fetched %d of %d hits for search on '%s'\n",
	$results->Fetched(), $results->Hits(), $w);
while ( my $r = $results->NextResult() ) {
	print join("\t", map { sprintf("%-10s", $r->Property($_)) } ( split(/,/, $p) ) ) . "\n"; 
}

