#!/usr/bin/perl -w

use FindBin;
use lib "$FindBin::Bin/../perl_lib";

######################################################################
#
#
######################################################################

=pod

=for Pod2Wiki

=head1 NAME

B<export> - export some or all items in a dataset using an output plugin.

=head1 SYNOPSIS

B<export> I<repository_id> [B<options>] I<dataset> 

B<export> I<repository_id> [B<options>] I<dataset> I<plugin> [I<objectid>] [I<objectid...>]

=head1 DESCRIPTION

This command outputs a dataset in the selected format. The formats available depend on what Export plugins have been installed.

Some plugins only operate on lists (e.g. RSS), others only operate on single objects only. Some plugins act slightly differently in single-item only mode. For example, outputing a list of eprints as XML gives a number of <eprint> elements in an outer <eprints> element. 

To get the plugin to export a single item use --single. This may not always return a complete file. For example it may be missing the <?xml header, depending on the plugin.

=head1 ARGUMENTS

=over 8

=item I<repository_id> 

The ID of the EPrint repository to use.

=item I<dataset>

The name of the dataset to export, such as "archive", "subject" or "user".

=item I<plugin>

The id of the output plugin to use. This should not include the leading "Export::". Examples: BibTeX, XML.

If this is ommited or an invalid plugin is requested, then 'export' will list all plugins compatible with the dataset and exit.

=item I<objectid>

If this is set then just output the single, specified item or items.

=back

=head1 OPTIONS

=over 8

=item B<--arg key=value>

Add an argument to the export plugin. May be repeated. Effects depend on te export plugin.

=item B<--help>

Print a brief help message and exit.

=item B<--man>

Print the full manual page and then exit.

=item B<--quiet>

Be vewwy vewwy quiet. This option will supress all output unless an error occurs.

=item B<--verbose>

Explain in detail what is going on.
May be repeated for greater effect.

Shows why a plugin is disabled.

=item B<--version>

Output version information and exit.

=back   



=cut


use Getopt::Long;
use Pod::Usage;
use strict;

use EPrints;

my $version = 0;
my $verbose = 0;
my $quiet = 0;
my $purge = 1;
my $help = 0;
my $man = 0;
my $single = 0;
my %opt_args = ();

Getopt::Long::Configure("permute");

GetOptions( 
	'help|?' => \$help,
	'man' => \$man,
	'version' => \$version,
	'verbose+' => \$verbose,
	'silent' => \$quiet,
	'quiet' => \$quiet,
	'single' => \$single,
	'arg=s' => \%opt_args,
) || pod2usage( 2 );
EPrints::Utils::cmd_version( "export_xml" ) if $version;
pod2usage( 1 ) if $help;
pod2usage( -exitstatus => 0, -verbose => 2 ) if $man;
pod2usage( 2 ) if( scalar @ARGV < 2 );

my $noise = 1;
$noise = 0 if( $quiet );
$noise = 1+$verbose if( $verbose );

# Set STDOUT to auto flush (without needing a \n)
$|=1;

my $repoid = shift @ARGV;
my $datasetid = shift @ARGV;
my $opluginid = shift @ARGV;
my @objectids = @ARGV;

my $session = new EPrints::Session( 1, $repoid, $noise );
if( !defined $session )
{
	print STDERR "Failed to load repository: $repoid\n";
	exit 1;
}

my $ds = $session->dataset( $datasetid ) ;
if( !defined $ds )
{
	print STDERR "Unknown Dataset ID: $datasetid\n";
	$session->terminate;
	exit 1;
}

if( $single && (scalar @objectids) != 1 )
{
	print STDERR "With --single exactly one object ID must be specified.\n";
	$session->terminate;
	exit 1;
}

if( !defined $opluginid )
{
	my @list_plugins = $session->plugin_list( 
					type=>"Export",
					can_accept=>"list/".$ds->confid );
	my @dataobj_plugins = $session->plugin_list( 
					type=>"Export",
					can_accept=>"dataobj/".$ds->confid );
	my %p = ();
	my %l = ();
	my %d = ();
	foreach( @list_plugins ) { $p{$_} = $_; $l{$_} = 1; }
	foreach( @dataobj_plugins ) { $p{$_} = 1; $d{$_} = 1; }
	print "Available output formats:\n";
	foreach my $a_plugin_id ( sort keys %p ) 
	{
		my $a_plugin = $session->plugin( $a_plugin_id );
		printf( "% 16s", $a_plugin->get_subtype);
		print ": ".$a_plugin->get_name();
		if( $l{$a_plugin_id} && !$d{$a_plugin_id} )
		{
			print " (List export only)";
		}
		if( $d{$a_plugin_id} && !$l{$a_plugin_id} )
		{
			print " (Single object export only)";
		}
		if( $a_plugin->broken )
		{
			print " (DISABLED)";
			print "\n** Disabled because: ".$a_plugin->error_message if( $noise > 1 );
		}
		print "\n";
	}
	$session->terminate();
	exit;
}

my $pluginid = "Export::".$opluginid;
my $plugin = $session->plugin( $pluginid );

if( !defined $plugin )
{
	# This warning is already generated by the plugin
	# code.
	#print STDERR "Plugin $pluginid not found.\n";
	$session->terminate();
	exit 1;
}
	
if( $plugin->broken )
{
	print STDERR "Plugin $pluginid could not run because:\n";
	print STDERR $plugin->error_message."\n";
	$session->terminate();
	exit 1;
}

my %arguments = %{$plugin->param( "arguments" )};
foreach my $argid ( keys %opt_args )
{
	if( !exists $arguments{$argid} )
	{
		print STDERR "Plugin $pluginid does not accept argument $argid\n";
		$session->terminate();
		exit 1;
	}
	$arguments{$argid} = $opt_args{$argid};
}

$plugin->initialise_fh( \*STDOUT );
print $plugin->byte_order_mark;

if( $single )
{
	my $objectid = $objectids[0];

	my $req_plugin_type = "dataobj/".$ds->confid;

	unless( $plugin->can_accept( $req_plugin_type ) )
	{
		print STDERR "Plugin $pluginid can't process $req_plugin_type data.\n";
		$session->terminate();
		exit 1;
	}
	my $dataobj = $ds->get_object( $session, $objectid );
	if( !defined $dataobj ) 
	{
		print STDERR "#$objectid not found in ".$ds->id.".\n";
		$session->terminate();
		exit 1;
	}

	print $dataobj->export( $opluginid, %arguments );
}
else
{
	my $req_plugin_type = "list/".$ds->confid;

	unless( $plugin->can_accept( $req_plugin_type ) )
	{
		print STDERR "Plugin $pluginid can't process $req_plugin_type data.\n";
		$session->terminate();
		exit 1;
	}

	my $list;

	if( scalar @objectids )
	{
		$list = EPrints::List->new(
			dataset => $ds,
			session => $session,
			ids=>\@objectids );
	}
	else
	{
		my $searchexp = new EPrints::Search(
			allow_blank=>1,
			session=>$session,
			dataset=>$ds );

		$list = $searchexp->perform_search;
	}

	$plugin->output_list( list=>$list, fh=>*STDOUT, %arguments );
}

$session->terminate();
exit;

=head1 COPYRIGHT

=for COPYRIGHT BEGIN

Copyright 2018 University of Southampton.
EPrints 3.4 is supplied by EPrints Services.

http://www.eprints.org/eprints-3.4/

=for COPYRIGHT END

=for LICENSE BEGIN

This file is part of EPrints 3.4 L<http://www.eprints.org/>.

EPrints 3.4 and this file are released under the terms of the
GNU Lesser General Public License version 3 as published by
the Free Software Foundation unless otherwise stated.

EPrints 3.4 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with EPrints 3.4.
If not, see L<http://www.gnu.org/licenses/>.

=for LICENSE END

