#!/usr/bin/perl

use strict;
use warnings;
use Switch;
use Getopt::Long qw(:config no_ignore_case);
use Perun::Agent;
use Perun::Common qw(printMessage);

sub help {
	return qq{
	Creates an External source. ExtSource name and type are required fields.
	--------------------------------------
	Available options:
	--extSourceName   | -e extSrouce name
	--extSourceType   | -t type (KERBEROS/IDP/INTERNAL/SQL/LDAP/ISMU/X509)
	--batch           | -b batch
	--help            | -h prints this help

	};
}

my ($extSourceName, $extSourceType, $batch);
GetOptions ("help|h" => sub { print help(); exit 0;} ,"batch|b" => \$batch,
"extSourceName|e=s" => \$extSourceName,"extSourceType|t=s" => \$extSourceType) || die help();

# Check options
unless (defined($extSourceName)) { die "ERROR: extSourceName is required \n";}
unless (defined($extSourceType)) { die "ERROR: extSourceType is required \n";}
unless ($extSourceName !~ /^\s*$/) { die "ERROR: extSourceName cannot be empty string\n";}
unless ($extSourceType !~ /^\s*$/) { die "ERROR: extSourceType cannot be empty string\n";}

# Convert type
my $extSourceTypeInternal;
switch ($extSourceType) {
	case "KERBEROS" {
		$extSourceTypeInternal = "cz.metacentrum.perun.core.impl.ExtSourceKerberos";
	}
	case "IDP" {
		$extSourceTypeInternal = "cz.metacentrum.perun.core.impl.ExtSourceIdp";
	}
	case "INTERNAL" {
		$extSourceTypeInternal = "cz.metacentrum.perun.core.impl.ExtSourceInternal";
	}
	case "LDAP" {
		$extSourceTypeInternal = "cz.metacentrum.perun.core.impl.ExtSourceLdap";
	}
	case "ISMU" {
		$extSourceTypeInternal = "cz.metacentrum.perun.core.impl.ExtSourceISMU";
	}
	case "SQL" {
		$extSourceTypeInternal = "cz.metacentrum.perun.core.impl.ExtSourceSql";
	}
	case "X509" {
		$extSourceTypeInternal = "cz.metacentrum.perun.core.impl.ExtSourceX509";
	}
}

my $agent = Perun::Agent->new();
my $extSourceAgent = $agent->getExtSourcesAgent;

my $extSource = Perun::beans::ExtSource->new;
$extSource->setName($extSourceName);
$extSource->setType($extSourceTypeInternal);

$extSource = $extSourceAgent->createExtSource(extSource => $extSource);

printMessage("ExtSource Id:".$extSource->getId." successfully created", $batch);
