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

######################################################################
#
# cjg Not doc'd yet, too flakey
#
######################################################################
#
#  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
#
######################################################################

use EPrints::Session;
use EPrints::XML;
#cjg Nice Command Options
#cjg man page

if( scalar @ARGV != 2 )
{
	print STDERR "export_xml <siteid> <dataset>\n";
	exit( 1 );
}

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

my $session = new EPrints::Session( 1 , $ARGV[0] );
exit( 1 ) unless( defined $session );

my $ds = $session->get_archive()->get_dataset( $ARGV[1] ) ;
if( !defined $ds )
{
	die "Bad Dataset ID";
}

my $sexp = EPrints::SearchExpression->new( 
		allow_blank => 1 , 
		dataset => $ds , 
		session => $session );
$sexp->perform_search();
my $info = { doc => EPrints::XML::make_document() };
print '<eprintsdata>'."\n";
$sexp->map( \&deal, $info );
print '</eprintsdata>'."\n";
$session->terminate();

exit;

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

	my $frag = $session->make_doc_fragment;
	$frag->appendChild( $session->make_text( "  " ) );
	my $r = $session->make_element( "record" );
	$r->appendChild( $session->make_text( "\n" ) );
	foreach my $field ( $dataset->get_fields() )
	{
		next unless( $field->get_property( "export_as_xml" ) );

		$r->appendChild( &mk_xml( 
			$session, 
			$field,
			$item->get_value( $field->get_name() ) ) );
	}
	$r->appendChild( $session->make_text( "  " ) );
	$frag->appendChild( $r );
	$frag->appendChild( $session->make_text( "\n" ) );

	print EPrints::XML::to_string( $frag )."\n";
	EPrints::XML::dispose( $frag );

}

sub mk_xml
{
	my( $session, $field, $v ) = @_;

	my $r = $session->make_doc_fragment;
	if( $field->get_property( "multiple" ) )
	{
		foreach( @{$v} )
		{
			$r->appendChild( $session->make_text( "    " ) );
			$r->appendChild( &mk_xml2( $session, $field, $_ ) );
			$r->appendChild( $session->make_text( "\n" ) );
		}
	}
	else
	{
		$r->appendChild( $session->make_text( "    " ) );
		$r->appendChild( &mk_xml2( $session, $field, $v ) );
		$r->appendChild( $session->make_text( "\n" ) );
	}
	return $r;
}

sub mk_xml2
{
	my( $session, $field, $v ) = @_;

	my %attrs = ( name=>$field->get_name() );
	if( $field->get_property( "hasid" ) )
	{
		$attrs{id} = $v->{id};
		$v = $v->{main};
	}
	my $r = $session->make_element( "field", %attrs );

	if( $field->get_property( "multilang" ) )
	{
		foreach( keys %{$v} )
		{
			my $l = $session->make_element( "lang", id=>$_ );
			$l->appendChild( mk_xml3( $session, $field, $v->{$_} ) );
			$r->appendChild( $l );
		}
	}
	else
	{
		$r->appendChild( mk_xml3( $session, $field, $v ) );
	}

	return $r;
}

sub mk_xml3
{
	my( $session, $field, $v ) = @_;

	my $r = $session->make_doc_fragment;
	if( $field->is_type( "name" ) )
	{
		foreach( "honourific", "given", "family", "lineage" )
		{
			next unless( defined $v->{$_} && $v->{$_} ne "" );
			my $e = $session->make_element( "part", name=>$_ );
			$e->appendChild( $session->make_text( $v->{$_} ) );
			$r->appendChild( $e );
		}
	}
	else
	{
		$r->appendChild( $session->make_text( $v ) ) if defined( $v );
	}
	return $r;
}
