#!/usr/local/bin/perl -w
use strict;
use warnings;

our $VERSION = '0.3.4'; # VERSION

use AnyEvent;
use FindBin;
use Log::Log4perl;
use Sys::Hostname;

use lib "$FindBin::Bin/../lib";

use App::Wubot::Logger;
use App::Wubot::SQLite;
use App::Wubot::Check;

my $logger = Log::Log4perl::get_logger( 'default' );

my ( $plugin, $key ) = @ARGV;
unless ( $plugin && $key ) {
    die "ERROR: you must specify both a plugin and a key name!"
}

$logger->warn( "PLUGIN: $plugin" );
$logger->warn( "KEY:    $key" );

my $glob = "$ENV{HOME}/wubot/config/plugins/$plugin/$key.yaml*";
my ( $config_file ) = glob( $glob );

unless ( $config_file ) {
    die "ERROR: config file not found: $glob";
}

unless ( -r $config_file ) {
    $logger->logdie( "ERROR: config file not found: $config_file" );
}
my $config = YAML::LoadFile( $config_file );
$config->{nofork} = 1;
$logger->info( "CONFIG: ", YAML::Dump $config );

my $cache_file = "$ENV{HOME}/wubot/cache/$plugin-$key.yaml";
if ( -r $cache_file ) {
    $logger->info( "Found cache file: $cache_file" );
}

my $check = App::Wubot::Check->new( { key        => "$plugin-$key",
                                 class      => "App::Wubot::Plugin::$plugin",
                                 cache_file => $cache_file,
                             } );

my $j = AnyEvent->condvar;

$logger->info( "Initializing" );
print YAML::Dump $check->init( $config );

$logger->info( "Checking" );

eval {                          # try
    print YAML::Dump $config;

    my $results = $check->check( $config );
    print YAML::Dump $results;
    1;
} or do {                       # catch
    print "ERROR RUNNING CHECK: $@\n";
    exit;
};


$j->wait;

__END__

=head1 NAME

 wubot-check - perform a single check of an instance of a plugin

=head1 VERSION

version 0.3.4

=head1 SYNOPSIS

  wubot-check Plugin id

  # example
  wubot-check RSS slashdot

=head1 DESCRIPTION

This script makes it easy to perform a single check of an instance of
a monitor.

See also: L<App::Wubot::Guide::Debugging>
