######################################################################
#
#  EPrints OpenURL Internal Resolver
#
######################################################################
#
#  This file is part of GNU EPrints 2.
#  
#  Copyright (c) 2000-2004 University of Southampton, UK. SO17 1BJ.
#  
#  EPrints 2 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.
#  
#  EPrints 2 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 EPrints 2; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
######################################################################

use EPrints;
use URI::OpenURL;

use strict;
my $session = new EPrints::Session;
exit( 0 ) unless( defined $session );
# $session->get_database->set_debug( 1 );

# Create an OpenURL object
my $uri = URI::OpenURL->new();

# Fill the OpenURL object with the current CGI parameters
my @query;
foreach my $name ($session->param)
{
	for ($session->param( $name ))
	{
		push @query, $name => $_;
	}
}
$uri->query_form( @query );

# warn $uri->dump;

# Get the referent (the referred to object)
my $rft = $uri->referent;

# Get the metadata given for the referent
my %md = $rft->metadata;

# Set up a new search expression
# NB I'm not worrying about filtering because this script will only
# redirect?
my $ds = $session->get_repository->get_dataset( "archive" );
my $searchexp = new EPrints::Search(
	keep_cache => 1,
	session => $session,
	dataset => $ds,
	satisfy_all => 1,
);

# Fill in the search expression, mapping OpenURL terms to eprint fields
if( $md{ "aulast" } )
{
	$searchexp->add_field(
		$ds->get_field( "creators_name" ),
		$md{ "aulast" }
	);
}
if( defined($rft->id) )
{
	my $oai_id = $session->get_repository->get_conf( "oai" )->{ "v2" }->{ "archive_id" };
	if( defined($rft->id) and $rft->id =~ /^oai:$oai_id:(\d+)$/ )
	{
		$searchexp->add_field(
			$ds->get_field( "eprintid" ),
			$1
		);
	}
	else
	{
		$searchexp->add_field(
			$ds->get_field( "official_url" ),
			scalar($rft->id)
		);
	}
}
my %FREE_TEXT = qw(
	title atitle
	publication title
);
while(my( $epf, $ouf ) = each %FREE_TEXT)
{
	next unless defined($md{ $ouf }) and length($md{ $ouf });
	$searchexp->add_field(
		$ds->get_field( $epf ),
		$md{ $ouf },
		"IN"
	);
}
my %EXACT_TERMS = qw(
	volume	volume
	number	number
	series	series
	date	date
	pagerange	pages
);
while(my( $epf, $ouf ) = each %EXACT_TERMS)
{
	next unless defined($md{ $ouf }) and length($md{ $ouf });
	$searchexp->add_field(
		$ds->get_field( $epf ),
		$md{ $ouf }
	);
}

# Urg, either the query was empty or we couldn't use the query given
if( $searchexp->is_blank )
{
	die "No query given";
}

# Perform the search, but we're only interested in the first match
# that comes back (perhaps we should error on more than one match?)
my $list = $searchexp->perform_search();
my( $eprint ) = $list->get_records( 0, 1 );

# No match found for the given query
unless( $eprint ) {
	die "No match found";
}

my $svc = $uri->serviceType;

my %svc_md = $svc->metadata;

if( $svc_md{ "fulltext" } and
	$svc_md{ "fulltext" } eq 'yes' )
{
	# Redirect to the main document (if possible)
	my @docs = $eprint->get_all_documents();
	if( @docs )
	{
		my $url = $docs[0]->get_url();
		$session->redirect( $url );
	}
}
else
{
	# Redirect to the abstract page
	my $url = $eprint->get_url;
	$session->redirect( $url );
}

$session->terminate;
