#!/usr/bin/perl
#
# This file is part of Module-Packaged-Generator
#
# This software is copyright (c) 2010 by Jerome Quelin.
#
# This is free software; you can redistribute it and/or modify it under
# the same terms as the Perl 5 programming language system itself.
#

use 5.010;
use strict;
use warnings;

package main;
BEGIN {
  $main::VERSION = '1.111040';
}
# ABSTRACT: create db of available Perl modules

use File::Spec::Functions qw{ catdir updir };
use Term::ProgressBar::Quiet;

# for dev purposes
use FindBin qw{ $Bin };
use lib catdir( $Bin, updir, 'lib' );


use Module::Packaged::Generator;


# -- main program

# try to find a suitable driver
my $driver = Module::Packaged::Generator->find_driver;
if ( not defined $driver ) {
    my $msg = join "\n",
        "no driver found for this machine distribution.\n",
        "list of existing distribution drivers:",
        map { ( my $d = $_ ) =~ s/^.*:://; "\t$d" }
        Module::Packaged::Generator->all_drivers;
    die "$msg\n\n";
}
print "found a distribution driver: $driver\n";


# create the database
( my $dist = $driver ) =~ s/^.*:://;
my $file = "cpan_$dist.db";
my $dbh  = Module::Packaged::Generator->create_db($file);
$dbh->{AutoCommit} = 0;
$dbh->{RaiseError} = 1;


# fetch the list of available perl modules
my @modules = $driver->list;


# insert the modules in the database
my $sth = $dbh->prepare("
    INSERT
        INTO   module (module, version, dist, pkgname)
        VALUES        (?,?,?,?);
");
my $prefix = "inserting modules in db";
my $progress = Term::ProgressBar::Quiet->new( {
    count     => scalar(@modules),
    bar_width => 50,
    remove    => 1,
    name      => $prefix,
} );
$progress->minor(0);  # we're so fast now that we don't need minor scale
my $next_update = 0;
foreach my $i ( 0 .. $#modules ) {
    my $m = $modules[$i];
    $sth->execute($m->name, $m->version, $m->dist, $m->pkgname);
    $next_update = $progress->update($_)
        if $i >= $next_update;
}
$progress->update( scalar(@modules) );
$sth->finish;
print "${prefix}: done\n";


# create indexes in the db to make it faster
print "creating indexes: modules ";
$dbh->do("CREATE INDEX module__module  on module ( module  );");
print "dists ";
$dbh->do("CREATE INDEX module__dist    on module ( dist    );");
print "packages ";
$dbh->do("CREATE INDEX module__pkgname on module ( pkgname );");
print "done\n";


# all's done, close the db
$dbh->commit;
$dbh->disconnect;

exit;


=pod

=head1 NAME

main - create db of available Perl modules

=head1 VERSION

version 1.111040

=head1 SYNOPSIS

    pkgcpan

=head1 DESCRIPTION

This script will create and populate a sqlite database with the
available Perl modules for your distribution.

=head1 AUTHOR

Jerome Quelin

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2010 by Jerome Quelin.

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

=cut


__END__

