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

my $VERSION = '0.01';

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

=head1 NAME

cpanstats-select - select stats from the CPAN Testers Statistics database.

=head1 SYNOPSIS

  perl cpanstats-convert ( --config=<file> | --help | --version )

=head1 DESCRIPTION

Using the cpanstats database, which should be in the local directory, extracts
all the required data.

=head1 OPTIONS

=over 4

=item --config

Specify the configuration file that provides database access details..

=back

=cut

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

use Config::IniFiles;
use CPAN::Testers::Common::DBUtils;
use Getopt::Long;

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

my (%options);

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

##### INITIALISE #####

init_options();

##### MAIN #####

my $next = $options{LITESTATS}->iterator(
            'array',
            'SELECT id,state,postdate,tester,dist,version,platform,perl,osname,osvers,date FROM cpanstats ORDER BY id');
while(my $row = $next->()) {
    $options{CPANSTATS}->do_query(
            'INSERT INTO cpanstats VALUES (?,?,?,?,?,?,?,?,?,?,?)',
            @$row);
}


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

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

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

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

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

    # configure databases
    for my $db (qw(CPANSTATS LITESTATS)) {
        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);
        $options{$db} = CPAN::Testers::Common::DBUtils->new(%opts);
        die "Cannot configure $db database\n" unless($options{$db});
    }
}

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

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

    if($full) {
        print <<HERE;

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

  --config=<file>   database configuration file
  -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, E<lt>barbie@cpan.orgE<gt>
for Miss Barbell Productions L<http://www.missbarbell.co.uk>.

=head1 COPYRIGHT AND LICENSE

  Copyright (C) 2005-2008 Barbie for Miss Barbell Productions
  All Rights Reserved.

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

=cut
