################################################## -*- mode: perl -*-
#
#  EPrints Person Identification Aid tool
#
######################################################################
#
#  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 strict;

use EPrints::Session;

use ACIS::PIDAID;
use ACIS::PIDAID::XML;

sub get_pidaid {
  if ( not $ACIS::PIDAID::interface ) {
    $ACIS::PIDAID::interface = ACIS::PIDAID -> new();
  }
  return $ACIS::PIDAID::interface;
}
    



my $session = new EPrints::Session;
if ( not $session ) {
  print "Content-Type: text/plain\n\n";
  exit 0;
}
exit( 0 ) unless( defined $session );



my $request   = $session ->get_request;

my $path_info = $request ->path_info;
# trim a leading slash
$path_info =~ s#^/##;

my $searchtype = $path_info;

#my $ct = $request ->header_in("Content-type");
#warn "content-type: $ct\n";

#my @a = ( $request -> content() );
#warn 'Parameters: '. join( '==', @a ) . "\n";

my $q = $session->{query};
my %args;
my @a       = ( $q ->param );
foreach ( @a ) {
  $args{$_} = $q -> param($_);
}

if ( not scalar %args ) {
  warn "no arguments\n";
} else {
#  warn 'Parameters: '. join( '==', %args ) . "\n";
}

my $result;
my $pid;

if ( defined $args{l} 
     or $args{f} ) {
  my $lname = $args{l};
  my $fname = $args{f};
  
  $pid    = get_pidaid();
  $result = $pid -> find_by_name( $lname, $fname );
  
} elsif ( $args{e} ) {
  $pid    = get_pidaid();
  my $email = $args{e};
  $result = $pid -> find_by_email( $email );

} elsif ( $args{s} ) {
  $pid    = get_pidaid();
  my $sid = $args{s};
  $result = $pid -> find_by_shortid( $sid );

}

$session -> send_http_header( content_type => 'text/xml; charset=utf-8' );

# Was checking it in real slow mode:
# sleep 2;

if ( not $result ) {
  print "<problem/>";
} else { 
  my $xml = ACIS::PIDAID::XML::make_xml( $result );
  binmode( STDOUT, ":utf8" );
  print $xml;
}


$session->terminate;
