#!/usr/bin/perl -w -I/opt/ep2stable/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<export_xml> - export a dataset as XML.

=head1 SYNOPSIS

B<export_xml> I<archiveid> [B<options>] [I<dataset>]

=head1 DESCRIPTION

This command outputs the named dataset as XML.

=head1 ARGUMENTS

=over 8

=item I<archiveid> 

The ID of the EPrint archive to use.

=item I<dataset>

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

=back

=head1 OPTIONS

=over 8

=item B<--xmlversion> I<version>

The version of the eprints data XML format to use. Either "1" or "1.1". Default is (currently) "1". 1.1 is still under development and may change. We strongly recommend not using 1.1 until we finalise it in a later version of EPrints.

=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.

=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: 2.3.13.1

=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::Session;
use EPrints::XML;

my $version = 0;
my $verbose = 0;
my $quiet = 0;
my $xmlversion = "1";
my $purge = 1;
my $help = 0;
my $man = 0;

GetOptions( 
	'help|?' => \$help,
	'man' => \$man,
	'xmlversion=s' => \$xmlversion , 
	'version' => \$version,
	'verbose+' => \$verbose,
	'silent' => \$quiet,
	'quiet' => \$quiet
) || 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 $session = new EPrints::Session( 1, $ARGV[0], $noise );
exit( 1 ) unless defined $session;

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

my $ns = EPrints::XML::namespace( 'data', $xmlversion );
if( !defined $ns )
{
	print STDERR "Unknown XML version: $xmlversion\n";
	$session->terminate;
	exit 1;
}

my $sexp = EPrints::SearchExpression->new( 
		allow_blank => 1 , 
		dataset => $ds , 
		session => $session );
$sexp->perform_search();
my $info = { doc => EPrints::XML::make_document() };
print <<END;
<?xml version="1.0" encoding="utf-8" ?>
<eprintsdata xmlns="$ns">
END
$sexp->map( \&deal, $info );
print '</eprintsdata>'."\n";
$session->terminate();

exit;

sub deal
{
	my( $session, $dataset, $item, $info ) = @_;

	my $frag = $item->to_xml( no_xmlns=>1, version=>$xmlversion, show_empty=>1 );
	print EPrints::XML::to_string( $frag )."\n";
	EPrints::XML::dispose( $frag );

}


