#!/usr/bin/perl

use strict;
use warnings;
use perunServicesInit;
use perunServicesUtils;
use File::Basename;

local $::SERVICE_NAME = basename($0);
local $::PROTOCOL_VERSION = "3.0.0";
my $SCRIPT_VERSION = "3.1.2";

perunServicesInit::init;
my $data = perunServicesInit::getHashedDataWithGroups;


our $A_PRINCIPAL;            *A_PRINCIPAL =           \'urn:perun:user:attribute-def:def:kerberosAdminPrincipal';
our $A_USER_STATUS;          *A_USER_STATUS =         \'urn:perun:member:attribute-def:core:status';
our $A_GROUP_DESTINATIONS;   *A_GROUP_DESTINATIONS =  \'urn:perun:group:attribute-def:def:listOfDestinations';

my %outputByDestination = ('all' => undef);

foreach my $resourceId ($data->getResourceIds()) {

	foreach my $groupId ($data->getGroupIdsForResource( resource => $resourceId )) {
		my @generatedDataDestination = ('all');
		if(defined $data->getGroupAttributeValue(group => $groupId, attrName => $A_GROUP_DESTINATIONS )) {
			@generatedDataDestination = @{$data->getGroupAttributeValue(group => $groupId, attrName => $A_GROUP_DESTINATIONS )};
		}

		foreach my $memberId ($data->getMemberIdsForResourceAndGroup(resource => $resourceId, group => $groupId )) {
			next if $data->getMemberAttributeValue( member => $memberId, attrName => $A_USER_STATUS ) ne 'VALID';
			push @{$outputByDestination{$_}}, $data->getUserAttributeValue( member => $memberId, attrName => $A_PRINCIPAL ) foreach @generatedDataDestination;
		}

	}
}


####### output ######################
{
	local $, = "\n";
	local $\ = "\n";

	for my $destination (keys %outputByDestination) {
		my $destinationDirectory = getDestinationDirectory $destination; 
		my $service_file_name = "$destinationDirectory/$::SERVICE_NAME";
		open SERVICE_FILE,">$service_file_name" or die "Cannot open $service_file_name: $! \n";
		print SERVICE_FILE sort &uniqList(@{$outputByDestination{$destination}}, @{$outputByDestination{"all"}});
		close(SERVICE_FILE);
	}
}

perunServicesInit::finalize;
