#!/usr/bin/perl -w
# This script (sophomorix-test) is maintained by Rüdiger Beck
# It is Free Software (License GPLv3)
# If you find errors, contact the author
# jeffbeck@web.de  or  jeffbeck@linuxmuster.net

# modules
use strict;
#use Quota;
use Getopt::Long;
Getopt::Long::Configure ("bundling");
use Sophomorix::SophomorixConfig;
#use IMAP::Admin;
#use DBI;
use Net::LDAP;
#use Test::More "no_plan";
use Config::IniFiles;
use Data::Dumper;
$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Useqq = 1;
$Data::Dumper::Terse = 1; 

# for smb://
use POSIX;
use Filesys::SmbClient;

use Sophomorix::SophomorixBase qw(
                                 print_line
                                 print_title
                                 NTACL_set_file
                                 unlock_sophomorix
                                 json_dump
                                 lock_sophomorix
                                 log_script_start
                                 log_script_end
                                 log_script_exit
                                 backup_auk_file
                                 get_passwd_charlist
                                 filelist_fetch
                                 get_plain_password
                                 check_options
                                 config_sophomorix_read
                                 result_sophomorix_init
                                 );
use Sophomorix::SophomorixTest qw(
                                 file_test_lines
                                 );
use Sophomorix::SophomorixSambaAD qw(
                                 AD_school_create
                                 AD_bind_admin
                                 AD_get_user
                                 AD_get_user_return_hash
                                 AD_user_kill
                                 AD_remove_sam_from_sophomorix_attributes
                                 AD_dns_get
                                 AD_unbind_admin
                                 AD_user_create
                                 AD_object_search
                                 AD_object_move
                                 AD_group_create
                                 AD_group_addmember
                                 AD_get_name_tokened
                                 AD_login_test
                                 AD_repdir_using_file
                                 AD_get_schoolname
                                 AD_dns_zonecreate
                                    );

$Conf::log_level=1;
my $help=0;


# OK:
# sophomorix-minitest -l 22
# sophomorix-minitest --length 22 -w 30 --info
# 
my %opt = ();
my $testopt=GetOptions (\%opt, 'length|l=i', 
                               'width|w=i',
                               'info|i',
                       );    


print Dumper (\%opt);

my $json=0;
my @arguments = @ARGV;



# Parsen der Optionen
my $testopt2=GetOptions(
           "verbose|v+" => \$Conf::log_level,
           "help|h" => \$help
          );

my %sophomorix_result=&result_sophomorix_init("sophomorix-school");

# Reading Configuration
my ($ldap,$root_dse) = &AD_bind_admin();
my $root_dns=&AD_dns_get($root_dse);
my %sophomorix_config=&config_sophomorix_read($ldap,$root_dse,\%sophomorix_result);
my @filelist=&filelist_fetch({filetype=>"users",
                              sophomorix_config=>\%sophomorix_config,
                            });



############################################################
# BEGIN: Test code goes here
############################################################
use Parallel::ForkManager;


my $pm = Parallel::ForkManager->new(2);
my %common_hash=();
my $ref_common_hash=\%common_hash;
$ref_common_hash->{'START'}="start";
#my $ref_common_hash;

# data structure retrieval and handling
$pm -> run_on_finish ( # called BEFORE the first call to start()
  sub {
    my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data_structure_reference) = @_;

    if (not defined $ident){
	$ident="HERE";
    }
#    # retrieve data structure from child
#    if (defined($data_structure_reference)) {  # children are not forced to send anything
    #my $string = ${$data_structure_reference};  # child passed a string reference
    print "\n";
      print "PID:    $pid\n";
      print "EXIT:   $exit_code\n";
      print "IDENT:  $ident\n";
      print "SIG:    $exit_signal\n";
      print "CORE:   $core_dump\n";
    #print "REF:    ${$data_structure_reference}\n";
    print Dumper ($data_structure_reference);

    $ref_common_hash->{$data_structure_reference->{'USER'}}=$data_structure_reference;
    #$ref_common_hash->{'KEY'}=$data_structure_reference;
     #$ref_common_hash= ($ref_common_hash, $data_structure_reference);
#    }
#    else {  # problems occurring during storage or retrieval will throw a warning
#      print qq|No message received from child process $pid!\n|;
#    }
  }
);
 

my @users=("wu42","oe42","blackmri42");



# collect data
my $ref_user;
foreach my $user (@users) {
    $ref_user = &Sophomorix::SophomorixSambaAD::AD_get_user_return_hash({
	ldap=>$ldap,
        root_dse=>$root_dse,
        root_dns=>$root_dns,
	user=>$user,
	hash_ref=>$ref_user,
        });
}

print Dumper ($ref_user);

# run the parallel processes
PERSONS:
foreach my $user (@users) {
    my $pid = $pm->start() and next PERSONS;

    my %child_return=();

    $child_return{'FIRSTNAME'}=$ref_user->{$user}{'givenName'};
    $child_return{'LASTNAME'}=$ref_user->{$user}{'sn'};
    $child_return{'USER'}=$user;
   
    # send it back to the parent process
    $pm->finish(0, \%child_return);  # note that it's a scalar REFERENCE, not the scalar itself
}
$pm->wait_all_children;



print "HERE\n";
print Dumper($ref_common_hash);
print "HERE\n";


############################################################
# END: Test code goes here
############################################################

&AD_unbind_admin($ldap);

