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

our $VERSION = '0.5.0'; # VERSION

# NOTE: this script is just a prototype!  please use the
# wubot-monitor, wubot-reactor, and wubot-check scripts instead.

use AnyEvent;
use FindBin;
use Getopt::Euclid;
use Log::Log4perl;
use Sys::Hostname;
use YAML::XS;

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

use App::Wubot::LocalMessageStore;
use App::Wubot::Logger;
use App::Wubot::SQLite;
use App::Wubot::Check;
use App::Wubot::WubotX;
use App::Wubot::Reactor;

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

# load plugin library paths
my $wubotx = App::Wubot::WubotX->new();
$wubotx->get_plugins();

my $hostname = hostname();
$hostname =~ s|\..*$||;
my $reactor_config = $ARGV{-react} || "$ENV{HOME}/wubot/config/reactor.yaml.$hostname";
$logger->warn( "loading reactor config: $reactor_config" );
my $rconfig = YAML::XS::LoadFile( $reactor_config );
if ( $rconfig->{rules} ) {
    $rconfig = $rconfig->{rules};
}
elsif ( $rconfig->{react} ) {
    $rconfig = $rconfig->{react};
}

my $reactor = App::Wubot::Reactor->new();

unless ( $ARGV{-plugin} && $ARGV{-key} ) {
    die "ERROR: you must specify both a plugin and a key name!"
}

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

my $glob = "$ENV{HOME}/wubot/config/plugins/$ARGV{-plugin}/$ARGV{-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::XS::LoadFile( $config_file );
$config->{nofork} = 1;
$logger->info( "CONFIG: ", YAML::XS::Dump $config );

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

my $messenger;
unless ( $ARGV{-react} ) {
    $messenger = App::Wubot::LocalMessageStore->new( { novacuum => 1 } );
}

my $check_h = { key           => "$ARGV{-plugin}-$ARGV{-key}",
                class         => "App::Wubot::Plugin::$ARGV{-plugin}",
                reactor_queue => $messenger,
                cache_file    => $cache_file,
            };

my $check = App::Wubot::Check->new( $check_h );

my $j = AnyEvent->condvar;

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

$logger->info( "Checking" );

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

    my $results = $check->check( $config );

    $reactor->react( $results, $rconfig );

    print "RESULTS\n";
    print YAML::XS::Dump $results;

    1;
} or do {                       # catch
    print "ERROR RUNNING CHECK: $@\n";
    exit;
};

if ( $ARGV{-loop} ) {
    $j->wait;
}




__END__

=head1 NAME

wubot - unified wubot command line interface - under construction

=head1 VERSION

version 0.5.0

=head1 USAGE

  wubot

=head1 OPTIONS

=over

=item  -p[lugin] <plugin>

Specify plugin to load.

=item  -k[ey] <key>

Specify instance of plugin to load.

=item -r[eact] <path>

Specify reactor config file name and/or path.

=item -l[oop]

Start the AnyEvent loop.  This is useful for checks that spawn their
own AnyEvent timers and such.

=back
