
use EPrints;

use strict;

my $session = EPrints::Session->new();

# security?

# How many chars needed before we try and suggest duplicates?
my $need = 5;

my $content = "text/xml";
$session->send_http_header( content_type=>$content );
my $q = $session->param( "q" );

exit unless length( $q ) >= $need; 

my $eprintid = $session->param( "eprintid" ) || '';
$eprintid =~ /^([0-9]+)$/;
$eprintid = $1;

my $sql = "SELECT eprintid, title FROM eprint WHERE eprintid!=$eprintid AND eprint_status='archive' AND title IS NOT NULL AND title LIKE '".EPrints::Database::prep_like_value($q)."\%' LIMIT 10";

print <<END;
<?xml version="1.0" encoding="UTF-8" ?>
<ul>
END

my $sth = $session->get_database->prepare( $sql );
$session->get_database->execute( $sth , $sql );
my $first = 1;

my $count = $sth->rows;
my $base_url = $session->get_repository->get_conf( "base_url" );

my $warning =<<END;
The following records matching this title already exist in the archive.
Please check that you are not entering a duplicate record.
END

if( $count )
{
	print $session->render_message( "warning", $session->make_text( $warning) )->toString;
}

while( my( $eprintid, $title ) = $sth->fetchrow_array )
{
	if( $first )
	{
		print "<li class='ep_first'>";
		$first = 0;
	}
	else
	{
		print "<li>";
	}
	if( $count > 5 )
	{
		print "<a target=\"_blank\" href=\"$base_url/$eprintid\">\"$title\"</a>\n";
	}
	else
	{
		my $eprint = EPrints::DataObj::EPrint->new( $session, $eprintid );
		my $citation = $eprint->render_citation_link( "default", target=>"_blank" );
		print $citation->toString;
	}

	print "</li>\n";
}
print "</ul>";

$session->terminate;


