#!/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 group. Group id is required.
	--------------------------------------
	Available options:
	--groupId     | -g group id
	--name        | -n new name for the group
	--dsc         | -d new description for the group
	--batch       | -b batch
	--help        | -h prints this help

	};
}

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

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

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

my $group = $groupsAgent->getGroupById( id => $groupId );

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

$group = $groupsAgent->updateGroup( group => $group );

printMessage("Group id:$groupId successfully updated", $batch);
