#!/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{
	Removes all attributes from the UserExtSource attribute. UserExtSource id or (external source Name or Id and userExtSourceLogin) required.
	-------------------------------------------------
	Available options:
	--userExtSourceId    | -u userExtSource Id
	--userExtSourceLogin | -l users login at ExtSource
	--extSourceName      | -E extSource name
	--extSourceId        | -e extSource Id
	--batch              | -b batch
	--help               | -h prints this help

};
}

my ($userExtSourceId, $userExtSourceLogin, $extSourceId, $extSourceName, $batch);
GetOptions ("help|h"   => sub {
		print help();
		exit 0;
	},
	"batch|b"       => \$batch,
	"userExtSourceId|u=i"   => \$userExtSourceId,
	"userExtSourceLogin|l=s" => \$userExtSourceLogin,
	"extSourceName|E=s" => \$extSourceName,
	"extSourceId|e=i" => \$extSourceId) || die help();

# Check options
unless (defined $userExtSourceId ) {
	unless (defined $extSourceName or defined $extSourceId) { die "ERROR: extSourceId  or extSourceName is required \n";}
	unless (defined $userExtSourceLogin) { die "ERROR: userExtSourceLogin is required \n";}
}

my $agent = Perun::Agent->new();
my $usersAgent = $agent->getUsersAgent;
my $extSourcesAgent = $agent->getExtSourcesAgent;

unless ($userExtSourceId) {
	my $extSource = $extSourcesAgent->getExtSourceByName( name => $extSourceName) if $extSourceName;
	$extSource = $extSourcesAgent->getExtSourceById( id => $extSourceId) if $extSourceId;

	my $userExtSource = $usersAgent->getUserExtSourceByExtLogin( extSourceLogin => $userExtSourceLogin, extSource => $extSource );
	$userExtSourceId = $userExtSource->getId;
}

my $attributesAgent = $agent->getAttributesAgent;

$attributesAgent->removeAllAttributes( userExtSource => $userExtSourceId );

printMessage("All attributes removed from the userExtSourceId Id:$userExtSourceId", $batch);
