#! /usr/bin/perl -w

############################################################
#
# This code is GPL|Artistic dual licensed.
#
# See either Artistic.html or COPYING for the exact conditions.
# Choose among Artistic.html and COPYING the license you prefer.
#
# THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
# WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# (C) 2001 Centre Universitaire d'Informatique, University of Geneva
#
# Author: Wolfgang M&uuml;ller  <Wolfgang.MUELLER@cui.unige.ch>

use XML::Writer;

require Exporter;

use MRML::Client::Base;
use Data::Dumper;
package main;



use vars qw($VERSION $g_query_string);

$g_query_string=shift || 'htdig';

$VERSION="0.01";


my $lWriter=XML::Writer->new();
my $g_client=new MRML::Client::Base(address=>"localhost:12789");
print "Client instantiiert\n";
my $l_session_1=$g_client->open_session('user-id'=>"tester",
					"session-name"=>"meine-session");
print "Session geöffnet\n";
my $l_session_1_id=$l_session_1->[0]->{attrib}->{"session-id"};
print "SessionID: $l_session_1_id\n";

print "Hole Liste von verfügbaren Algorithmen\n";
my $l_response=$g_client->send_message(tree=>$g_client->generate_mrml(tree=>[
									     {
									      type=>'e',
									      name=>'get-algorithms',
									      content=>[]
									     }
									    ]
								     ));


#print Dumper($l_response);

my($i,$j,$k);
for $i (@{$l_response}){
  if($i->{content}){
    for $j (@{$i->{content}}){
      if(($j->{content})
	 &&
	 ($j->{name} eq "algorithm-list")){
	for $k (@{$j->{content}}){
	  print "Algorithm gefunden, ID/Type: ",$k->{attrib}->{"algorithm-id"},"/",$k->{attrib}->{"algorithm-type"}," Name: ",$k->{attrib}->{"algorithm-name"},"\n";
	}
      }
    }
  }
}

print "Hole Liste von verfügbaren Collections\n";
 $l_response=$g_client->send_message(tree=>$g_client->generate_mrml(tree=>[
									     {
									      type=>'e',
									      name=>'get-collections',
									      content=>[]
									     }
									    ])
				      );


for $i (@{$l_response}){
  if($i->{content}){
    for $j (@{$i->{content}}){
      if(($j->{content})
	 &&
	 ($j->{name} eq "collection-list")){
	for $k (@{$j->{content}}){
	  print "Collection gefunden, ID: ",$k->{attrib}->{"collection-id"}," Name: ", $k->{attrib}->{"collection-name"},"\n";
	}
      }
    }
  }
}


print "Konfiguriere Session $l_session_1_id\n";
 $l_response=$g_client->configure_session( "collection-id"=>"c-htdig",
					     "algorithm-id"=> "a-htdig",
					     "algorithm-type"=> "a-htdig");


my $l_query_result=$g_client->send_message(tree=>$g_client->generate_query_step('algorithm-id'=> 'a-htdig',
										'result-size'=> 20,
										'result-cutoff'=> 0,
										'cui-comment'=>"Eine text query wuerde hingegen so aussehen:",
										tree=>[{
											type=>'e',
											name=>'cui-text-query',
											attrib=>{'query-string'=>$g_query_string},
											content=>[]
										       }]))
					  ;

print Dumper($l_query_result);
my @lURLList;
#
# Now, let's look for elements with he path mrml/query-result/query-result-element
#
foreach $i (@{$l_query_result}){
  if($i->{name} eq "mrml"){
    for $j (@{$i->{content}}){
      if($j->{name} eq "query-result"){
	for $k (@{$j->{content}}){
	  if($k->{name} eq "query-result-element"){
	    push @lURLList,$k->{attrib}->{'image-location'};
	  }
	}
      }
    }
  }
}
{
  #
  # Now, save the stuff as some html page
  #
  open OUTFILE,">outfile.html";
  my $i;

  print OUTFILE << "HEADER";
  <html><head><title>Query for $g_query_string, using the GIFT ht://dig plugin</title></head>
    <body>
      <h1>Query for "$g_query_string", using the GIFT ht://dig plugin</h1>
	The following results where obtained:
      <table>
HEADER

  foreach $i (@lURLList){
    print OUTFILE "<tr><td><a href=\"$i\">$i</a></td></tr>\n";
  }

  print OUTFILE << "FOOTER";
      </table>
	<hr>
	<p>Please note, that currently no relevance values are given by the 
	ht://dig plugin. This is left as an exercise ;-)</p>
  </body>
</html>
FOOTER
system("konqueror","outfile.html");
}

print "If you do not see anything, probably there is no konqueror in your path
If that is the case, please replace the string \"konqueror\" within this script
by the path to the browser of your choice.
";
