#!/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::State;
use Armadito::Agent::Task::PullRequest;
use Armadito::Agent::Task::Enrolment;

my $options = {};

GetOptions(
    $options,
    'server=s',
    'task=s',
    'help',
    'version',
) 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;
}

pod2usage(-verbose => 0) unless
    $options->{server} && $options->{task};

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

my $config = {
    plugin_server_url => $options->{server}."/plugins/armadito/index.php",
    av_server_url => "http://localhost/armaditoAV:8081",
    task => $options->{task}
};


eval{

	## new agent instance
	my $agent = Armadito::Agent->new(%setup);
	$agent->init(options => $options);

    ## TODO : Use switch to select tasks
	## new task instance
	my $task = "Armadito::Agent::Task::$options->{task}"->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 - script binary used to call Armadito Agent.

=head1 SYNOPSIS

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

  Options:
    --help                 this menu
    --server server        Armadito GLPI Plugin server URL
    --task task            Armadito task to be executed

=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 a command line interface for Armadito Agent.

