#!/usr/bin/env perl
use strict;
use warnings;

use Getopt::Long qw(GetOptions);
use Log::Log4perl qw(:easy get_logger);
use Log::Log4perl::MDC;

my $logconf = '/usr/local/etc/badips/log4perl.conf';
my $logconf_refresh_interval = 5;

eval {
  Log::Log4perl::init_and_watch($logconf, $logconf_refresh_interval);
};
if ($@) {
  die "Failed to initialize Log::Log4perl with config '$logconf': $@\n";
}
Log::Log4perl::MDC->put("THREAD" => "main");

use lib '/usr/local/lib/site_perl';
use BadIPs;
use Config::Tiny;

my $conf_main = '/usr/local/etc/badips.conf';
my $conf_dir  = '/usr/local/etc/badips.d';

my $test_config = 0;
my $dry_run     = 0;

GetOptions(
  'test-config!' => \$test_config,
  'dry-run!'     => \$dry_run,
) or die "Usage: $0 [--test-config] [--dry-run]\n";

my $logger = get_logger("BadIPs");
$logger->info("Starting BadIPs application");

my $app = BadIPs->new(
  conf_main => $conf_main,
  conf_dir  => $conf_dir,
  dry_run   => $dry_run,
  logconf   => $logconf,
  logconf_refresh_interval => $logconf_refresh_interval,
);

if ($test_config) {
  my ($ok, $msg, $report) = $app->test_config();
  print $report;
  exit($ok ? 0 : 1);
}

$app->run();

$logger->info("BadIPs application exited normally");

exit(0);

