#!/usr/bin/perl -w

# Quick and dirty perl script to convert RSS feed subscription data from the
# Nokia N810 RSS Reader to the com.meecal.feedreader Android App.
#
# It works by parsing the OPML of the N810 RSS Reader and generating 
# SQL "INSERT" statements for the sqlite3 database of the Android feedreader.
#
#   (C) 2010 by Harald Welte <laforge@gnumonks.org>
#
#   This program 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 3 of the License, or
#   (at your option) any later version.
#
#   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, see <http://www.gnu.org/licenses/>.

# KNOWN PROBLEMS:
#  * it does not properly generate sections/groups, i.e. you will have to
#    manually create the groups and sort the feeds into them
#  * all items will be 'unread' again

use XML::Simple;
use Data::Dumper;
use strict;

my $file = 'feedlist.opml';
my $xs1 = XML::Simple->new();

my $doc = $xs1->XMLin($file);

my $body = $doc->{body};

sub handle_outline($)
{
	my $outlines = shift;
	foreach my $id (keys %$outlines) {
		my $entry = $$outlines{$id};
		if ($$entry{outline}) {
			handle_outline($$entry{outline});
		} else {
			#print Dumper($$entry{$id});
			printf("INSERT INTO Feed (CategoryId, FeedUrl, Title, Link) VALUES (%u, \"%s\", \"%s\", \"%s\");\n", 1, $$entry{xmlUrl}, $$entry{text}, $$entry{htmlUrl});
		}
	}
}

foreach my $href ($body->{outline}) {
	handle_outline($href);
}

#foreach my $key (keys (%{$doc->{species}})){
#   print $doc->{species}->{$key}->{'common-name'} . ' (' . $key . ') ';
#   print $doc->{species}->{$key}->{conservation}->final . "\n";
#}
