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

use EPrints;

use strict;
use Data::Dumper;

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

if( defined $session->param( "view" ) )
{
	# hmm, got GET parameters, not a path...
	my $viewid = $session->param( "view" );
	my $values = $session->param( "values" );
	my $format = $session->param( "format" );

	my $plugin = get_plugin( $session, $format );
	if( !defined $plugin )
	{
		error( $session, $session->html_phrase( "cgi/export:not_available",
					format => $session->make_text( $format ) ) );
		$session->terminate;
		exit;
	}

	my $export_url = $session->get_repository->get_conf( "perl_url" )."/exportview";
	my $fn = $values;
	$fn =~ s/\//_/g;
	$export_url .= "/$viewid/$values/$format/$fn".$plugin->param("suffix");
	$session->redirect( $export_url );
	exit;
}


my $path_info = $session->get_request->path_info;
#http://devel.eprints.org/cgi/exportview/person/Nittrouer=3AX=2E=3A=3A/HTML/Nittrouer=3AX=2E=3A=3A.html
unless( $path_info =~ m!^/([^/]+)/(.*)/([^/]+)/[^/]+$! )
{
	error( $session, $session->html_phrase( "cgi/export:no_id" ) );
	$session->terminate;
	exit;
}
my $viewid = $1;
my @view_path = split( '/', $2 );
my $format = $3;

my $ds = $session->get_repository->get_dataset( "archive" );

my $view;
foreach my $a_view ( @{$session->get_repository->get_conf( "browse_views" )} )
{
	$view = $a_view if( $a_view->{id} eq $viewid );
}
if( !defined $view )
{
	EPrints::abort( "view with ID '$viewid' is not available." );
}

my $filters = EPrints::Update::Views::get_filters( $session, $view, \@view_path );
if( !defined $filters )
{
	$session->not_found; #404
	$session->terminate;
	exit;
}

my $search = new EPrints::Search(
				custom_order=>$view->{order},
				satisfy_all=>1,
				session=>$session,
				dataset=>$ds );
$search->add_field( $ds->get_field('metadata_visibility'), 'show', 'EQ' );
my $n=0;
foreach my $filter ( @{$filters} )
{
	$search->add_field( $filter->{fields}, $filter->{value}, "EX", undef, "filter".($n++), 1 );
}

my $plugin = get_plugin( $session, $format );
if( !defined $plugin )
{
	error( $session, $session->html_phrase( "cgi/export:not_available",
				format => $session->make_text( $format ) ) );
	$session->terminate;
	exit;
}

$session->send_http_header( "content_type"=>$plugin->param("mimetype") );

my $list = $search->perform_search;
print $list->export( $format );	
	
$list->dispose;

$session->terminate;
exit;

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

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

sub get_plugin
{
	my( $session, $format ) = @_;

	my @plugins = $session->plugin_list( 
				type=>"Export", 
				can_accept=>"list/eprint", 
				is_visible=>"all" );
	my $ok = 0;
	foreach( @plugins ) 
	{ 
		if( $_ eq "Export::$format" ) 
		{
			return $session->plugin( "Export::$format" );
		}
	}
	return;
}

