###
### Script pro generovani novych uzivatelu na mailboxu s WSDL souboru
###


#!/usr/bin/perl -w

use strict;
use warnings;
use Data::Dumper;
use Config::Simple;
use Log::Log4perl qw(get_logger :levels);
use XML::LibXML;

# metody : getAliases getOrgMailboxes getGroups
# Logování debug info warn error fatal

# Konfigurační soubor
my $config = new Config::Simple('cyrus_tools.conf');
# Logování
Log::Log4perl::init('cyrus_tools.conf');
# stupen logování
# Log::Log4perl->easy_init($INFO);
# instance loggeru
my $logger = Log::Log4perl->get_logger();

# delete and create directory for output files if not exists tmp_dir and tmp_dir_groups
my $tmp_dir = $config->param('tmp_dir');
if (-d $tmp_dir) {
    system("rm -rf $tmp_dir");
    $logger->info("Directory $tmp_dir deleted");
}
if (!-d $tmp_dir) {
    mkdir $tmp_dir or die "Failed to create directory: $tmp_dir";
    $logger->info("Directory $tmp_dir created");
}




### MAIN ###

my $getaliases = getData('getAliases');
parse_xml_aliases($getaliases);

my $getOrgMailboxes = getData('getOrgMailboxes');
parse_xml_orgMailboxes($getOrgMailboxes);





### SUBS ###

sub cut_domain {
    my $email = shift;
    # cut domain from email
    $email =~ s/@.*//;
    return $email;
}



############################################

sub parse_xml_aliases {
my $data = shift;
# print Dumper($data);
my $parser = XML::LibXML->new();
my $doc = $parser->parse_string($data);
my $root = $doc->getDocumentElement;
my @account = $root->findnodes('//account');

# all_users_access_vsb_cz
my $filename3 = $tmp_dir . '/' . "all_users_access_vsb_cz";
open(my $fh3, '>>:encoding(UTF-8)', $filename3) or die "Could not open file '$filename3' $!";
foreach my $mailbox (@account) {
    my $rescp_string = "";
    my $login = $mailbox->findvalue('login');

    print $fh3 $login."\n";
}
close $fh3;
$logger->info("File $filename3 appended");
}   

############################################

sub parse_xml_orgMailboxes {
my $data = shift;
my $parser = XML::LibXML->new();
my $doc = $parser->parse_string($data);
my $root = $doc->getDocumentElement;
my @orgmailbox = $root->findnodes('//orgMailbox');

# all_users_access_vsb_cz
my $filename3 = $tmp_dir . '/' . "all_users_access_vsb_cz";
open(my $fh, '>>:encoding(UTF-8)', $filename3) or die "Could not open file '$filename3' $!";
foreach my $mailbox (@orgmailbox) {
    my $login = $mailbox->findvalue('login');

    print $fh $login ."\n";
}
close $fh;
$logger->info("File $filename3 appended");
}    

############################################

sub getData {
    my $myget = shift;
    my $soapaction = $config->param('soapaction');
    my $endpoint = $config->param('endpoint');
    my $namespace = $config->param('namespace');
    my $user = $config->param('username');
    my $pass = $config->param('password');

    my $command = "curl -s -X POST -H \"Content-Type: text/xml\" -H \"SOAPAction: $namespace/$myget\" -d ' <soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"$namespace\"> <soapenv:Header/> <soapenv:Body> <web:$myget/> </soapenv:Body> </soapenv:Envelope> ' -u $user:$pass $endpoint";
    my $response = `$command`;
    # check  if curl command was successful
    if ($? != 0) {
        print "Error: $response\n";
        $logger->error("Error: $response");
        exit 1;
    }
    return $response;

}
