# -*-mode: cperl; coding: iso-latin-1-unix; fill-column:100-*-
eval 'exec perl -S $0 ${1+"$@"}' if $running_under_some_shell;

=head1 NAME

rls2xml -- Convert Rlist/XML files

=head1 SYNOPSIS

	$ rls2xml input-file [output-file]

Parses the Rlist input file and compiles XML from it, or vice versa.  The direction is deduced from
the extension of the input file. Depends on F<XML::Simple>.

=cut

# $Writestamp: 2007-11-30 15:07:43 spindlea$
# $Author: Andreas Spindler$
# $Compile: ./rls2xml September2007-65.in.out.rls out.xml$

#use Data::Rlist;
BEGIN {
    $0 =~ /[^\/]+$/;
	push @INC, $`||'.', "$ENV{HOME}/bin";
	require Rlist;
	Data::Rlist->import();		# ...call the Exporter manually
	Data::Rlist->import(qw/:aux/);
}

use XML::Simple;

my($infile, $outfile) = (shift, shift||'-');
my $rlobj = new Data::Rlist(-input => $infile, -output => $outfile, -echo => 1);

if ($infile =~ /\.x\w+$/i) {
	# Generically compile XML to Rlist.
	#
	$rlobj->set(-data => XMLin($infile))->write();
} else {
	# Compile Rlist to XML.
	#
	my $data = $rlobj->read();
	$data->{links}->{page}->[0]->{time} = time();
	open O, ">$outfile" or die $!;
	print O XMLout($data, (xmldecl => 1));
	close O;
}
