#!/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{
	Updates resource. Resource id is required.
	--------------------------------------
	Available options:
	--resourceId	| -r resource id
	--name        | -n new name for the resource
	--dsc         | -d new description for the resource
	--batch       | -b batch
	--help        | -h prints this help

	};
}

our $batch;
my ($resourceId, $name, $dsc);
GetOptions ("help|h" => sub {
		print help();
		exit 0;
	},
	"batch|b"        => \$batch,
	"resourceId|r=i" => \$resourceId,
	"dsc|d=s"        => \$dsc,
	"name|n=s"       => \$name ) || die help();

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

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

my $resource = $resourcesAgent->getResourceById( id => $resourceId );

if (defined($name)) {
	unless ($name !~ /^\s*$/) { die "ERROR: name cannot be empty string\n";}
	$resource->setName( $name );
}
if (defined($dsc)) {
	$resource->setDescription( $dsc );
}

$resource = $resourcesAgent->updateResource( resource => $resource );

printMessage("Resource id:$resourceId successfully updated", $batch);
