#!/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{
Updates a NotifRegex. ID is required field.
--------------------------------------
Available options:
 --NotifRegexId       | -i id of the NotifRegex
 --regex              | -r regex
 --note               | -n note
 --batch              | -b batch
 --help               | -h prints this help
 
};
}

our $batch;
my ($id, $regex, $note);
GetOptions ("help|h" => sub { print help(); exit 0;} ,"batch|b" => \$batch,
 "NotifRegexId|i=i" => \$id , "regex|r=s" => \$regex,"note|n=s" => \$note) || die help(); 

# Check option
unless (defined($id)) {die "ERROR: NotifRegex: id is required\n"}

my $agent = Perun::Agent->new();
my $notifAgent = $agent->getNotificationsAgent;

my $object = $notifAgent->getPerunNotifRegexById(id => $id);

if (defined($regex)) {
  unless ($regex !~ /^\s*$/) { die "ERROR: regex cannot be empty string\n";}
  $object->setRegex($regex);
}

if (defined($note)) {
  $object->setNote($note);
}

$object = $notifAgent->updatePerunNotifRegex(regex => $object);

printMessage("NotifRegex Id:".$id." successfully updated", $batch);
