#!/usr/bin/perl

=head1 NAME

Olive -- The Totally Sweet Newsfeeder

=head1 DESCRIPTION

Olive is a totally sweet console mode RSS aggregator / newsreader
written in Perl with Curses::UI as its toolkit.

=head1 SYNOPSIS

    olive             # run olive
    olive <file.opml> # import a feedlist from an OPML file

=cut

use warnings;
use strict;
use lib ".";

use Config::YAML;
use Curses::UI;
use DBI;
use LWP::UserAgent;

use OliveFeed;
use OliveMisc;
use OliveStory;
use OliveWindow;

# force XML::Simple to use XML::Parser instead of XML::SAX
$XML::Simple::PREFERRED_PARSER = "XML::Parser";

# set uber-config vars
my $rel = 'r1';
my $rev = '$Rev: 439 $';
$rev =~ s/\D//g;
$rev = uc(sprintf("%4.4x",$rev));
my $tz  = `date +%z`;
$tz =~ s/00$//;

# system init;
sysinit();

# OPML import (and help/version messages)
if (@ARGV) {
    showhelp() if ($ARGV[0] eq '-h' or $ARGV[0] eq '--help');
    showver($rel, $rev)  if ($ARGV[0] eq '-v' or $ARGV[0] eq '--version');
    my $opml = shift;
    require OliveOPML;
    import OliveOPML;
    opmlimport($opml);
    exit;
}

# create Curses::UI object
my $cui = new Curses::UI( -color_support => 1,
                          -clear_on_exit => 1,
                          -keydelay      => 30,
                          -mouse_support => 0,
                        );
#create objects, windows and userdata storage
my $root = $cui->add('root', 'Window');
my $ud = { dbh   => DBI->connect("dbi:SQLite:dbname=$ENV{HOME}/.olive/stories.db","",""),
           feeds => "$ENV{HOME}/.olive/feeds",
           focus => 'news',
           rel   => $rel,
           rev   => $rev,
           opts  => [ qw(sls snu knf gsp dst coe) ],
           tz    => $tz,
         };
$cui->userdata($ud);
$cui->userdata->{c}    = Config::YAML->new( config => "$ENV{HOME}/.olive/olive.yaml" );
$cui->userdata->{sid}  = 0;
$cui->userdata->{ua}   = LWP::UserAgent->new( agent   => 'Olive/' . $cui->userdata->{rel}, 
                                              timeout => ($cui->userdata->{c}->{to} || 30) );
$cui->userdata->{wins} = wininit($cui,$root);
$cui->userdata->{keys} = { prev      => '[',
                           next      => ']',
                           mark      => 'm',
                           unmark    => 'u',
                           markall   => 'M',
                           unmarkall => 'U',
                           star      => 's',
                           focus     => 'w',
                           link      => 'l',
                           linklist  => 'L',
                           poll      => 'p',
                           force     => 'P',
                           filterf   => 'F',
                           filters   => 'S',
                           gpup      => '-',
                           gpdn      => ' ',
                          };
@{ $cui->userdata->{keys} }{ keys %{$cui->userdata->{c}->{keys}} } = 
    values %{ $cui->userdata->{c}->{keys} };

# set up database, keybindings, and timer loop
sysinit2($cui);
setkeys($cui,$root,"./docs");
setpollwait($cui);

# run a poll and populate the story list
feedpoll($cui);
refreshlist($cui);

# go!
$cui->mainloop();

#-------------------------------------------------------------

sub showhelp {
    print <<HELP;

Usage is: olive [<options> | <filename.opml>]

If a filename is specified, an OPML feed import will be performed and
the program will exit. Otherwise the program will start normally.
Options are:

 -h  --help     Show this message and exit
 -v  --version  Display version string and exit

HELP
1;
    exit;
}

sub showver {
    print "This is Olive $_[0] ($_[1])\n";
    exit;
}

=head1 FILES

    ~/.olive/olive.yaml # config file
    ~/.olive/stories.db # SQLite feed data db
    ~/.olive/errors.log # logger messages go here

=head1 COPYRIGHT & LICENSE

Copyright 2005,2006 Shawn Boyette, All Rights Reserved.

This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut
