#!/usr/bin/perl -w
# This script (sophomorix-cron) 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 Getopt::Long;
Getopt::Long::Configure ("bundling");
use Sophomorix::SophomorixConfig;
use List::MoreUtils qw(uniq);
use Net::LDAP;
use JSON;
use Data::Dumper;
$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Useqq = 1;
$Data::Dumper::Terse = 1; 
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_add_summary
                                 result_sophomorix_check_exit
                                 result_sophomorix_print
                                 run_hook_scripts
                                 json_dump
                                 read_sophomorix_add
                                 );
use Sophomorix::SophomorixSambaAD qw(
                                 AD_school_create
                                 AD_bind_admin
                                 AD_dns_get
                                 AD_unbind_admin
                                 AD_user_create
                                 AD_group_create
                                 AD_group_addmember
                                 AD_group_update
                                 AD_get_schoolname
                                 AD_get_name_tokened
                                 AD_dn_fetch_multivalue
                                 AD_object_search
                                 AD_get_passwd
                                    );

my @arguments = @ARGV;

my $user_count=0;
my $login_check_ok="";
my $sophomorix_first_password="";
my @users_for_quota=();


############################################################
# options
############################################################
$Conf::log_level=1;
my $help=0;
my $info=0;
my $json=0;
my $lock=0;
my $unlock=0;

# selection lookup hash
my $school_opt="";
my $class_opt="";
my $user_opt="";
my %selection=();

my $testopt=GetOptions(
           "help|h" => \$help,
           "info|i" => \$info,
           "json|j+" => \$json,
           "verbose|v+" => \$Conf::log_level,
          );

my %sophomorix_result=&result_sophomorix_init("sophomorix-add");
&check_options($testopt,\%sophomorix_result,$json);

# Reading Configuration
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);
my ($smb_admin_pass)=&AD_get_passwd($DevelConf::sophomorix_file_admin,
                                     $DevelConf::secret_file_sophomorix_file_admin);



# --help
if ($help==1) {
   # get scriptname
   my @list = split(/\//,$0);
   my $scriptname = pop @list;
   print('
sophomorix-cron-daily is run automatically each day. At the moment it does nothing

Options
  -h  / --help
  -v  / --verbose
  -vv / --verbose --verbose
  -i  / --info

Please see the sophomorix-cron-daily(8) man pages for full documentation
');
   print "\n";
   exit;
}



# --info
if ($info==1) {
   exit;
}


print "Hello this is the daily cronjob sophomorix-cron-daily";
exit 0; # 0: success
