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

my $VERSION = '0.03';

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

=head1 NAME

cpanreps-verify - Verify that the CPAN Testers Reports website pages.

=head1 SYNOPSIS

  perl cpanreps-verify

=head1 DESCRIPTION

Given the website directory and a local cpanstats database, this program will
verify that each page contains the latest updates. Outputs in a format that can
then be passed to cpanreps-update.

=cut

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

use DBI;
use File::Find::Rule;
use File::Path;
use File::Slurp;
use Getopt::ArgvFile default=>1;
use Getopt::Long;
use IO::File;
use Parse::CPAN::Authors;

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

my (%options);

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

##### INITIALISE #####

init_options();

my $dbh = DBI->connect( "dbi:SQLite:dbname=$options{testers}", '', '', { RaiseError => 1 } );
die "Cannot access database [$options{testers}]\n" unless($dbh);

check_dists();
check_authors();


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

sub check_dists {
    my (%distros,$distro);
    my $sth = $dbh->prepare("SELECT DISTINCT(dist) FROM cpanstats WHERE state='cpan'");
    $sth->execute();
    $sth->bind_columns( \$distro );
    while($sth->fetch) {
        next    unless($distro =~ /^[A-Za-z0-9][A-Za-z0-9\-_]*$/);
        $distros{$distro} = 1;
    }

    my @files = File::Find::Rule->file()->name( '*.html' )->in( "$options{directory}/show/" );
    for my $file (sort @files) {
        my ($dist) = $file =~ m!/([^/]+)\.html$!;
        next    unless($dist);
        next    if($dist =~ /[^-\w\.]/);    # illegal chars

        $distros{$dist} = 0;

        next    if($dist =~ /^index$/);     # tough luck!

        my $js = "$options{directory}/show/$dist.js.gz";
        unless(-f $js) {
            print "dist:$dist: $js [MISSING JAVASCRIPT]\n";
            next;
        }

	    my ($id,$version);
        my $sth = $dbh->prepare("SELECT id,version FROM cpanstats WHERE dist=? AND state='cpan' ORDER BY id desc LIMIT 1");
        $sth->execute($dist);
        $sth->bind_columns( \$id, \$version );
        $sth->fetch;

        next    unless($id && $version);
        next    if($version =~ /[^-\w\.]/);    # illegal chars

        my $content = read_file($file);

        #<h2><a name="1.41" />marc-lint 1.41
        next    if($content =~ m!<a\s+(id|name)="$version" />$dist!si);
        next    if($content =~ m!$dist\s+$version\s+\(No\s+reports\)!si);

        print "dist:$dist: $file [$id][$dist-$version]\n";
    }

    print "dist:$_: $options{directory}/show/$_.html [MISSING HTML]\n"
        for(grep {$distros{$_} == 1} keys %distros);
}

sub check_authors {
    my $p = Parse::CPAN::Authors->new("data/01mailrc.txt.gz");
    my %authors = map {$_->pauseid => 1} $p->authors;

    my @files = File::Find::Rule->file()->name( '*.html' )->in( "$options{directory}/author/" );
    for my $file (sort @files) {
        my ($author) = $file =~ m!/([^/]+)\.html$!;
        next    unless($author);

        $authors{$author} = 0;

        my $js = "$options{directory}/author/$author.js.gz";
        unless(-f $js) {
            print "author:$author: $js [MISSING JAVASCRIPT]\n";
            next;
        }

        my (%dists,$dist,$version);
        my $sth = $dbh->prepare("SELECT dist,version FROM cpanstats WHERE tester=? AND state='cpan' ORDER BY id desc");
        $sth->execute($author);
        $sth->bind_columns( \$dist, \$version );
        while( $sth->fetch ) {
            next unless($dist =~ /^[A-Za-z0-9][A-Za-z0-9\-_]*$/
            $dists{$dist} ||= $version;
        }

        my $content = read_file($file);

        for $dist (keys %dists) {
            $version = $dists{$dist};
            next    if($version =~ /[+]/);  # bad characters

            #<h2><a name="Games-LogicPuzzle" /><a href="../show/Games-LogicPuzzle.html">Games-LogicPuzzle</a> 0.20
            next    if($content =~ m!<h2><a name="$dist" /><a\s+href="\.\./show/$dist.html">$dist</a>\s+$version!si);

            print "author:$author: $file [$dist-$version]\n";
            last;
        }
    }

    print "author:$_: $options{directory}/author/$_.html [MISSING HTML]\n"
        for(grep {$authors{$_} == 1} keys %authors);
}

sub init_options {
    GetOptions( \%options,
        'directory|d=s',
        'testers|t=s',
        'help|h',
        'version|V'
    );

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

    unless(-d $options{directory}) {
        print STDERR "No directory found: $options{directory}\n";
        _help(1);
    }

    unless(-f $options{testers}) {
        print STDERR "No database found: $options{testers}\n";
        _help(1);
    }
}

sub _help {
    my $full = shift;

    if($full) {
        print <<HERE;

Usage: $0 \\
         [-d directory] [-t database] [-h] [-V]

  -d directory  directory location of website files
  -t database   local database file
  -h            this help screen
  -V            program version

HERE

    }

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


__END__

=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: http://rt.cpan.org/Public/Dist/Display.html?Name=CPAN-WWW-Testers

=head1 SEE ALSO

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

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

=head1 AUTHOR

  Barbie <barbie@cpan.org>

=head1 COPYRIGHT AND LICENSE

  Copyright (C) 2008      Barbie <barbie@cpan.org>

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

=cut
