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

my $VERSION = '0.48';

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

=head1 NAME

cpanstats-writepages - script to create the CPAN Testers Statistics website.

=head1 SYNOPSIS

  perl cpanstats-writepages

=head1 DESCRIPTION

Using the cpanstats database, which should in the local directory, extracts
all the data into the components of each page. Then creates each HTML page for
the site.

=cut

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

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

use Getopt::ArgvFile default=>1;
use Getopt::Long;

use CPAN::Testers::WWW::Statistics::Pages;

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

use constant DATABASE => './cpanstats.db';
use constant ADDRESS  => 'data/addresses.txt';

my (%options);

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

init_options();

my $pages = CPAN::Testers::WWW::Statistics::Pages->new(
        directory => $options{directory},
        templates => $options{templates},
        database  => $options{database},
        address   => $options{address},
        progress  => $options{progress}
);
$pages->create();

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

=head1 FUNCTIONS

=over 4

=item init_options

Prepare command line options

=cut

sub init_options {
    GetOptions( \%options,
        'templates=s',
        'directory=s',
        'database|d=s',
        'address|a=s',
        'progress|p',
        'help|h',
        'version|V'
    );

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

    # use defaults if none provided
    $options{database} ||= DATABASE;
    $options{address}  ||= ADDRESS;

    if($options{database} && ! -f $options{database}) {
        print "Given database file not a valid file, see help below.\n";
        _help(1);
    }

    if($options{address} && ! -f $options{address}) {
        print "Given address data file not a valid file, see help below.\n";
        _help(1);
    }
}

sub _help {
    my $full = shift;

    if($full) {
        print "\n";
        print "Usage:$0 [--help|h] [--version|V] \\\n";
        print "         [--templates=<dir>]   \\\n";
        print "         [--directory=<dir>]   \\\n";
        print "         [--database|d=<file>] \\\n";
        print "         [--address|a=<file>]  \\\n";
        print "         [--progress|p]  \n\n";

#              12345678901234567890123456789012345678901234567890123456789012345678901234567890
        print "This program builds the CPAN Testers Statistics website.\n";

        print "\nFunctional Options:\n";
        print "  [--templates=<dir>]        # path to templates\n";
        print "  [--directory=<dir>]        # path to website directory\n";
        print "  [--database=<file>]        # path/file to database\n";
        print "  [--address=<file>]         # path/file to address file\n";
        print "  [--progress]               # enable progress messages\n";

        print "\nOther Options:\n";
        print "  [--version]                # program version\n";
        print "  [--help]                   # this screen\n";

        print "\nFor further information type 'perldoc $0'\n";
    }

    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-WWW-Statistics

=head1 SEE ALSO

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

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

=head1 AUTHOR

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

=head1 COPYRIGHT AND LICENSE

  Copyright (C) 2005-2008 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

