######################################################################
#
#  EPrints Object Exporter
#
######################################################################
#
#  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 strict;
my $session = new EPrints::Session;
exit( 0 ) unless( defined $session );
# $session->get_database->set_debug( 1 );

my $path_info = $session->get_request->path_info;

unless( $path_info =~ m!^/([0-9]+)(/([^/]*)?)?! ) 
{
	error( $session, $session->html_phrase( "cgi/export:no_id" ) );
	$session->terminate;
	exit;
}

my $id = $1;
my $afterid = $2;
my $format = $3;
my $export_url = $session->get_repository->get_conf( "perl_url" )."/export";

if( !defined $id ) 
{
	error( $session, $session->html_phrase( "cgi/export:no_id" ) );
	$session->terminate;
	exit;
}

if( !defined $afterid ) 
{
	$session->redirect( $export_url."/".$id."/" );
	$session->terminate;
	exit;
}

my $eprint = EPrints::DataObj::EPrint->new( 
			$session, 
			$id, 
			$session->get_repository->get_dataset( "archive" ) );

if( !defined $eprint )
{
	error( 
		$session, 
		$session->html_phrase( 
			"cgi/export:eprint_not_fount",
			eprintid => $session->make_text( $id ) ) );
	$session->terminate;
	exit;
}

if( !defined $format || $format eq "" ) 
{
	my $title = $session->html_phrase( "cgi/export:title",
			shortname=>$eprint->render_description );
	my $page = $session->html_phrase( "cgi/export:page",
		citation => $eprint->render_citation_link,
		formats => $eprint->render_export_links ); 
	$session->build_page( $title, $page, "export" );
	$session->send_page;
	$session->terminate;
	exit;
}

my @plugins = $session->plugin_list( 
				type=>"Export", 
				can_accept=>"dataobj/eprint", 
				is_visible=>"all" );
my $ok = 0;
foreach( @plugins ) { if( $_ eq "Export::$format" ) { $ok = 1; last; } }
unless( $ok ) 
{
	error( $session, $session->html_phrase( "cgi/export:not_available",
				format => $session->make_text( $format ) ) );
	$session->terminate;
	exit;
}

my $plugin = $session->plugin( "Export::$format" );
$session->send_http_header( "content_type"=>$plugin->param("mimetype") );
print $eprint->export( $format );	
	
$session->terminate;
exit;

sub error
{
	my( $session, $msg ) = @_;

	$session->build_page( 
		$session->html_phrase( "cgi/export:error_title" ),
		$msg,
		"export_error" );
	$session->send_page;
}

