#!/usr/bin/perl -w

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{
	List all user external sources. User id is required.
	-------------------------------------------------
	Available options:
	--userId      | -u user id
	--orderById   | -i order by user external source id
	--batch       | -b batch
	--help        | -h help

	};
}

my ($userId, $sortingFunction, $batch);
GetOptions ("help|h" => sub { print help(); exit 0;}, "batch|b" => \$batch,
"userId|u=i" => \$userId, "orderById|i" => sub { $sortingFunction = getSortingFunction('getId') } ) || die help();

unless(defined $sortingFunction) { $sortingFunction = getSortingFunction("getId", 1); }

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

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

my @extSources = $usersAgent->getUserExtSources(user =>$userId);

unless(@extSources) { printMessage "No user extrnal sources found", $batch;  exit 0; }

printTable $sortingFunction, @extSources;
