#!/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 printTable);


sub help {
	return qq{
	Prints list of group members. Group is required field. Group can by specified by it's ID or by both it's name and VO.
	---------------------------------
	Available options:
	--groupId     | -g  Group identifier
	--groupName   | -G  Group name
	--voId        | -v  VO idetifier
	--voShortName | -V  VO short name
	--orderById   | -i  order by member's identifier
	--batch       | -b  batch
	--help        | -h  prints this help

	};
}

our $batch;
my ($groupId, $groupName, $voId, $voShortName, $sortingFunction);
GetOptions("help|h"   => sub {
		print help;
		exit 0;
	},
	"groupId|g=i"     => \$groupId,
	"groupName|G=s"   => \$groupName,
	"voId|v=i"        => \$voId,
	"voShortName|V=s" => \$voShortName,
	"orderById|i"     => sub { $sortingFunction = getSortingFunction("getMemberId") },
	"batch|b"         => \$batch) || die help;

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

#options check
unless (defined $sortingFunction) { $sortingFunction = getSortingFunction("getMemberId"); }
unless (defined $groupId) {
	unless (defined $groupName) { die "ERROR: Group specification required.\n"; }
	unless (defined $voId) {
		unless (defined $voShortName) { die "ERROR: VO specification required.\n"; }
		my $vo = $agent->getVosAgent->getVoByShortName( shortName => $voShortName );
		$voId = $vo->getId;
	}
	my $group = $groupsAgent->getGroupByName( vo => $voId, name => $groupName );
	$groupId = $group->getId;
}

my @richMembers = $groupsAgent->getGroupRichMembers( group => $groupId, pageSize => 0, pageNum => 0 );
unless (@richMembers) {
	printMessage "No member found", $batch;
	exit 0;
}

#output
printTable($sortingFunction, @richMembers);
