#!/usr/bin/perl -w
# This script (sophomorix-postinst) 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 Sophomorix::SophomorixConfig;
use Sophomorix::SophomorixSambaAD qw(AD_sophomorix_schema_update);
use Sophomorix::SophomorixBase qw(read_smb_conf);

$Conf::log_level=1;
my %smb_conf=();
my $ref_result;
&read_smb_conf(\%smb_conf,$ref_result);
use Data::Dumper;
print Dumper(\%smb_conf);
my $conf_dir="/etc/linuxmuster/sophomorix";
my $conf_file=$conf_dir."/sophomorix.conf";
my $conf_file_source="/usr/share/sophomorix/config-templates/sophomorix.conf.template.ini";
print "##### sophomorix-postinst start #####\n";

print "\n";
print "* Testing $conf_file\n";
# create directory
system "mkdir -p $conf_dir";
######################################################################
# copy the default sophomorix.conf if nonexisting
######################################################################
if (-e $conf_file){
    # do nothing
    print "   * File exists, no installation:\n";
    print "     $conf_file\n";
    # LATER: do upgrade sophomorix.conf
} else {
    # copy the template
    print "     Installing $conf_file\n";
    system("install -oroot -groot --mode=0644 $conf_file_source $conf_file");    
}


######################################################################
# copy the school.conf 
######################################################################
# defaults without options in sophomorix-postinst 
my $school="default-school";
my $school_dir=$conf_dir."/".$school;
my $school_file=$school_dir."/school.conf";

# option given, modify the schoolname
if (defined $ARGV[0] and ($ARGV[0] eq "global" or $ARGV[0] eq "linuxmuster-global")){
    print " \nERROR: You cannot create a school with the name $ARGV[0]\n\n\n";
    exit;
} elsif (defined $ARGV[0] and $ARGV[0] ne $school){
    $school=$ARGV[0];
    $school_dir=$conf_dir."/".$school;
    $school_file=$school_dir."/".$school.".school.conf";
}


my $school_file_source="/usr/share/sophomorix/config-templates/school.conf.template.ini";
print "\n";
print "* Testing for $school_file\n";
# create directory
system "mkdir -p $school_dir";
# copy the default sophomorix.conf
if (-e $school_file){
    # do nothing
    print "   * File exists, no installation:\n";
    print "     $school_file\n";
    # LATER: do upgrade sophomorix.conf
} else {
    # copy the template
    print "     Installing $school_file\n";
    system("install -oroot -groot --mode=0644 $school_file_source $school_file");    
}


############################################################
print "\n";
print "* Installing netlogon example scripts for school $school:\n";
############################################################

# linux lmn
my $netlogon_school_lmn_linux=$smb_conf{'samba'}{'smb.conf'}{'netlogon'}{'path'}."/".$school."/lmn/linux";
system("mkdir -p \"$netlogon_school_lmn_linux\"");
my $startscript_skel_lmn_linux="/usr/share/sophomorix/devel/startscript-skel/lmn/linux";
my @lmn_linux=("logon.sh",
               "logoff.sh",
               "sysstart.sh",
               "sysstop.sh",
              );
foreach my $script (@lmn_linux){
    my $source=$startscript_skel_lmn_linux."/".$script;
    my $target=$netlogon_school_lmn_linux."/".$script;
    print "   Overwrite lmn/linux: $target\n";
    system("install -oroot -groot --mode=0644 \"$source\" $target");
}

# linux custom
my $netlogon_school_custom_linux=$smb_conf{'samba'}{'smb.conf'}{'netlogon'}{'path'}."/".$school."/custom/linux";
system("mkdir -p \"$netlogon_school_custom_linux\"");
my $startscript_skel_custom_linux="/usr/share/sophomorix/devel/startscript-skel/custom/linux";
my @custom_linux=("logon.sh",
               "logoff.sh",
               "sysstart.sh",
               "sysstop.sh",
              );
foreach my $script (@custom_linux){
    my $source=$startscript_skel_custom_linux."/".$script;
    my $target=$netlogon_school_custom_linux."/".$script;
    if (-f $target){
        print "   Custom linux exists: $target\n";
    } else {
        print "   Create custom/linux: $target\n";
        system("install -oroot -groot --mode=0644 \"$source\" \"$target\"");
    }
} 


# windows lmn
my $netlogon_school_lmn_windows=$smb_conf{'samba'}{'smb.conf'}{'netlogon'}{'path'}."/".$school."/lmn/windows";
system("mkdir -p \"$netlogon_school_lmn_windows\"");
my $startscript_skel_lmn_windows="/usr/share/sophomorix/devel/startscript-skel/lmn/windows";
my @lmn_windows=("logon.bat",
                 "logoff.bat",
                 "sysstart.bat",
                 "sysstop.bat",
                );
foreach my $script (@lmn_windows){
    my $source=$startscript_skel_lmn_windows."/".$script;
    my $target=$netlogon_school_lmn_windows."/".$script;
    print "   Overwrite lmn/windows: $target\n";
    system("install -oroot -groot --mode=0644 \"$source\" $target");
}

# windows custom 
my $netlogon_school_custom_windows=$smb_conf{'samba'}{'smb.conf'}{'netlogon'}{'path'}."/".$school."/custom/windows";
system("mkdir -p \"$netlogon_school_custom_windows\"");
my $startscript_skel_custom_windows="/usr/share/sophomorix/devel/startscript-skel/custom/windows";
my @custom_windows=("logon.bat",
                 "logoff.bat",
                 "sysstart.bat",
                 "sysstop.bat",
                );
foreach my $script (@custom_windows){
    my $source=$startscript_skel_custom_windows."/".$script;
    my $target=$netlogon_school_custom_windows."/".$script;
    if (-f $target){
        print "   Custom windows exists: $target\n";
    } else {
        print "   Create custom/windows: $target\n";
        system("install -oroot -groot --mode=0644 \"$source\" \"$target\"");
    }
} 



######################################################################
# update rolegroup memberships
######################################################################
print "\n";
print "* Updating rolegroups (sophomorix 3.79.3 or later) ...\n";
print "   ... this can take a few minutes ...\n";
system("sophomorix-managementgroup --update-rolegroups > /dev/null");
print "* Updating rolegroups ... done!\n";



######################################################################
# update the schema (If the schema is not there --> DO NOTHING)
######################################################################

# testing if schema needs to be updated
&AD_sophomorix_schema_update($smb_conf{'samba'}{'from_smb.conf'}{'DomainDNS'});

print "\n";


######################################################################
# install python dependencies
######################################################################

# Actually only install diceware, would be better to handle this
# with requirements.txt

system('export PIP_BREAK_SYSTEM_PACKAGES=1; export PIP_ROOT_USER_ACTION=ignore; python3 -m pip install diceware');

print "\n";
print "##### sophomorix-postinst end #####\n";
