#!/usr/bin/perl -w

#
# Name:		ag_xmlrepos
# Authors:	Lukas Ocilka <locilka@suse.cz>
# Summary:	Agent for parsing any XML files
#

use strict;
use lib "/usr/lib/YaST2/agents_non_y2";
use ycp;
use XML::Bare;

while (<STDIN>) {
    my ($command, $path, $file) = ycp::ParseCommand ($_);

    if ($command eq "Read") {
	if (! defined $file || $file eq "") {
	    y2error ("No file defined!");
	    ycp::Return (undef);
	    next;
	} elsif (! -e $file) {
	    y2error ("File ".$file." doesn't exist!");
	    ycp::Return (undef);
	    next;
	}

	y2milestone ("Reading: '".$file."'");
	open (FILE, $file) || do {
	    y2error ("Cannot open file ".$file.": ".$!);
	    next;
	};
	my @lines = <FILE>;
	close FILE;

	my $xml = new XML::Bare ('text' => join ('', @lines));
	my $root = $xml->parse();
	undef $xml;

	ycp::Return ($root);
    } elsif ($command eq "result") {
	exit 0;
    } else {
	my $return_value = sprintf( "Unknown instruction %s", $command);
	ycp::Return ($return_value);
    }
}
