#!/usr/bin/perl

use strict;
use warnings;
use lib './lib';

use English qw(-no_match_vars) ;
use Getopt::Long;
use Pod::Usage;
use UNIVERSAL::require;
use Armadito::Prelude;

my $options = {};

pod2usage(-verbose => 0, -exitstatus => 0) unless $ARGV[0];

GetOptions(
    $options,
	'input|i=s',
	'setup',
	'max=i',
	'action|a=s',
	'help|h',
	'no-rm',
	'path=s',
	'version|v',
) or pod2usage(-verbose => 0);

pod2usage(-verbose => 0, -exitstatus => 0) if $options->{help};

if ($options->{version}) {
    print "armadito-prelude $Armadito::Prelude::VERSION\n";
    exit 0;
}

my %setup = (
    confdir => './etc',
    datadir => './share',
    libdir  => './lib',
    vardir  => './var',
);

if ($options->{setup}) {
    foreach my $key (keys %setup) {
        print "$key: $setup{$key}\n";
    }
    exit 0;
}

my $prelude = Armadito::Prelude->new(%setup);
my $config = {};

eval{
	$prelude->init(options => $options);
        $prelude->run();
};

if ($EVAL_ERROR) {
    print STDERR "Execution failure:\n";
    print STDERR $EVAL_ERROR;
    exit(1);
}

exit(0);
__END__

=head1 NAME

armadito-prelude - command line interface script for Armadito Prelude.

=head1 SYNOPSIS

armadito-prelude --input <alertsdir>

  Options:
    --help                 this menu
    -i --input alertsdir   Armadito alerts directory
    -a --action action     Action ( status | scan )
    --path scan_path       Scan path (only if action is scan).
    --max   max            Maximum alerts processed (>0)
    --no-rm 		   Do not remove alerts after processing (not by default).
    --setup 		   Show setup paths

=head1 EXAMPLES

	% armadito-prelude --input /var/spool/armadito
	% armadito-prelude -i $PREFIX/install/var/spool/armadito --max 10
	% armadito-prelude --action status
	% armadito-prelude -a scan --path "/home/armadito/toscandir" 

=head1 DESCRIPTION

F<armadito-prelude> is the command line interface for Armadito Prelude.

=head1 OPTIONS

=head2 Main options

=over

=item B<-i>, B<--input>=I<ALERTSDIR>

Read alerts in given directory.

=item B<--max>=I<MAX>

Process alerts up to given maximum (>0). 

=item B<--no-rm>

Do not remove alerts after processing.
By using that option, be aware that these alerts will be reprocessed if they are still in alerts directory.

=item B<-a>, B<--action>=I<ACTION>

Executes the selected action. 

=item B<--path>=I<SCAN_PATH>

Path of directory to scan. It is used as a parameter of Armadito AV's RESTful API.

=back
