#!/usr/bin/perl -w -I /opt/eprints3/perl_lib

=pod

=head1 NAME

B<create_transfers> - Create Archivematica records which dont already exist for live EPrint records.

=head1 SYNOPSIS

B<create_transfers> I<repository_id>

=head1 DESCRIPTION

=head1 ARGUMENTS

=over 8

=item B<repository_id> 

The ID of the eprint repository to use.

=back

=head1 OPTIONS

=over 8

=item B<--help>

Print a brief help message and exit.

=item B<--man>

Print the full manual page and then exit.

=item B<--verbose>

Explain in detail what is going on.
May be repeated for greater effect.

=item B<--version>

Output version information and exit.

=back   

=cut

use EPrints;
use strict;
use Getopt::Long;
use Pod::Usage;

my $version = 0;
my $verbose = 0;
my $force = 0;
my $datasetid = 'eprint'; # default to eprint
my $dataobjid = '';
my $help = 0;
my $man = 0;
my $limit=0;

Getopt::Long::Configure("permute");

GetOptions(
        'help|?' => \$help,
        'man' => \$man,
        'version' => \$version,
        'verbose+' => \$verbose,
        'datasetid=s' => \$datasetid,
        'dataobjid=s' => \$dataobjid,
		'limit=s' => \$limit,
        'force' => \$force,
) || pod2usage( 2 );
EPrints::Utils::cmd_version( "process_transfers" ) if $version;
pod2usage( 1 ) if $help;
pod2usage( -exitstatus => 0, -verbose => 2 ) if $man;
pod2usage( 2 ) if( @ARGV < 1 );

my $noise = 1;
$noise = 1+$verbose if( $verbose );

my $repoid = shift(@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 );
my $searchexp = new EPrints::Search( session=>$session, dataset=>$ds );

$searchexp->add_field( $ds->get_field( "eprint_status" ), "archive", "EQ" ) if $datasetid eq "eprint"; # very eprint ds specific

if( $datasetid && $dataobjid )
{
        print "Limiting to $datasetid/$dataobjid\n" if $verbose;
        # $searchexp->add_field( $ds->get_field( "datasetid" ), $datasetid, "EQ" );
        $searchexp->add_field( $ds->get_key_field(), $dataobjid, "EQ" );
}

my $list = $searchexp->perform_search;

my $a_ds = $session->dataset( "archivematica" );

print "Found " . $list->count() . " records to process.\n" if $verbose;
my $created = 0;
$list->map( sub {
	my( $session, $dataset, $obj ) = @_;

	# create an archivematica record for this item if one doesnt exist

	my $a_searchexp = new EPrints::Search( session=>$session, dataset=>$a_ds );
	$a_searchexp->add_field( $a_ds->get_field( "datasetid" ), $datasetid, "EQ" );
	$a_searchexp->add_field( $a_ds->get_field( "dataobjid" ), $obj->id, "EQ" );
	my $a_list = $a_searchexp->perform_search;

	if ($limit != 0) {return if $created >= $limit;}
	if( !$a_list || $a_list->count() == 0 )
	{
		print "Creating record for: " . $obj->id . "\n" if $verbose;
		# create a new entry
		my $am=$session->dataset( "archivematica" )->create_dataobj({
			datasetid => $datasetid,
			dataobjid => $obj->id,
			is_dirty => 'TRUE',
		});
		$am->add_to_record_log( "create_transfer", "created", "success" );
		$am->commit();
		$created++;
	}
	else
	{
		print "Record exists for: " . $obj->id . "\n" if $verbose;
	}
});

print "Created $created new records\n" if $verbose;

$session->terminate();
