#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long qw(:config no_ignore_case);
use Perun::Agent;
use Perun::Common qw(printMessage);

sub help {
	return qq{
	Removes selected hosts from the facility. Facility id and one or more host id are required fields.
	------------------------------------
	Available options:
	--hosts        | -H list Of Hosts
	--facilityId   | -f facility id
	--facilityName | -F facility name
	--batch        | -b batch
	--help         | -h prints this help

	};
}

my (@hosts, $facilityId, $facilityName, $batch);
GetOptions ("help|h" => sub { print help(); exit 0;} ,
	"batch|b" => \$batch,
'hosts|H=i@{1,}' => \@hosts,
"facilityId|f=i" => \$facilityId,
"facilityName|F=s" => \$facilityName) || die help();

# Check options
unless (@hosts) { die "ERROR: hosts are required \n";}
unless(defined $facilityId or (defined $facilityName)) { die "ERROR: facilityId or facilityName is required\n";}

#Bug in Getopt::Long causes that when you read array of integer,
#only first one is integer, others are integers values but in variable with string type.
#Example: you read: 1 2 3    you get: [1, '2', '3']
#
#This convert whole array to integers. (This is required for conversion to JSON.)
@hosts = map { 0+$_ } @hosts;

my $agent = Perun::Agent->new();
my $hostsAgent = $agent->getHostsAgent;
my $facilitiesAgent = $agent->getFacilitiesAgent;

unless($facilityId) {
	my $facility = $facilitiesAgent->getFacilityByName(name => $facilityName);
	$facilityId = $facility->getId;
}

$facilitiesAgent->removeHosts(hosts => \@hosts, facility => $facilityId);

printMessage("Hosts: @hosts successfully removed from the facility Id:$facilityId", $batch);
