#!/usr/local/bin/perl

use v5.10.1;
use strict;
use warnings FATAL => 'all';
use experimental qw(smartmatch);

use Sport::Analytics::NHL::LocalConfig;
use Sport::Analytics::NHL;
use Sport::Analytics::NHL::Usage;

my @accepts_opts = qw(
	:compile :merge :normalize :database
	data-dir force
	:season test
	no-player-crawl no-injury-crawl
);

my @accepts_args = (
	{
		name        => 'GAME_IDS',
		description => 'Our 9-digit game ids that are present in the file system',
	}
);

my $what_it_does = 'Populates the normalized NHL reports referenced in a stored schedule into a Mongo database.';
my $opts = gopts($what_it_does, \@accepts_opts, \@accepts_args);
my $hockeydb = Sport::Analytics::NHL->new($opts);
my $query = {};
if ($opts->{start_season} || $opts->{stop_season}) {
	$query->{season} = {
		$opts->{start_season} ? ('$gte' => $opts->{start_season}) : (),
		$opts->{stop_season}  ? ('$lte' => $opts->{stop_season})  : (),
	}
}
my @game_ids = $hockeydb->{db}{dbh}->get_collection('schedule')->find(
	$query, {_id => 0, game_id => 1}
)->all();
$hockeydb->populate($opts, map($_->{game_id},@game_ids));

=head1 NAME

populate - populate (recompile if necessary) the normalized NHL boxscores referenced in schedule into a DB.

=head1 SYNOPSIS

Populates the normalized NHL reports referenced in a stored schedule into a Mongo database.

 Usage: scripts/populate-schedule [Options] Arguments
 Options:
	-h|--help                       print this message and exit
	-V|--version                    print version and exit
	-v|--verbose                    produce verbose output to STDERR
	-d|--debug                      produce debug output to STDERR
	--no-compile                    Do not compile file even if storable is absent
	--recompile                     Compile file even if storable is present
	--no-merge                      Do not merge file even if storable is absent
	--remerge                       Compile file even if storable is present
	--no-normalize                  Do not normalize file even if storable is absent
	--renormalize                   Compile file even if storable is present
	--no-database                   Do not use a MongoDB backend
	-D|--database        DB         Use Mongo database DB (default hockeytest)
	-E|--data-dir        DIR        Data directory root (default /misc/nhl)
	-f|--force                      override/overwrite existing data
	-s|--start-season    SEASON     Start at season SEASON (default 2018)
	-S|--stop-season     SEASON     Stop at season SEASON (default 2018)
	-T|--stage           STAGE      Scrape stage STAGE (2: REGULAR, 3: PLAYOFF, default: 2
	--test                          Test the validity of the files (use with caution)

=cut

=head1 AUTHOR

More Hockey Stats, C<< <contact at morehockeystats.com> >>

=head1 BUGS

Please report any bugs or feature requests to C<contact at morehockeystats.com>, or through
the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Sport::Analytics::NHL>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.




=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Sport::Analytics::NHL


You can also look for information at:

=over 4

=item * RT: CPAN's request tracker (report bugs here)

L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Sport::Analytics::NHL>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/Sport::Analytics::NHL>

=item * CPAN Ratings

L<https://cpanratings.perl.org/d/Sport::Analytics::NHL>

=item * Search CPAN

L<https://metacpan.org/release/Sport::Analytics::NHL>

=back


=head1 ACKNOWLEDGEMENTS


=head1 LICENSE AND COPYRIGHT

Copyright 2018 More Hockey Stats.

This program is released under the following license: gnu


=cut

1;

