#!/usr/bin/perl -w
# This script (sophomorix-user) 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 List::MoreUtils qw(uniq);
#use IMAP::Admin;
#use DBI;
use Net::LDAP;
use Data::Dumper;
$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Useqq = 1;
$Data::Dumper::Terse = 1; 
use JSON;

use Sophomorix::SophomorixBase qw(
                                 print_line
                                 print_title
                                 unlock_sophomorix
                                 lock_sophomorix
                                 log_script_start
                                 log_script_end
                                 log_script_exit
                                 backup_auk_file
                                 get_passwd_charlist
                                 get_plain_password
                                 check_options
                                 config_sophomorix_read
                                 result_sophomorix_init
                                 result_sophomorix_add
                                 result_sophomorix_check_exit
                                 result_sophomorix_print
                                 remove_from_list
                                 test_webui_permission
                                 json_dump
                                 );
use Sophomorix::SophomorixSambaAD qw(
                                 AD_school_create
                                 AD_bind_admin
                                 AD_unbind_admin
                                 AD_get_user
                                 AD_user_create
                                 AD_user_update
                                 AD_group_create
                                 AD_group_kill
                                 AD_group_addmember
                                 AD_get_schoolname
                                 AD_get_name_tokened
                                 AD_group_update
                                 AD_project_sync_members
                                 AD_dn_fetch_multivalue
                                 AD_dns_get
                                 AD_get_users_v
                                 AD_get_full_userdata
                                 AD_debug_logdump
                                 AD_object_search
                                    );

my @arguments = @ARGV;

my $today=`date +%d.%m.%Y`;
chomp($today);

# ===========================================================================
# Optionen verarbeiten
# ==========================================================================
$Conf::log_level=1;

my $help=0;
my $info=0;
my $json=0;
my $empty_password=0;

my $webui_dashboard;
my $webui_permissions;
my $add_webui_permissions;
my $remove_webui_permissions;

my $comment;
my $clear_unid=0;
my $set_unid="";
my $set_proxy_addresses;
my $add_proxy_addresses;
my $remove_proxy_addresses;

my $set_single_value_attribute;
my $set_multi_value_attribute;
my $add_multi_value_attribute;
my $remove_multi_value_attribute;
my $entry;

my $custom_1;
my $custom_2;
my $custom_3;
my $custom_4;
my $custom_5;
my $custom_multi_1;
my $custom_multi_2;
my $custom_multi_3;
my $custom_multi_4;
my $custom_multi_5;
my $add_custom_multi_1;
my $add_custom_multi_2;
my $add_custom_multi_3;
my $add_custom_multi_4;
my $add_custom_multi_5;
my $remove_custom_multi_1;
my $remove_custom_multi_2;
my $remove_custom_multi_3;
my $remove_custom_multi_4;
my $remove_custom_multi_5;

my $intrinsic_1;
my $intrinsic_2;
my $intrinsic_3;
my $intrinsic_4;
my $intrinsic_5;
my $intrinsic_multi_1;
my $intrinsic_multi_2;
my $intrinsic_multi_3;
my $intrinsic_multi_4;
my $intrinsic_multi_5;
my $add_intrinsic_multi_1;
my $add_intrinsic_multi_2;
my $add_intrinsic_multi_3;
my $add_intrinsic_multi_4;
my $add_intrinsic_multi_5;
my $remove_intrinsic_multi_1;
my $remove_intrinsic_multi_2;
my $remove_intrinsic_multi_3;
my $remove_intrinsic_multi_4;
my $remove_intrinsic_multi_5;

my $quota;
my $quota_reset_students;
my $quota_reset_teachers;
my $mailquota;

# stati
my $usable=0;
my $enable=0;
my $activate=0;
my $selfactivate=0;
my $permanent=0;
my $tolerate=0;
my $deactivate=0;
my $lock=0;
my $freeze=0;
my $killable=0;
my $removable=0;

my $kill_all_users=0;
my $teacher="";
my $sc_toleration="empty";
my $reset_user="";
my $user="";
my $old_uid="";
my $new_uid="";
my $old_mailbox="";
my $new_mailbox="";
my $new_class="";
my $list_teachers_by_year=0;
my $school="";

# Parsen der Optionen
my $testopt=GetOptions(
       "help|h" => \$help,
       "i|info" => \$info,
       "json|j+" => \$json,
       "verbose|v+" => \$Conf::log_level,
       "school=s" => \$school,
       "u|user=s" => \$user,
       "comment=s" => \$comment,
       "clear-unid" => \$clear_unid,
       "set-unid=s" => \$set_unid,
       "set-single-value-attribute=s" => \$set_single_value_attribute,
       "set-multi-value-attribute=s" => \$set_multi_value_attribute,
       "add-multi-value-attribute=s" => \$add_multi_value_attribute,
       "remove-multi-value-attribute=s" => \$remove_multi_value_attribute,
       "set-proxy-addresses=s" => \$set_proxy_addresses,
       "add-proxy-addresses=s" => \$add_proxy_addresses,
       "remove-proxy-addresses=s" => \$remove_proxy_addresses,
       "entry=s" => \$entry,
       "custom1=s" => \$custom_1,
       "custom2=s" => \$custom_2,
       "custom3=s" => \$custom_3,
       "custom4=s" => \$custom_4,
       "custom5=s" => \$custom_5,
       "custom-multi1=s" => \$custom_multi_1,
       "custom-multi2=s" => \$custom_multi_2,
       "custom-multi3=s" => \$custom_multi_3,
       "custom-multi4=s" => \$custom_multi_4,
       "custom-multi5=s" => \$custom_multi_5,
       "add-custom-multi1=s" => \$add_custom_multi_1,
       "add-custom-multi2=s" => \$add_custom_multi_2,
       "add-custom-multi3=s" => \$add_custom_multi_3,
       "add-custom-multi4=s" => \$add_custom_multi_4,
       "add-custom-multi5=s" => \$add_custom_multi_5,
       "remove-custom-multi1=s" => \$remove_custom_multi_1,
       "remove-custom-multi2=s" => \$remove_custom_multi_2,
       "remove-custom-multi3=s" => \$remove_custom_multi_3,
       "remove-custom-multi4=s" => \$remove_custom_multi_4,
       "remove-custom-multi5=s" => \$remove_custom_multi_5,
       "intrinsic1=s" => \$intrinsic_1,
       "intrinsic2=s" => \$intrinsic_2,
       "intrinsic3=s" => \$intrinsic_3,
       "intrinsic4=s" => \$intrinsic_4,
       "intrinsic5=s" => \$intrinsic_5,
       "intrinsic-multi1=s" => \$intrinsic_multi_1,
       "intrinsic-multi2=s" => \$intrinsic_multi_2,
       "intrinsic-multi3=s" => \$intrinsic_multi_3,
       "intrinsic-multi4=s" => \$intrinsic_multi_4,
       "intrinsic-multi5=s" => \$intrinsic_multi_5,
       "add-intrinsic-multi1=s" => \$add_intrinsic_multi_1,
       "add-intrinsic-multi2=s" => \$add_intrinsic_multi_2,
       "add-intrinsic-multi3=s" => \$add_intrinsic_multi_3,
       "add-intrinsic-multi4=s" => \$add_intrinsic_multi_4,
       "add-intrinsic-multi5=s" => \$add_intrinsic_multi_5,
       "remove-intrinsic-multi1=s" => \$remove_intrinsic_multi_1,
       "remove-intrinsic-multi2=s" => \$remove_intrinsic_multi_2,
       "remove-intrinsic-multi3=s" => \$remove_intrinsic_multi_3,
       "remove-intrinsic-multi4=s" => \$remove_intrinsic_multi_4,
       "remove-intrinsic-multi5=s" => \$remove_intrinsic_multi_5,
       "quota=s" => \$quota,
       "quota-reset-students=s" => \$quota_reset_students,
       "quota-reset-teachers=s" => \$quota_reset_teachers,
       "mailquota=s" => \$mailquota,
       "webui-dashboard=s" => \$webui_dashboard,
       "webui-permissions=s" => \$webui_permissions,
       "add-webui-permissions=s" => \$add_webui_permissions,
       "remove-webui-permissions=s" => \$remove_webui_permissions,
       "U|usable" => \$usable,
       "E|enable" => \$enable,
       "A|activate" => \$activate,
       "S|selfactivate" => \$selfactivate,
       "P|permanent" => \$permanent,
       "T|tolerate" => \$tolerate,
       "D|deactivate" => \$deactivate,
       "L|lock" => \$lock,
       "F|freeze" => \$freeze,
       "K|killable" => \$killable,
       "R|removable" => \$removable,
#       "old-uid=s" => \$old_uid,
#       "new-uid=s" => \$new_uid,
#       "old-mailbox=s" => \$old_mailbox,
#       "new-mailbox=s" => \$new_mailbox,
#       "kill-all-users" => \$kill_all_users,
#       "reset-user=s" => \$reset_user,
#       "empty-password" => \$empty_password,
#       "scheduled-toleration=s" => \$sc_toleration,
#       "t|teacher=s" => \$teacher,
#       "list-teachers-by-year" => \$list_teachers_by_year,
#       "new-class|newclass=s" => \$new_class,
          );


my %sophomorix_result=&result_sophomorix_init("sophomorix-user");
# Prüfen, ob Optionen erkannt wurden
&check_options($testopt,\%sophomorix_result,$json);


# Scriptname ermitteln
my @list = split(/\//,$0);
my $scriptname = pop @list;

# --help
if ($help==1) {
   # Befehlbeschreibung
   print('
sophomorix-user edits the sophomorix user database.

Options
  -h   /  --help
  -i   /  --info                           summary of users
  -iv  (-school <school>)                  one user per line
  -iu / --info --user <user1>,<user2>,...  detailed view of user                 
  -v   /  --verbose
  -vv  /  --verbose --verbose

Search for a user/users in sAMAccountName:
  -u user   /   --user user
  -u \'*\'   /   --user u*se*r (option is passed to ldap filter)
Grep in the results:
 -u *user* | grep -i --text <strg>
  
Choose the school:
  --school <school>

Changing status of a user:
  -E --user <user>  /   --enable --user <user>
  -P --user <user>  /   --permanent --user <user>
  -F --user <user>  /   --freeze --user <user>
  -L --user <user>  /   --lock --user <user>
  -K --user <user>  /   --killable --user <user>

These are done by the automatic workflow, you shold not need them
  -U --user <user>  /   --usable --user <user>
  -A --user <user>  /   --activate --user <user>
  -S --user <user>  /   --selfactivate --user <user>
  -T --user <user>  /   --tolerate --user <user>
  -D --user <user>  /   --deactivate --user <user>
  -R --user <user>  /   --removable --user <user>

Share Quota:
    Updating individual Quota of a user:
        --quota <linuxmuster-global>:<number(MiB)>:<comment>,
                <share1>:<quota1 in MiB>:<comment1>,...
    Resetting individual Quota of a user:
        --quota <linuxmuster-global>:---:<comment>,
                <share1>:---:<comment1>,...
    Resetting Quota of all students of a school:
        --quota-reset-students <schoolname>
    Resetting Quota of all teachers of a school:
        --quota-reset-teachers <schoolname>

To update the quota on the smb share use sophomorix-quota!

MailQuota:
    Updating MailQuota of a user:
        --quota <linuxmuster-global>:<number(MiB)>:<comment>,
                <share1>:<quota1 in MiB>:<comment1>,...
        --mailquota <mailquota in MiB)>:<comment>

Updating unid of a user:
  --user <user> --set-unid <unid>   (set unit to <unid>)
  --user <user> --clear-unid        (prepare for upload of a unid from students file)

Updating comment of a user:
  --user <user> --comment "This is the comment"

Updating the user configurable E-Mail address attribute proxyAddresses (multi-value):
  --user <user> --set-proxy-addresses <address1>,<address2>,...
  --user <user> --add-proxy-addresses <address>
  --user <user> --remove-proxy-addresses <address>

Updating sophomorixCustom1 - sophomorixCustom5:
  --user <user> --custom<n> "Content of sophomorixCustom<n>"
Updating sophomorixCustomMulti1 - sophomorixCustomMulti5:
  --user <user> --custom-multi<n> "Content of sophomorixCustomMulti<n>"
  --user <user> --add-custom-multi<n> "Added entry in sophomorixCustomMulti<n>"
  --user <user> --remove-custom-multi<n> "Removed entry from sophomorixCustomMulti<n>"

Updating sophomorixIntrinsic1 - sophomorixIntrinsic5: (Warning! This is for sophomorix internal use)
  --user <user> --intrinsic<n> "Content of sophomorixIntrinsic<n>"
Updating sophomorixIntrinsicMulti1 - sophomorixIntrinsicMulti5: (Warning! This is for sophomorix internal use)
  --user <user> --intrinsic-multi<n> "Content of sophomorixIntrinsicMulti<n>"
  --user <user> --add-intrinsic-multi<n> "Added entry in sophomorixIntrinsicMulti<n>"
  --user <user> --remove-intrinsic-multi<n> "Removed entry from sophomorixIntrinsicMulti<n>"


Updating WebuiDashboard of a user:
  --user <user> --webui-dashboard "Webui Dashboard String"
  --user <user> --webui-permissions permission1,permission2,permission3, ... 
  --user <user> --add-webui-permissions permission1,permission2,permission3, ...
  --user <user> --remove-webui-permissions permission1,permission2,permission3, ...

  permissions1, ... must have the systax:  <ui>:<module>:<TRUE/FALSE>

Updating OTHER single value attribute:
  Set <attributeName> from <user> to <data>
    --user <user> --set-single-value-attribute <attributeName> --entry <data>
  Remove <attributeName> from <user>
    --user <user> --set-single-value-attribute <attributeName> --entry ""

Updating OTHER multi value attribute:
  Set <attributeName> of <user> to <data1,data2, ...>
    --user <user> --set-multi-value-attribute <attributeName> --entry <data1,data2, ...>
  Remove all entries from <attributeName> of <user>
    --user <user> --set-multi-value-attribute <attributeName> --entry ""
  Add <data1,data2,...> to <attributeName> of <user>
    --user <user> --add-multi-value-attribute <attributeName> --entry <data1,data2, ...>
  Remove <data1,data2,...> from <attributeName> of <user>
    --user <user> --remove-multi-value-attribute <attributeName> --entry <data1,data2, ...>

-> continue here
  --list-teachers-by-year

Todo:
Changing loginname of a user:
  --old-uid oldlogin --new-uid newlogin
  (WARNING: This is Alpha!!! This will never work 100%!)
  Problems: The user has to be logged out!
            cyrus and imap will be restartet!
  --> Do this at night!
  
Todo:
Moving the mailbox of a user:
  (This is included in --old-uid oldlogin --new-uid newlogin.
  It should never be necessary to move the mailbox by itself)
  --old-mailbox oldlogin --new-mailbox newlogin
  --> Do this only when you know what to do!

Todo:
Changing class of a user (that is not in students.csv)
  -u user --newclass newclass
  (to move user, sophomorix-move must be called)

Todo:
Actions:
  --reset-user user1,user2,...
  --reset-user user1,user2,... --empty-password
  --teacher teacher --scheduled-toleration yyyy-mm-dd
  --kill-all-users

Please see the sophomorix-user(8) man pages for full documentation
');
   print "\n";

   exit;
}

my ($ldap,$root_dse) = &AD_bind_admin(\@arguments,\%sophomorix_result,$json);
my $root_dns=&AD_dns_get($root_dse);
my %sophomorix_config=&config_sophomorix_read($ldap,$root_dse,\%sophomorix_result);




# --info (overview)
if ($info==1 and $user eq "" and $Conf::log_level==1){
    my $ref_user_v=&AD_get_users_v({ldap=>$ldap,
                                  root_dse=>$root_dse,
                                  root_dns=>$root_dns,
                                  school=>$school,
                                  sophomorix_config=>\%sophomorix_config,
                                });
    #print Dumper($ref_user_v);
    my $jsoninfo="USERS_OVERVIEW";
    my $jsoncomment="The sophomorix users";
    &json_dump({json => $json,
                jsoninfo => $jsoninfo,
                jsoncomment => $jsoncomment,
                object_name => $school,
                log_level => $Conf::log_level,
                hash_ref => $ref_user_v,
                sophomorix_config => \%sophomorix_config,
               });
    exit;
}




# --info --verbose (one user per line)
if ($info==1 and $user eq "" and $Conf::log_level==2){
    my $ref_user_v=&AD_get_users_v({ldap=>$ldap,
                                  root_dse=>$root_dse,
                                  root_dns=>$root_dns,
                                  school=>$school,
                                  sophomorix_config=>\%sophomorix_config,
                                });
    #print Dumper($ref_user_v);
    my $jsoninfo="USERS_V";
    my $jsoncomment="All users";
    &json_dump({json => $json,
                jsoninfo => $jsoninfo,
                jsoncomment => $jsoncomment,
                object_name => $school,
                log_level => $Conf::log_level,
                hash_ref => $ref_user_v,
                sophomorix_config => \%sophomorix_config,
               });
   exit;
}




# Setting the filters
my $school_filter="";
my $user_filter="";
if ($school eq $DevelConf::name_default_school){
    $school_filter="";
} elsif ($school ne ""){
    $school_filter="(sophomorixSchoolPrefix=$school)";
}
if ($user ne ""){
    $user_filter="(sAMAccountName=$user)";
}



# --info --user <user>
if ($user ne "" and $info==1){
    my $ref_users=&AD_get_full_userdata({ldap=>$ldap,
                                        root_dse=>$root_dse,
                                        root_dns=>$root_dns,
                                        userlist=>$user,
                                        sophomorix_config=>\%sophomorix_config,
                                      });
    #print Dumper($ref_users);
    my $jsoninfo="USER";
    my $jsoncomment="One sophomorix user";
    &json_dump({json => $json,
                jsoninfo => $jsoninfo,
                jsoncomment => $jsoncomment,
                object_name => $user,
                log_level => $Conf::log_level,
                hash_ref => $ref_users,
                sophomorix_config => \%sophomorix_config,
               });
    # print more info stuff
    print "\n";
    print "More info for users in AD with:\n"; 
    if ($Conf::log_level==1){
        print "   $scriptname -iv -u $user\n";
    }
    print "   sophomorix-quota -iv -u $user\n";
    print "   sophomorix-mail -iv -u $user\n";
    exit;
} elsif ($list_teachers_by_year==1){
    print "... still to do!\n";
    #&list_teachers_by_year();
    exit 88;
}






&result_sophomorix_check_exit(\%sophomorix_result,\%sophomorix_config,$json);
&log_script_start(\@arguments,\%sophomorix_result,\%sophomorix_config);


# # --old-uid oldlogin --new-uid newlogin
# if ($old_uid ne "" and $new_uid ne ""){

#     my %forbidden_login_hash=&forbidden_login_hash();

#     if (exists($forbidden_login_hash{$new_uid})){
#         &log_script_exit("ERROR: New login $new_uid exists in the system",
#                          1,1,0,\@arguments,\%sophomorix_result,$json);
#     }

#     # Fetch old data
#     my ($old_home,$old_type,$old_gecos,$old_group,$old_uidnumber,
#        $old_sambahomepath) = &fetchdata_from_account($old_uid);

#     if ($old_home eq ""){
#         &log_script_exit("Could not find old-uid $old_uid",
#                          1,1,0,\@arguments,\%sophomorix_result,$json);
#     }
#     my $dbh=&db_connect();
#     my ($old_dn)= $dbh->selectrow_array( "SELECT dn
#                                          FROM ldap_entries 
#                                          WHERE dn LIKE 'uid=$old_uid%'
#                                         ");
#     # changing stuff
#     my $new_home_update=$old_home;
#     $new_home_update=~s/\/${old_uid}$/\/${new_uid}/;
#     my $new_dn_update=$old_dn;
#     $new_dn_update=~s/uid=${old_uid},/uid=${new_uid},/;

#     print "1) Updating $old_uid to $new_uid in the sophomorix database\n";
#     &update_user_db_entry($old_uid,"Uid=$new_uid");
#     my $sql="UPDATE ldap_entries SET dn='$new_dn_update'
#              WHERE dn='$old_dn'";
#     if($Conf::log_level>=3){
#               print "\nSQL: $sql\n";
#     }
#     $dbh->do($sql);
#     &update_uid_ldap($old_uid,$new_uid);

#     print "2) Moving homedir.\n";
#     system("mv $old_home $new_home_update");

#     print "3) Renaming mailbox: $old_uid --> $new_uid\n";
#     &move_mailbox($old_uid,$new_uid);

#     print "4) Updating Horde database not supported\n";

#     print "5) Updating lehrer.txt if $old_uid is a teacher\n";
#     if ($old_type eq "teacher"){
#         &lehrer_ordnen($old_uid,$new_uid);
#     }

#     print "6) Updating /etc/aliases:\n";

#     system("${DevelConf::path_sbin}/sophomorix-mail --skiplock");
#     # log
#     &append_login_rename_log("man-rename-login",
#                            $old_uid,
#                            $new_uid,
#                            $old_uidnumber,
#                          );
#     print "... done!\n";


#     # Fetch new data
#     my ($new_home_db,$new_type,$new_gecos,$new_group,$new_uidnumber,
#        $new_sambahomepath_db) = &fetchdata_from_account($new_uid);
#     my ($new_dn_db)= $dbh->selectrow_array( "SELECT dn
#                                          FROM ldap_entries 
#                                          WHERE dn LIKE 'uid=$new_uid%'
#                                         ");
#     my $id_result_old="";
#     if (system("id $old_uid > /dev/null 2>&1")){
#         $id_result_old="$old_uid (nonexisting,OK)";
#     } else {
#         $id_result_old="$old_uid (existing,ERROR)";
#     };
#     my $id_result_new="";
#     if (system("id $new_uid > /dev/null 2>&1")){
#         $id_result_new="$new_uid (nonexisting,ERROR)";
#     } else {
#         $id_result_new="$new_uid (existing,OK)";
#     };

#     my $old_home_result="";
#     if (-d $old_home){
# 	$old_home_result=$old_home." (exists,ERROR)";
#     } else {
# 	$old_home_result=$old_home." (nonexisting,OK)";
#     }
#     my $new_home_result="";
#     if (-d $new_home_db){
# 	$new_home_result=$new_home_db." (exists,OK)";
#     } else {
# 	$new_home_result=$new_home_db." (nonexisting,ERROR)";
#     }

#     # Path to mailbox
#     my $old_initial= substr $old_uid,0,1;
#     my $new_initial= substr $new_uid,0,1;
#     my $old_mailbox_dir="/var/spool/cyrus/mail/".$old_initial."/user/".$old_uid;
#     my $new_mailbox_dir="/var/spool/cyrus/mail/".$new_initial."/user/".$new_uid;
#     my $old_mailbox_dir_result="";
#     my $new_mailbox_dir_result="";

#     if (-d $old_mailbox_dir){
# 	$old_mailbox_dir_result=$old_mailbox_dir." (exists, ERROR)";
#     } else {
# 	$old_mailbox_dir_result=$old_mailbox_dir." (nonexisting, OK)";
#     }
#     if (-d $new_mailbox_dir){
# 	$new_mailbox_dir_result=$new_mailbox_dir." (exists, OK)";
#     } else {
# 	$new_mailbox_dir_result=$new_mailbox_dir." (nonexisting, ERROR)";
#     }

#     # ????? /var/lib/cyrus/lock/w/user/wei.lock bleibt erhalten

#     # print results
#     print "Checking success:\n";
#     print " Unix Acount:\n";
#     print "    OLD: $id_result_old\n";
#     print "    NEW: $id_result_new\n";
#     print " LDAP DN:\n";
#     print "    OLD: $old_dn\n";
#     print "    NEW: $new_dn_db\n";
#     print " UNIX home:\n";
#     print "    OLD: $old_home_result\n";
#     print "    NEW: $new_home_result\n";
#     print " SAMBA home:\n";
#     print "    OLD: $old_sambahomepath\n";
#     print "    NEW: $new_sambahomepath_db\n";
#     print " CYRUS mailbox dir:\n";
#     print "    OLD: $old_mailbox_dir_result\n";
#     print "    NEW: $new_mailbox_dir_result\n";
#     &log_script_exit("",0,1,0,\@arguments,\%sophomorix_result,$json);
# }

######################################################################
# Standard: suchen nach string
######################################################################

#if ($user eq "" and $list_teachers_by_year==0){
#   print "I don't know what to search for! (searchstring is empty)\n";
#   &log_script_exit("",0,1,0,\@arguments,\%sophomorix_result,$json);
#}


############################################################
# --quota-reset-students <schoolname>
if (defined $quota_reset_students and $quota_reset_students ne ""){
    print "\n";
    print "Resetting individual quota for all students in school $quota_reset_students:\n";
    my $ref_user_v=&AD_get_users_v({ldap=>$ldap,
                                  root_dse=>$root_dse,
                                  root_dns=>$root_dns,
                                  school=>$quota_reset_students,
                                  sophomorix_config=>\%sophomorix_config,
                                });

    my $max_user_count=$#{ $ref_user_v->{'LISTS'}{'USER_by_sophomorixSchoolname'}{$quota_reset_students}{'student'} }+1;
    my $user_count=0;
    foreach my $user (@{ $ref_user_v->{'LISTS'}{'USER_by_sophomorixSchoolname'}{$quota_reset_students}{'student'} }){
        my ($count,$dn,$cn)=&AD_object_search($ldap,$root_dse,"user",$user);
        $user_count++;
        &AD_user_update({ldap=>$ldap,
                         root_dse=>$root_dse,
                         dn=>$dn,
                         user=>$user,
                         quota=>"linuxmuster-global:---:---",
                         quota_force=>"TRUE",
                         user_count=>$user_count,
                         max_user_count=>$max_user_count,
                         json=>$json,
                         sophomorix_config=>\%sophomorix_config,
                         sophomorix_result=>\%sophomorix_result,
                       });
        &AD_user_update({ldap=>$ldap,
                         root_dse=>$root_dse,
                         dn=>$dn,
                         user=>$user,
                         quota=>"default-school:---:---",
                         quota_force=>"TRUE",
                         user_count=>$user_count,
                         max_user_count=>$max_user_count,
                         json=>$json,
                         sophomorix_config=>\%sophomorix_config,
                         sophomorix_result=>\%sophomorix_result,
                       });
    }
    print "\n";
    &log_script_end(\@arguments,\%sophomorix_result,\%sophomorix_config,$json);
}

############################################################
# --quota-reset-teachers <schoolname>
if (defined $quota_reset_teachers and $quota_reset_teachers ne ""){
    print "\n";
    print "Resetting individual quota for all teachers in school $quota_reset_teachers:\n";
    my $ref_user_v=&AD_get_users_v({ldap=>$ldap,
                                  root_dse=>$root_dse,
                                  root_dns=>$root_dns,
                                  school=>$quota_reset_teachers,
                                  sophomorix_config=>\%sophomorix_config,
                                });

    my $max_user_count=$#{ $ref_user_v->{'LISTS'}{'USER_by_sophomorixSchoolname'}{$quota_reset_teachers}{'teacher'} }+1;
    my $user_count=0;
    foreach my $user (@{ $ref_user_v->{'LISTS'}{'USER_by_sophomorixSchoolname'}{$quota_reset_teachers}{'teacher'} }){
        my ($count,$dn,$cn)=&AD_object_search($ldap,$root_dse,"user",$user);
        $user_count++;
        &AD_user_update({ldap=>$ldap,
                         root_dse=>$root_dse,
                         dn=>$dn,
                         user=>$user,
                         quota=>"linuxmuster-global:---:---",
                         quota_force=>"TRUE",
                         user_count=>$user_count,
                         max_user_count=>$max_user_count,
                         json=>$json,
                         sophomorix_config=>\%sophomorix_config,
                         sophomorix_result=>\%sophomorix_result,
                       });
        &AD_user_update({ldap=>$ldap,
                         root_dse=>$root_dse,
                         dn=>$dn,
                         user=>$user,
                         quota=>"default-school:---:---",
                         quota_force=>"TRUE",
                         user_count=>$user_count,
                         max_user_count=>$max_user_count,
                         json=>$json,
                         sophomorix_config=>\%sophomorix_config,
                         sophomorix_result=>\%sophomorix_result,
                       });
    }
    print "\n";
    &log_script_end(\@arguments,\%sophomorix_result,\%sophomorix_config,$json);
}



######################################################################
# Options that need a valid user follow here
######################################################################

# get dn if user exists and continue
############################################################
my ($count,$dn,$cn)=&AD_object_search($ldap,$root_dse,"user",$user);

if ($count>1){
    print "\n$count users found. Cannot modify more than one user!\n";
    print "Dont use * and stuff like this\n\n";
    exit 88;
} elsif ($count==0){
    print "\nERROR retrieving user $user: $count users found.\n\n";
    exit 88;
}



############################################################
# --user <user> --comment "Test for comment"
if ($user ne "" and defined $comment){
    print "Updating comment for user $user:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     comment=>$comment,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}

############################################################
# --user <user> --custom1(-5) "Test for custom1(-5)"
if ($user ne "" and defined $custom_1){
    print "Updating sophomorixCustom1 for user $user:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     custom_1=>$custom_1,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}
if ($user ne "" and defined $custom_2){
    print "Updating sophomorixCustom2 for user $user:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     custom_2=>$custom_2,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}
if ($user ne "" and defined $custom_3){
    print "Updating sophomorixCustom3 for user $user:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     custom_3=>$custom_3,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}
if ($user ne "" and defined $custom_4){
    print "Updating sophomorixCustom4 for user $user:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     custom_4=>$custom_4,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}
if ($user ne "" and defined $custom_5){
    print "Updating sophomorixCustom5 for user $user:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     custom_5=>$custom_5,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}


############################################################
# --user <user> --intrinsic1(-5) "Test for intrinsic1(-5)"
if ($user ne "" and defined $intrinsic_1){
    print "Updating sophomorixIntrinsic1 for user $user:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     intrinsic_1=>$intrinsic_1,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}
if ($user ne "" and defined $intrinsic_2){
    print "Updating sophomorixIntrinsic2 for user $user:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     intrinsic_2=>$intrinsic_2,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}
if ($user ne "" and defined $intrinsic_3){
    print "Updating sophomorixIntrinsic3 for user $user:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     intrinsic_3=>$intrinsic_3,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}
if ($user ne "" and defined $intrinsic_4){
    print "Updating sophomorixIntrinsic4 for user $user:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     intrinsic_4=>$intrinsic_4,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}
if ($user ne "" and defined $intrinsic_5){
    print "Updating sophomorixIntrinsic5 for user $user:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     intrinsic_5=>$intrinsic_5,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}


############################################################
# --user <user> --quota <school1>:<number1>,<school2>:<number2>
if ($user ne "" and defined $quota){
    print "Updating quota for user $user:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     quota=>$quota,
                     quota_force=>"TRUE",
                     user_count=>"1",
                     max_user_count=>"1",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}


############################################################
# --user <user> --intrinsic-multi1(-5) "Test for intrinsic-multi1(-5)"
if (defined $intrinsic_multi_1 and $user ne ""){
   my @permissions=split(/,/,$intrinsic_multi_1);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     intrinsic_multi_1=>$intrinsic_multi_1,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}
if (defined $intrinsic_multi_2 and $user ne ""){
   my @permissions=split(/,/,$intrinsic_multi_2);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     intrinsic_multi_2=>$intrinsic_multi_2,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}
if (defined $intrinsic_multi_3 and $user ne ""){
   my @permissions=split(/,/,$intrinsic_multi_3);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     intrinsic_multi_3=>$intrinsic_multi_3,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}
if (defined $intrinsic_multi_4 and $user ne ""){
   my @permissions=split(/,/,$intrinsic_multi_4);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     intrinsic_multi_4=>$intrinsic_multi_4,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}
if (defined $intrinsic_multi_5 and $user ne ""){
   my @permissions=split(/,/,$intrinsic_multi_5);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     intrinsic_multi_5=>$intrinsic_multi_5,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}

# --add-intrinsic-multi1(-5) "Test for intrinsic-multi1(-5)"
if (defined $add_intrinsic_multi_1 and $user ne ""){
    my @add=split(/,/,$add_intrinsic_multi_1);
    my @old = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,"sophomorixIntrinsicMulti1");
    my @intrinsic_multi_1 = uniq(@old,@add); 
    my $intrinsic_multi_1=join(",",@intrinsic_multi_1);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     intrinsic_multi_1=>$intrinsic_multi_1,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}
if (defined $add_intrinsic_multi_2 and $user ne ""){
    my @add=split(/,/,$add_intrinsic_multi_2);
    my @old = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,"sophomorixIntrinsicMulti2");
    my @intrinsic_multi_2 = uniq(@old,@add); 
    my $intrinsic_multi_2=join(",",@intrinsic_multi_2);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     intrinsic_multi_2=>$intrinsic_multi_2,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}
if (defined $add_intrinsic_multi_3 and $user ne ""){
    my @add=split(/,/,$add_intrinsic_multi_3);
    my @old = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,"sophomorixIntrinsicMulti3");
    my @intrinsic_multi_3 = uniq(@old,@add); 
    my $intrinsic_multi_3=join(",",@intrinsic_multi_3);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     intrinsic_multi_3=>$intrinsic_multi_3,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}
if (defined $add_intrinsic_multi_4 and $user ne ""){
    my @add=split(/,/,$add_intrinsic_multi_4);
    my @old = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,"sophomorixIntrinsicMulti4");
    my @intrinsic_multi_4 = uniq(@old,@add); 
    my $intrinsic_multi_4=join(",",@intrinsic_multi_4);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     intrinsic_multi_4=>$intrinsic_multi_4,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}
if (defined $add_intrinsic_multi_5 and $user ne ""){
    my @add=split(/,/,$add_intrinsic_multi_5);
    my @old = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,"sophomorixIntrinsicMulti5");
    my @intrinsic_multi_5 = uniq(@old,@add); 
    my $intrinsic_multi_5=join(",",@intrinsic_multi_5);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     intrinsic_multi_5=>$intrinsic_multi_5,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}

# --remove-intrinsic-multi1(-5) "Test for intrinsic-multi1(-5)"
if (defined $remove_intrinsic_multi_1){
    my @old = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,"sophomorixIntrinsicMulti1");
    my @intrinsic_multi_1 = &remove_from_list($remove_intrinsic_multi_1,@old);
    my $intrinsic_multi_1=join(",",@intrinsic_multi_1);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     intrinsic_multi_1=>$intrinsic_multi_1,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}
if (defined $remove_intrinsic_multi_2){
    my @old = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,"sophomorixIntrinsicMulti2");
    my @intrinsic_multi_2 = &remove_from_list($remove_intrinsic_multi_2,@old);
    my $intrinsic_multi_2=join(",",@intrinsic_multi_2);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     intrinsic_multi_2=>$intrinsic_multi_2,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}
if (defined $remove_intrinsic_multi_3){
    my @old = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,"sophomorixIntrinsicMulti3");
    my @intrinsic_multi_3 = &remove_from_list($remove_intrinsic_multi_3,@old);
    my $intrinsic_multi_3=join(",",@intrinsic_multi_3);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     intrinsic_multi_3=>$intrinsic_multi_3,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}
if (defined $remove_intrinsic_multi_4){
    my @old = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,"sophomorixIntrinsicMulti4");
    my @intrinsic_multi_4 = &remove_from_list($remove_intrinsic_multi_4,@old);
    my $intrinsic_multi_4=join(",",@intrinsic_multi_4);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     intrinsic_multi_4=>$intrinsic_multi_4,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}
if (defined $remove_intrinsic_multi_5){
    my @old = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,"sophomorixIntrinsicMulti5");
    my @intrinsic_multi_5 = &remove_from_list($remove_intrinsic_multi_5,@old);
    my $intrinsic_multi_5=join(",",@intrinsic_multi_5);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     intrinsic_multi_5=>$intrinsic_multi_5,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}




############################################################
# --user <user> --custom-multi1(-5) "Test for custom-multi1(-5)"
if (defined $custom_multi_1 and $user ne ""){
   my @permissions=split(/,/,$custom_multi_1);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     custom_multi_1=>$custom_multi_1,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}
if (defined $custom_multi_2 and $user ne ""){
   my @permissions=split(/,/,$custom_multi_2);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     custom_multi_2=>$custom_multi_2,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}
if (defined $custom_multi_3 and $user ne ""){
   my @permissions=split(/,/,$custom_multi_3);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     custom_multi_3=>$custom_multi_3,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}
if (defined $custom_multi_4 and $user ne ""){
   my @permissions=split(/,/,$custom_multi_4);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     custom_multi_4=>$custom_multi_4,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}
if (defined $custom_multi_5 and $user ne ""){
   my @permissions=split(/,/,$custom_multi_5);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     custom_multi_5=>$custom_multi_5,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}



# --clear-unid
if ($user ne "" and $clear_unid==1){
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     unid=>"", # sets unid to "---"
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}


# --set-unid <unid>
if ($user ne "" and $set_unid ne ""){
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     unid=>$set_unid,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}

# --set-single-value-attribute <attribute> --entry <entry>
if ($user ne "" and defined $set_single_value_attribute and defined $entry ){
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     single_value_set=>$set_single_value_attribute,
                     single_value_entry=>$entry,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}

# --set-multi-value-attribute <mvattr> --entry <entry>
if ($user ne "" and defined $set_multi_value_attribute and defined $entry ){
    #my @permissions=split(/,/,$custom_multi_1);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     multi_value_set=>$set_multi_value_attribute,
                     multi_value_entry=>$entry,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}

# --add-multi-value-attribute <mvattr> --entry <entry>
if ($user ne "" and defined $add_multi_value_attribute and defined $entry ){
    my @add=split(/,/,$entry);
    my @old = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,$add_multi_value_attribute);
    my @multi_value_entry = uniq(@old,@add); 
    my $multi_value_entry=join(",",@multi_value_entry);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     multi_value_add=>$add_multi_value_attribute,
                     multi_value_entry=>$multi_value_entry,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}

# --remove-multi-value-attribute <mvattr> --entry <entry>
if ($user ne "" and defined $remove_multi_value_attribute and defined $entry ){
    my @old = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,$remove_multi_value_attribute);
    my @multi_value_entry = &remove_from_list($entry,@old);
    my $multi_value_entry=join(",",@multi_value_entry);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     multi_value_remove=>$remove_multi_value_attribute,
                     multi_value_entry=>$multi_value_entry,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}

# --set-proxy-addresses <entry>
if ($user ne "" and defined $set_proxy_addresses ){
    #my @permissions=split(/,/,$custom_multi_1);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     proxy_addresses_set=>$set_proxy_addresses,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}

# --add-proxy-addresses <entry>
if ($user ne "" and defined $add_proxy_addresses ){
    my @add=split(/,/,$add_proxy_addresses);
    my @old = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,"proxyAddresses");
    my @proxy_addresses_entry = uniq(@old,@add); 
    my $proxy_addresses_entry=join(",",@proxy_addresses_entry);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     proxy_addresses_add=>$add_proxy_addresses,
                     proxy_addresses_entry=>$proxy_addresses_entry,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}

# --remove-proxy-addresses <entry>
if ($user ne "" and defined $remove_proxy_addresses){
    my @old = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,"proxyAddresses");
    my @proxy_addresses_entry = &remove_from_list($remove_proxy_addresses,@old);
    my $proxy_addresses_entry=join(",",@proxy_addresses_entry);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     proxy_addresses_remove=>$remove_proxy_addresses,
                     proxy_addresses_entry=>$proxy_addresses_entry,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}

# --add-custom-multi1(-5) "Test for custom-multi1(-5)"
if (defined $add_custom_multi_1 and $user ne ""){
    my @add=split(/,/,$add_custom_multi_1);
    my @old = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,"sophomorixCustomMulti1");
    my @custom_multi_1 = uniq(@old,@add); 
    my $custom_multi_1=join(",",@custom_multi_1);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     custom_multi_1=>$custom_multi_1,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}
if (defined $add_custom_multi_2 and $user ne ""){
    my @add=split(/,/,$add_custom_multi_2);
    my @old = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,"sophomorixCustomMulti2");
    my @custom_multi_2 = uniq(@old,@add); 
    my $custom_multi_2=join(",",@custom_multi_2);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     custom_multi_2=>$custom_multi_2,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}
if (defined $add_custom_multi_3 and $user ne ""){
    my @add=split(/,/,$add_custom_multi_3);
    my @old = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,"sophomorixCustomMulti3");
    my @custom_multi_3 = uniq(@old,@add); 
    my $custom_multi_3=join(",",@custom_multi_3);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     custom_multi_3=>$custom_multi_3,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}
if (defined $add_custom_multi_4 and $user ne ""){
    my @add=split(/,/,$add_custom_multi_4);
    my @old = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,"sophomorixCustomMulti4");
    my @custom_multi_4 = uniq(@old,@add); 
    my $custom_multi_4=join(",",@custom_multi_4);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     custom_multi_4=>$custom_multi_4,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}
if (defined $add_custom_multi_5 and $user ne ""){
    my @add=split(/,/,$add_custom_multi_5);
    my @old = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,"sophomorixCustomMulti5");
    my @custom_multi_5 = uniq(@old,@add); 
    my $custom_multi_5=join(",",@custom_multi_5);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     custom_multi_5=>$custom_multi_5,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}

# --remove-custom-multi1(-5) "Test for custom-multi1(-5)"
if (defined $remove_custom_multi_1){
    my @old = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,"sophomorixCustomMulti1");
    my @custom_multi_1 = &remove_from_list($remove_custom_multi_1,@old);
    my $custom_multi_1=join(",",@custom_multi_1);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     custom_multi_1=>$custom_multi_1,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}
if (defined $remove_custom_multi_2){
    my @old = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,"sophomorixCustomMulti2");
    my @custom_multi_2 = &remove_from_list($remove_custom_multi_2,@old);
    my $custom_multi_2=join(",",@custom_multi_2);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     custom_multi_2=>$custom_multi_2,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}
if (defined $remove_custom_multi_3){
    my @old = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,"sophomorixCustomMulti3");
    my @custom_multi_3 = &remove_from_list($remove_custom_multi_3,@old);
    my $custom_multi_3=join(",",@custom_multi_3);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     custom_multi_3=>$custom_multi_3,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}
if (defined $remove_custom_multi_4){
    my @old = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,"sophomorixCustomMulti4");
    my @custom_multi_4 = &remove_from_list($remove_custom_multi_4,@old);
    my $custom_multi_4=join(",",@custom_multi_4);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     custom_multi_4=>$custom_multi_4,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}
if (defined $remove_custom_multi_5){
    my @old = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,"sophomorixCustomMulti5");
    my @custom_multi_5 = &remove_from_list($remove_custom_multi_5,@old);
    my $custom_multi_5=join(",",@custom_multi_5);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     custom_multi_5=>$custom_multi_5,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}



############################################################
# --user <user> --mailquota <number>
if ($user ne "" and defined $mailquota){
    print "Updating mailquota for user $user:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     mailquota=>$mailquota,
                     user_count=>"1",
                     max_user_count=>"1",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}



# --user <user> --webui-dashboard "String"
if ($user ne "" and defined $webui_dashboard){
    print "Updating webui-dashboard for user $user:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     webui_dashboard=>$webui_dashboard,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}



# webui-permissions
# --webui-permissions permission1,permission2,permission3, ... 
if (defined $webui_permissions and $user ne ""){
    my ($firstname_utf8_AD,$lastname_utf8_AD,$adminclass_AD,$existing_AD,$exammode_AD,$role_AD,
        $home_directory_AD,$user_account_control_AD,$toleration_date_AD,$deactivation_date_AD,
        $school_AD,$status_AD,$firstpassword_AD,$unid_AD)=
        &AD_get_user({ldap=>$ldap,
                      root_dse=>$root_dse,
                      root_dns=>$root_dns,
                      user=>$user,
                    });
   my @permissions=split(/,/,$webui_permissions);
    foreach my $perm (@permissions){
        my ($mod_path,$setting)=&test_webui_permission($perm,
                                                       \%sophomorix_config,
                                                       "Option",
                                                       "check",
                                                       $school_AD,
                                                       $role_AD);
    }
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     webui_permissions=>$webui_permissions,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}



# --add-webui-permissions permission1,permission2,permission3, ...
if (defined $add_webui_permissions and $user ne ""){
    my ($firstname_utf8_AD,$lastname_utf8_AD,$adminclass_AD,$existing_AD,$exammode_AD,$role_AD,
        $home_directory_AD,$user_account_control_AD,$toleration_date_AD,$deactivation_date_AD,
        $school_AD,$status_AD,$firstpassword_AD,$unid_AD)=
        &AD_get_user({ldap=>$ldap,
                      root_dse=>$root_dse,
                      root_dns=>$root_dns,
                      user=>$user,
                    });
    my @add_webui_permissions=split(/,/,$add_webui_permissions);
    foreach my $perm (@add_webui_permissions){
        my ($mod_path,$setting)=&test_webui_permission($perm,
                                                       \%sophomorix_config,
                                                       "Option",
                                                       "check",
                                                       $school_AD,
                                                       $role_AD);
    }
    my @old_webui_permissions = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,"sophomorixWebuiPermissions");
    my @webui_permissions = uniq(@old_webui_permissions,@add_webui_permissions); 
    my $webui_permissions=join(",",@webui_permissions);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     webui_permissions=>$webui_permissions,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}


# --remove-webui-permissions permission1,permission2,permission3, ...
if (defined $remove_webui_permissions){
    my @old_webui_permissions = &AD_dn_fetch_multivalue($ldap,$root_dse,$dn,"sophomorixWebuiPermissions");
    my @webui_permissions = &remove_from_list($remove_webui_permissions,@old_webui_permissions);
    my $webui_permissions=join(",",@webui_permissions);
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     webui_permissions=>$webui_permissions,
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                    });
}



# -D --user <user> / --deactivate --user <user>
if ($deactivate==1){
    print "Deactivating the account of $user:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     status=>"D",
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}



# -L --user <user> / --deactivate --user <user>
if ($lock==1){
    print "Locking the account of $user:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     status=>"L",
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}



# -F --user <user> / --freeze --user <user>
if ($freeze==1){
    print "Freezing the account of $user:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     status=>"F",
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}



# -P --user <user> / --permanent --user <user>
if ($permanent==1){
    print "Making the account of $user permanent:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     status=>"P",
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}



# -T --user <user> / --tolerate --user <user>
if ($tolerate==1){
    print "Tolerating the account of $user:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     status=>"T",
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}



# -R --user  <user> / --removable --user <user>
if ($removable==1){
    print "Making the account of $user removable by sophomorix-kill:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     status=>"R",
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}



# -K --user <user> / --killable --user <user>
if ($killable==1){
    print "Making the account of $user killable by sophomorix-kill:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     status=>"K",
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}



# -A --user <user> / --activate --user <user>
if ($activate==1){
    print "Actvating the account of $user:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     status=>"A",
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}



# -S --user <user> / --selfactivate --user <user>
if ($selfactivate==1){
    print "Selfctvating the account of $user:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     status=>"S",
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}



# -U --user <user> / --usable --user <user>
if ($usable==1){
    print "Setting the account of $user to usable:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     status=>"U",
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}



# -E --user <user> / --enable --user <user>
if ($enable==1){
    print "Setting the account of $user to enable:\n";
    &AD_user_update({ldap=>$ldap,
                     root_dse=>$root_dse,
                     dn=>$dn,
                     user=>$user,
                     status=>"E",
                     user_count=>"",
                     max_user_count=>"-",
                     json=>$json,
                     sophomorix_config=>\%sophomorix_config,
                     sophomorix_result=>\%sophomorix_result,
                   });
}



# # --old-mailbox oldlogin --new-mailbox newlogin
# if ($old_mailbox ne "" and $new_mailbox ne ""){
#     print "Renaming mailbox: $old_mailbox --> $new_mailbox\n";
#     &move_mailbox($old_mailbox,$new_mailbox);
#     &log_script_exit("",0,1,0,\@arguments,\%sophomorix_result,$json);
# }




# # --kill-all-users
# if ($kill_all_users==1){
#     print "\n\n I will kill all users in 5 seconds!!\n";
#     sleep 1;
#     print "\n\n Hit Ctrl-C to abort!\n\n\n";
#     sleep 5;
#     my @teachers=&fetchstudents_from_adminclass(${DevelConf::teacher});
#     my @students=&Sophomorix::SophomorixAPI::fetchstudents_from_school();
#     my @users=(@teachers,@students);

#     my $count=0;
#     my $max=$#users;
#     foreach my $user_kill (@users){
#         $count++;
#         print "Mark $user_kill as killable. ($count/$max) \n";
#         system("sophomorix-user -K $user_kill");
#     }
#     # kein nscd_stop, da exit folgt.
#     &log_script_exit("",0,1,0,\@arguments,\%sophomorix_result,$json);
# }



# # --teacher teacher --scheduled-toleration
# if ($teacher ne"" and $sc_toleration ne "empty"){
#     # check if it is really an account
#     my ($home,$type,$gecos,$group,$uidnumber)=&fetchdata_from_account($teacher);
 
#     if ($type ne "teacher"){
#         &log_script_exit("$teacher is NOT a valid username of type teacher",
#                          1,1,0,\@arguments,\%sophomorix_result,$json);
#     } else {
#         print "   - $teacher is a valid username and of type 'teacher'\n";
#     }


#     # check date
#     my ($year,$month,$day)=split(/-/,$sc_toleration);
#     # define values, so that check_date does not throw errors 
#     if (not defined $year){
#         $year=9.99;
#     }
#     if (not defined $month){
#         $month=100;
#     }
#     if (not defined $day ){
#         $day=100;
#     }
#     if ($year eq ""){
#         $year=9.99;
#     }
#     if ($month eq ""){
#         $month=100;
#     }
#     if ($day eq "" ){
#         $day=100;
#     }

#     # check if date exists
#     # return 1 when valid
#     # return 0 when invalid
#     my $return=check_date($year,$month,$day);

#     # if date is completely empty, allow this to clear the date in the db
#     if ($sc_toleration eq ""){
#         $return=1;
#     }    

#     if ($return==0){
#         print "Date $sc_toleration is not a valid date ",
#               "(use this syntax: 2012-04-22)\n";
#     } else {
#         $sc_toleration=&date_pg2perl($sc_toleration);
# 	print "   - $sc_toleration is a valid date.\n";
#         # set scheduled-toletration for the account
#         if ($sc_toleration eq ""){
#             print "Removing ScheduledToleration date for $teacher \n";
#         } else {
#             print "Setting ScheduledToleration date for $teacher to $sc_toleration\n";
#         }
#         &update_user_db_entry($teacher, 
#                               "ScheduledToleration=$sc_toleration");
#     }
#     &log_script_exit("",0,1,0,\@arguments,\%sophomorix_result,$json);
# }


# # --reset-user
# if ($reset_user ne""){
#     my @users=split(/,/, $reset_user);
#     # repair.directories einlesen
#     &get_alle_verzeichnis_rechte();
#     # fetch permission for all homes
#     &fetch_repairhome();
#     foreach my $user (@users){
#         # check if it is really an account
#         if (getpwnam("$user")){
#             print "Resetting the account of $user:\n";
# 	    print "   - $user is a valid username.\n";
#         } else {
#             &log_script_exit("$user is not a valid username",
#                          1,1,0,\@arguments,\%sophomorix_result,$json);
#         }
#         # reset the account
#         &reset_user($user);
#         if ($empty_password==1){
#             print "   Setting empty password for $user\n";
#             &set_sophomorix_passwd($user,"");
#         }
#     }
#     &log_script_exit("",0,1,0,\@arguments,\%sophomorix_result,$json);
# }

&AD_unbind_admin($ldap);
&log_script_end(\@arguments,\%sophomorix_result,\%sophomorix_config,$json);



############################################################
# subs
############################################################
