#!/usr/bin/perl
use strict;
$|++;

my $VERSION = '0.03';

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

=head1 NAME

articles-view - script to view an article from the articles database.

=head1 SYNOPSIS

  perl articles-view --config=<file> --ids=0,0,0

=head1 DESCRIPTION

Reads named articles from the articles database.

=head1 OPTIONS

=over 4

=item --config

Configuration file contain database access details.

=item --ids

Comma separated list of NNTP ids.

=back

=cut

# -------------------------------------
# Library Modules

use lib	qw(./lib ../lib);

use Config::IniFiles;
use CPAN::Testers::Common::DBUtils;
use Net::NNTP;
use Getopt::ArgvFile default=>1;
use Getopt::Long;
use IO::File;

# -------------------------------------
# Variables

my (%log,%options,$dbi);

use constant    NNTPSTART   => 0;

# -------------------------------------
# Program

init_options();
view_articles();

# -------------------------------------
# Subroutines

=item view_articles

Report on the database entries which are either missing, or have reported bad
processing.

=cut

sub view_articles {
    my $count = $options{start};
    my $iterator = $dbi->iterator('array',"SELECT * FROM articles WHERE id IN ($options{ids}) ORDER BY id asc");
    while(my $row = $iterator->()) {
        print "ID:$row->[0]\nARTICLE:\n$row->[1]\n";
    }
}

=item init_options

Determine command line options and initialise any defaults.

=cut

sub init_options {
    GetOptions( \%options,
        'config=s',
        'ids=s',
        'help|h',
        'version|v'
    );

    help(1) if($options{help});
    help(0) if($options{version});

    help(1,"Must specify the configuration file")               unless($options{config});
    help(1,"Configuration file [$options{config}] not found")   unless(-f $options{config});
    help(1,"No ids given")                                      unless($options{ids});

    # load configuration
    my $cfg = Config::IniFiles->new( -file => $options{config} );

    # configure databases
    my $db = 'LITEARTS';
    die "No configuration for $db database\n"   unless($cfg->SectionExists($db));
    my %opts = map {$_ => $cfg->val($db,$_);} qw(driver database dbfile dbhost dbport dbuser dbpass);
    $dbi = CPAN::Testers::Common::DBUtils->new(%opts);
    die "Cannot configure $db database\n" unless($dbi);
}

sub help {
    my ($full,$mess) = @_;

    print "\n$mess\n\n" if($mess);

    if($full) {
        print <<HERE;

Usage: $0 \\
     --config=<file>				\\
     [--ids=<string>] [-h] [-v]

  --config=<file>   - configuration file
  --ids=<string>    - article ids to view (CSV)
  -h                - this help screen
  -v                - program version

HERE

    }

    print "$0 v$VERSION\n";
    exit(0);
}

__END__

=back

=head1 BUGS, PATCHES & FIXES

There are no known bugs at the time of this release. However, if you spot a
bug or are experiencing difficulties, that is not explained within the POD
documentation, please send bug reports and patches to the RT Queue (see below).

Fixes are dependant upon their severity and my availablity. Should a fix not
be forthcoming, please feel free to (politely) remind me.

RT Queue -
http://rt.cpan.org/Public/Dist/Display.html?Name=CPAN-Testers-Data-Generator

=head1 SEE ALSO

L<CPAN::WWW::Testers>,
L<CPAN::Testers::WWW::Statistics>

F<http://www.cpantesters.org/>,
F<http://stats.cpantesters.org/>,
F<http://wiki.cpantesters.org/>

=head1 AUTHOR

  Barbie, <barbie@cpan.org>
  for Miss Barbell Productions <http://www.missbarbell.co.uk>.

=head1 COPYRIGHT AND LICENSE

  Copyright (C) 2005-2009 Barbie for Miss Barbell Productions.

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

=cut

