#!/usr/bin/perl

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

sub help {
	return qq{
	Gets attribute determining number of cores on Host. Host name is required field.
	---------------------------------------------------
	Available options:
	--hostName    | -H  host name
	--batch       | -b  batch
	--help        | -h  prints this help
	};
}

my ($hostName, $batch);
GetOptions("help|h" => sub {
		print help;
		exit 0;
	},
	"hostName|H=s"  => \$hostName,
	"batch|b"       => \$batch) || die help;

#options check
unless (defined $hostName) { die "ERROR: hostName is required\n";}

my $attrName = "urn:perun:host:attribute-def:def:coresNumber";

my $agent = Perun::Agent->new();

my $attributesAgent = $agent->getAttributesAgent;
my $facilitiesAgent = $agent->getFacilitiesAgent;

my @hosts = $facilitiesAgent->getHostsByHostname( hostname => $hostName );
my @rows = ();
while (@hosts) {
	my $host = shift(@hosts);
	my $hostId = $host->getId;
	my $attribute = $attributesAgent->getAttribute( attributeName => $attrName, host => $hostId );
	my $numCores;
	$numCores = $attribute->getValueAsScalar;
	unless (defined $numCores) { $numCores = "undefined";}

	my $facility = $facilitiesAgent->getFacilityForHost( host => $hostId );
	my $facilityName;
	unless ($facility) {$facilityName = " ";}
	$facilityName = $facility->getName;

	my @row = ($hostId, $hostName, $numCores, $facilityName);
	push(@rows, \@row);
}

my @columnsNames = ('Id', 'Host name', 'Number of cores', 'Facility name');
print tableContentToPrint(\@columnsNames, \@rows, $batch);
