#!/usr/bin/perl
#
# Snownews - A lightweight console RSS newsreader
#
# Copyright 2003-2004 Oliver Feiler <kiza@kcore.de>
# http://kiza.kcore.de/software/snownews/
#
# opml2snow
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

use XML::LibXML;

if ($ARGV[0] eq "-h") {
	print "Snownews opml2snow - subsription file import utility\n\n";
	print "Creative usage examples:\n";
	print "       opml2snow <list.opml >urls\n";
	print "       opml2snow list.opml >urls\n";
	print "       cat list.opml | opml2snow >urls\n\n";
	print "The program will print to stdout by default. If you want to append the output\n".
		"to your subsription list, redirect to >>~/.snownews/url instead.\n";
	exit (0);
}

my $parser = XML::LibXML->new();
$parser->validation(0);				# Turn off validation from libxml
$parser->recover(1);				# And ignore any errors while parsing

@lines = <>;						# The diamond operator. Someone told me this thing ruleZ.
$input = join ("\n", @lines);

my $doc = $parser->parse_string($input);
$root = $doc->documentElement();

# Parsing the document tree using xpath
@items = $root->findnodes("//outline");
foreach (@items) {
	@attrs = $_->attributes();
	foreach (@attrs) {
		# Only print attribute xmlUrl=""
		if ($_->nodeName =~ /xmlUrl/i) {
			print $_->value."\n";
		}
	}
}
