#!/usr/bin/perl -w -I/opt/eprints/perl_lib

######################################################################
#
#  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
#
######################################################################

=pod

=head1 NAME

B<import> - Import a file using an import plugin.

=head1 SYNOPSIS

B<import> I<repository_id> [B<options>] I<dataset> [I<plugin> I<filename>] 

=head1 DESCRIPTION

This command imports a set of EPrints from a file into the given dataset.

=head1 ARGUMENTS

=over 8

=item I<repository_id> 

The ID of the EPrint repository to import to.

=item I<dataset>

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

=item I<plugin>

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

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

=back

=head1 OPTIONS

=over 8

=item B<--user USERID/USERNAME> 

Required for importing EPrint records.

When importing EPrints, this field sets the userid of the user who will
own the created EPrint records. If set to star (*) it will use the userid
values imported from the file.

If this is an integer then it is assumed to be the userid of the user,
otherwise it is assumed to be the username.

=item B<--parse-only>

Don't import, just check the file.

=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   


=head1 AUTHOR

This is part of this EPrints 2 system. EPrints 2 is developed by Christopher Gutteridge.

=head1 VERSION

EPrints Version: eprints-2-cvs-2006-06-27

=head1 CONTACT

For more information goto B<http://www.eprints.org/> which give information on mailing lists and the like.

Chris Gutteridge may be contacted at B<support@eprints.org>

Should you need a real world address for some reason, EPrints can be contacted in the real world at

 EPrints c/o Christopher Gutteridge
 Department of Electronics and Computer Science
 University of Southampton
 SO17 1BJ
 United Kingdom

=head1 COPYRIGHT

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


=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 $user = 0;
my $parse_only = 0;

GetOptions( 
	'help|?' => \$help,
	'man' => \$man,
	'user=s' => \$user,
	'version' => \$version,
	'verbose+' => \$verbose,
	'silent' => \$quiet,
	'quiet' => \$quiet,
	'single' => \$single,
	'parse-only' => \$parse_only,
) || 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 && scalar @ARGV !=4 );

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

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

my $repository_id = shift @ARGV;
my $datasetid = shift @ARGV;
my $ipluginid = shift @ARGV;
my $filename = shift @ARGV;

my $session = new EPrints::Session( 1, $repository_id, $noise );
exit( 1 ) unless defined $session;

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


if( !defined $ipluginid )
{
	my @list_plugins = $session->plugin_list( 
					type=>"Import",
					can_produce=>"list/".$ds->confid );
	my @dataobj_plugins = $session->plugin_list( 
					type=>"Import",
					can_produce=>"dataobj/".$ds->confid );
	my %p = ();
	my %l = ();
	my %d = ();
	foreach( @list_plugins ) { $p{$_} = $_; $l{$_} = 1; }
	foreach( @dataobj_plugins ) { $p{$_} = 1; $d{$_} = 1; }
	print "Available input formats:\n";
	foreach my $a_plugin_id ( sort keys %p ) 
	{
		my $a_plugin = $session->plugin( $a_plugin_id );
		print $a_plugin->get_subtype.": ".$a_plugin->get_name();
		if( $l{$a_plugin_id} && !$d{$a_plugin_id} )
		{
			print " (List input only)";
		}
		if( $d{$a_plugin_id} && !$l{$a_plugin_id} )
		{
			print " (Single object input 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 = "Import::".$ipluginid;
my $plugin = $session->plugin( $pluginid, parse_only=>$parse_only );

if( !defined $plugin )
{
	# This warning is already generated by the plugin
	# code.
	#print STDERR "Plugin $pluginid not found.\n";
	$session->terminate();
	exit 1;
}
	
my $req_plugin_type = "list/".$ds->confid;
	
unless( $plugin->can_produce( $req_plugin_type ) )
{
	print STDERR "Plugin $pluginid can't process $req_plugin_type data.\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 $fh;
if( $filename eq "-" )
{
	$fh = *STDIN;
}
else
{
	unless( open( INFILE, $filename ) )
	{
		print STDERR "Could not open file $filename: $!\n";
		$session->terminate();
		exit 1;
	}
	$fh = *INFILE;
}

my $list = $plugin->input_list( dataset=>$ds, fh=>$fh, filename=>$filename );

unless( $filename eq "-" )
{
	close $fh;
}

if( defined $list )
{
	print "Number of records imported: ".$list->count."\n" if $noise > 0;
	print join( ",", @{$list->get_ids})."\n" if $noise > 1;
	
}

$session->terminate();
exit;
