#!/home/perl5262/bin/perl -w

# $Id$

use strict;
use warnings;

my $REPORTS_DIR     = $ENV{'HOME'} . '/.cpanreporter';  # directory where reports are stored

$REPORTS_DIR = $ENV{'HOME'} . '/cpan/reports' if -d $ENV{'HOME'} . '/cpan/reports';

my $SEND_TIMEOUT        = 0;            # seconds between submitting reports
my $SLEEP               = 3600;         # seconds between rescans when no reports left to send

my $RESCAN_AFTER        = 1000;         # rescan reports directory after sending that much reports
my $SUBMISSION_TIME     = 2.5;          # average time in seconds for submission
my $INT_ERROR_TIMEOUT   = 5;            # increasing interval between "Internal Server Error" submission

my $METABASE_URL = 'http://metabase.cpantesters.org/api/v1/';
my $METABASE_ID  = $ENV{'HOME'} . '/.cpanreporter/stro.json';

use Test::Reporter 1.57;
use Test::Reporter::Transport::Metabase 1.999008;
use Metabase::Client::Simple 0.008;
use Metabase::Fact 0.016;

use File::Spec;

$| = 1;

# Main loop

my $done;

my $sent_after_rescan = 0;

my $start_time = time;
my $sent_files = 0;
my $avg_report_time = $SUBMISSION_TIME;
my $error_timeout = 0;

RESCAN:

while (not $done) {
    # Read directory, get file list

    opendir(my $DIR, $REPORTS_DIR) or die 'Cannot read directory: ', $!;
    my @files = grep { /\.rpt$/ } readdir $DIR;
    closedir $DIR;

#    @files = grep { not /^pass/ } @files;

    if (scalar @files) {
        my $j = 0;
        print $/, 'Found ', scalar @files, ' files, estimated completion time is ', 
            (scalar localtime time + scalar @files * ($SEND_TIMEOUT + $SUBMISSION_TIME)) .
            ', ';
        foreach my $file (@files) {
            $file = File::Spec->catfile($REPORTS_DIR, $file);

            $j++;

            my $tr = Test::Reporter->new(
                'transport' => 'Metabase',
                'transport_args' => [
                    'uri'     => $METABASE_URL,
                    'id_file' => $METABASE_ID,

                ],
            )->read( $file );

            my $res = $tr->send();

            if (my $err = $tr->errstr()) {
                print $/, $file, $/ x 2, $err, $/;
                if ($err =~ /Internal\sServer\sError/) {
                    $error_timeout += $INT_ERROR_TIMEOUT;
                    sleep $error_timeout;
                }
                next;

            } else {
                print '.';
                $error_timeout = 0;
            }

            unlink $file or die 'Cannot unlink the file, something wrong happened';

            sleep $SEND_TIMEOUT;
            $sent_after_rescan++;
            $sent_files++;

            if ($sent_after_rescan == $RESCAN_AFTER) {
                $sent_after_rescan = 0;
                $avg_report_time = (time - $start_time) / $sent_files;
                print $/, 'Average report sending time = ', $avg_report_time, 's';
                next RESCAN;
            }
        }

        print $/;
        next RESCAN;

    } else {
        print 'No reports to send, sleeping until ', scalar localtime time + $SLEEP, $/;

        sleep $SLEEP;

        # Reset average submission time parameters
        $sent_files = 0;
        $start_time = time;
    }
}

print $/;
