#!/usr/bin/perl

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

use English qw(-no_match_vars) ;
use Getopt::Long;
use Pod::Usage;

use Armadito::Agent;
use Armadito::Agent::Task;
use Armadito::Agent::Task::State;
use Armadito::Agent::Task::State::Armadito;
use Armadito::Agent::Task::PullRequest;
use Armadito::Agent::Task::PullRequest::Armadito;
use Armadito::Agent::Task::Enrolment;
use Armadito::Agent::Task::Enrolment::Armadito;

my $options = {};

GetOptions(
    $options,
    'server|s=s',
    'task|t=s',
    'antivirus|av=s',
    'list-tasks',
    'list-avs',
    'help|h',
    'version|v',
) or pod2usage(-verbose => 0);

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

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

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


my $agent = Armadito::Agent->new(%setup);

if ($options->{'list-tasks'}) {
   $agent->displaySupportedTasks();
   exit 0;
}

if ($options->{'list-avs'}) {
   $agent->displaySupportedAVs();
   exit 0;
}

## default
$options->{antivirus} = "Armadito" unless(defined($options->{antivirus}));
$options->{server} = "http://armadito-glpi/glpi/plugins/armadito/index.php" unless(defined($options->{server}));

my $config = {
    plugin_server_url => $options->{server},
    av_server_url => "http://localhost/armaditoAV:8081"
};

eval{
	# validate args
	$agent->isAVSupported($options->{antivirus}) or die "Unsupported Antivirus. Use --list-avs to see which antiviruses are supported.";
	$agent->isTaskSupported($options->{task}) or die "Unsupported Task. Use --list-tasks to see which tasks are supported.";

	## init
	$agent->init(options => $options);

	## new task instance
	my $task = "Armadito::Agent::Task::$options->{task}::$options->{antivirus}"->new(config => $config);

	## run task
	$task->run();
};

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

exit(0);
__END__

=head1 NAME

armadito-agent - command line interface script used for Armadito Agent.

=head1 SYNOPSIS

armadito-agent --server <server> --task <task>

  Options:
    --help                 this menu

  Target definition options:
    --server server        Armadito Plugin for GLPI server URL

  Task selection options:
    --task task            Task to be executed
	--list-tasks           List supported tasks

  Antivirus selection options:
    --antivirus antivirus  Antivirus to be managed
	--list-avs             List supported antiviruses

=head1 EXAMPLES

    % armadito-agent --server http://armadito-glpi/glpi/ --task "Enrolment"
    % armadito-agent --server http://armadito-glpi/glpi/ --task "State"
    % armadito-agent --server http://armadito-glpi/glpi/ --task "PullRequest"

=head1 DESCRIPTION

F<armadito-agent> is the command line interface for Armadito Agent.

=head1 OPTIONS

Some options are available in a I<short> form and a I<long> form.  For
example, the two lines below are all equivalent:

    % armadito-agent -s localhost
    % armadito-agent --server localhost

=head2 Target definition options

=over

=item B<-s>, B<--server>=I<URI>

Send the results of tasks execution to given server.

In general, Armadito plugin URLs are like the following:
    http://servername/glpi/plugins/armadito/index.php

=back

=head2 Task selection options

=over

=item B<--list-tasks>

List all available tasks and exit

=item B<--task>=I<TASK>

Run given task immediately.

See option I<--list-tasks> for the list of available tasks.

=back

=head2 Antivirus selection options

=over

=item B<--list-avs>

List all available antiviruses and exit

=item B<--antivirus>=I<ANTIVIRUS>

Mange the given Antivirus

See option I<--list-avs> for the list of available antiviruses.

