The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl
use strict;
$|++;

my $VERSION = '0.15';

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

=head1 NAME

cpanstats-delete - script to delete entries from the cpanstats database.

=head1 SYNOPSIS

  perl cpanstats-delete --config=<file> [-a=0] [-d=0] [--file=<file>]

=head1 DESCRIPTION

Using the cpanstats database, deletes the given entries.

=head1 OPTIONS

=over 4

=item --config

Configuration file contain database access details.

=item -a | --all

Delete all the cpanstats report ids greater than the specified number.

=item -d | --delete

Delete a single cpanstats report id.

=item --file

Named file used when deleting a list of cpanstats report ids.

=back

=cut

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

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

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

my (%options);

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

##### INITIALISE #####

init_options();

##### MAIN #####

my @list = get_list();
for my $id (@list) {
        print "Deleting ... $id\n";
        $options{CPANSTATS}->do_query("DELETE from cpanstats WHERE id=$id");
        $options{LITESTATS}->do_query("DELETE from cpanstats WHERE id=$id");
}

if($options{all}) {
        $options{CPANSTATS}->do_query("DELETE from cpanstats WHERE id>$options{all}");
        $options{LITESTATS}->do_query("DELETE from cpanstats WHERE id>$options{all}");
}
if($options{delete}) {
        $options{CPANSTATS}->do_query("DELETE from cpanstats WHERE id=$options{delete}");
        $options{LITESTATS}->do_query("DELETE from cpanstats WHERE id=$options{delete}");
}

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

=item get_list

Returns the list of cpanstats report ids from the named file.

=cut

sub get_list {
        my @list;
        my $file = $options{file} || return ();
        die "file [$file] not found"    unless(-f $file);

        my $fh = IO::File->new($file)   or die "Cannot open file [$file]: $!";
        while(<$fh>) {
                chomp;
                my ($num) = (m/^(\d+)/);
                push @list, $num        if($num);
        }
        $fh->close;
        return @list;
}

=item init_options

Determine command line options and initialise any defaults.

=cut

sub init_options {
    GetOptions( \%options,
        'config|c=s',
        'all|a=i',
        'delete|d=i',
        'file=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});

    # 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> \\
         ( [--all=<num>] [--delete=<num>] [--file=<file>] | -h | -v )

  --config=<file>   configuration file
  --all=<num>       delete all entried greater than given id
  --delete=<num>    delete given id
  --file=<file>     delete multiple ids (1 per line)
  -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 dependent upon their severity and my availability. 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::Testers::WWW::Reports>,
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-2012 Barbie for Miss Barbell Productions.

  This module is free software; you can redistribute it and/or
  modify it under the Artistic License 2.0.

=cut