#!/usr/bin/perl -w

use strict;
use Getopt::Long;
use File::Spec;
use File::Basename;
use Module::Load;
use Cwd qw(realpath);

my $debug = 0;
my %config = (
    mode => 'download',
    getter => 'filegetter',
);
my $success = 0;
my $fail = 0;
my $notest = 0;

push(@INC, dirname(realpath($0)));
load 'libvideosite';

unless(libvideosite::register_api({
    _debug => sub { return $debug },
})) {
    die("Error registering API: $libvideosite::error");
}

unless(libvideosite::init()) {
    die("Could not init libvideosite: $libvideosite::error");
}

select(STDOUT);
$| = 1;
printf("Doing self tests:\n");
foreach my $g (libvideosite::_grabbers()) {
    my $r;

    if (@ARGV) {
        my $found;

        # If there are grabber names given on the command line check
        # the current name against that list and skip if not present
        $found = grep { $_ eq $g->{'NAME'} } @ARGV;

        if ($found == 0) {
            next;
        }
    }

    printf("  %s...", $g->{'NAME'});
    $r = $g->_selftest();
    if(defined($r)) {
        if ($r == 1) {
            printf(" OK\n");
            $success++;
        } else {
            printf(" no self test\n");
            $notest++;
        }
    } else {
        $fail++;
    }
}

printf("\n\n%d succeeded\n%d failed\n%d not testable\n", $success, $fail, $notest);
