#!/usr/local/bin/perl5.8.0 -w -I/opt/eprints2/perl_lib 

use EPrints::EPrint;
use EPrints::Document;
use EPrints::Session;

use strict;

# the userid to make new eprints belong to
my $USERID = 4;
# the id of the archive to import to
my $ARCHIVEID = 'ep2stable';


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

my $ds = $session->get_archive->get_dataset( 'inbox' );


################################
# this part creates an eprint & a document.
# it could be in a loop with a function to generate
# $data and $file
################################

# create a new eprint record

my $data = {
	userid => $userid,
	title => "My example",
	type => "article",
	creators => [ { id=>"", main=>{ family=>"Smith",given=>"P." }, {id=>"", main=>{ family=>"Jones", given=>"H." } } ]
# add other fields here.
# multiple fields should be lists ie ["a","b","c"]
# Look in ArchiveMetadataFieldsConfig.pm for a list of fields
};

my $eprint = EPrints::EPrint::create( $session, $ds, $data );

# add a document to the eprint
$file = "/tmp/myfile.pdf";
my $doc = EPrints::Document::create( $session, $eprint );
$file =~ m!([^/]*)$!;
$doc->add_file( $file, $1 );
$doc->set_value( "type", "pdf" );
$doc->commit();

##########################################
# eprint and document now created
##########################################

# clean up.

$session->terminate();
exit;

