#!/usr/bin/perl -w
use strict;
use Getopt::Long;
Getopt::Long::Configure ("bundling");

my $script="";
my $option="";
my $help=0;

my $testopt=GetOptions(
           "script|s=s" => \$script,
           "option|o=s" => \$option,
           "help|h" => \$help,
          );

if ($help==1) {
   # Scriptname ermitteln
   my @list = split(/\//,$0);
   my $scriptname = pop @list;
   # Befehlsbeschreibung
   print('
sophomorix-supertest runs multiple tests
Options:
  -h  / --help

Preparation:
  -s r     reset samba, configfiles, shares to installation default
  -s p     prepare samba for the following tests

  -s rp    reset+prepare 

Run tests:
  -s all    reset an run all tests

  -s 1      reset samba and run sophomorix-test-1
  -s 2      reset samba and run sophomorix-test-2
  -s 3      reset samba and run sophomorix-test-3
  -s 4      reset samba and run sophomorix-test-4
  -s 5      reset samba and run sophomorix-test-5
  -s w      reset samba and run sophomorix-test-workflow-schoolisolation
  -s er     reset samba and run sophomorix-test-errorfiles
  -s ex     reset samba and run sophomorix-test-exammode

  -s rep1   reset samba and run sophomorix-test-repair1
  -s rep2   reset samba and run sophomorix-test-repair2
  -s rep3   reset samba and run sophomorix-test-repair3
  -s rep4   reset samba and run sophomorix-test-repair4
  -s rep5   reset samba and run sophomorix-test-repair5


  -o j    run tests with option -j
  -o jj   run tests with option -jj
  -o vv   run tests with option -vv
  -o n    run tests with option --ntacl
');
   print "\n";
   exit;
}



my @scriptlist=();

if ($option ne ""){
    $option="-".$option;
}


if ($script eq "r"){
    &reset_smb();
} elsif ($script eq "p"){
    &prepare_smb();
} elsif ($script eq "rp"){
    &reset_smb();
    &prepare_smb();
} elsif ($script eq "all"){
    @scriptlist=("sophomorix-test-1 $option ", 
                 "sophomorix-test-2 $option ", 
                 "sophomorix-test-3 $option ", 
                 "sophomorix-test-4 $option ", 
                 "sophomorix-test-5 $option ", 
                 "sophomorix-test-workflow-schoolisolation $option ",
                 "sophomorix-test-errorfiles $option ",
                 "sophomorix-test-exammode $option ",
                 );
} elsif ($script eq "1"){
    @scriptlist=("sophomorix-test-1 $option ");
} elsif ($script eq "2"){
    @scriptlist=("sophomorix-test-2 $option ");
} elsif ($script eq "3"){
    @scriptlist=("sophomorix-test-3 $option ");
} elsif ($script eq "4"){
    @scriptlist=("sophomorix-test-4 $option ");
} elsif ($script eq "5"){
    @scriptlist=("sophomorix-test-5 $option ");
} elsif ($script eq "w"){
    @scriptlist=("sophomorix-test-workflow-schoolisolation $option ");
} elsif ($script eq "er"){
    @scriptlist=("sophomorix-test-errorfiles $option ");
} elsif ($script eq "ex"){
    @scriptlist=("sophomorix-test-exammode $option ");
} elsif ($script eq "rep1"){
    @scriptlist=("sophomorix-test-repair1 $option ");
} elsif ($script eq "rep2"){
    @scriptlist=("sophomorix-test-repair2 $option ");
} elsif ($script eq "rep3"){
    @scriptlist=("sophomorix-test-repair3 $option ");
} elsif ($script eq "rep4"){
    @scriptlist=("sophomorix-test-repair4 $option ");
} elsif ($script eq "rep5"){
    @scriptlist=("sophomorix-test-repair5 $option ");
}



# run the scripts
foreach my $script (@scriptlist){
    my (@parts)=split(/ /,$script);
    my $command=$script."--full 1> /tmp/".$parts[0].".log 2> /tmp/".$parts[0].".error";
    &reset_smb();
    &prepare_smb();
    print "\n";
    &printline();
    print "TEST: $command \n";
    system($command);
    print "\n";
}



# typeout
foreach my $script (@scriptlist){
    my (@parts)=split(/ /,$script);
    my $logfile_abs="/tmp/".$parts[0].".log";
    my $errfile_abs="/tmp/".$parts[0].".error";
    &printline();
    print "TEST: $script \n";
    &printline();
    my $res1_command="tail -n 3 $logfile_abs";
    print "SHOW STDOUT: $res1_command\n";
    system($res1_command);
    &printline();
    my $res2_command="cat $errfile_abs | grep -v \"Domain=\" | grep -v \"OS=\" | grep -v \"Server=\"";
    print "SHOW ERRORS: $res2_command\n";
    system($res2_command);
    &printline();
    my $password_hits_log= `grep \`cat /etc/linuxmuster/.secret/administrator\` $logfile_abs | wc -l`;
    chomp($password_hits_log);
    my $password_hits_err= `grep \`cat /etc/linuxmuster/.secret/administrator\` $errfile_abs | wc -l`;
    chomp($password_hits_err);
    print "$password_hits_log password hits in $logfile_abs (should be 0)\n";
    print "$password_hits_err password hits in $errfile_abs (should be 0)\n";
    print "\n";
}



sub prepare_smb {
    print "Preparing samba for test ... \n";
    my $templates="/usr/share/sophomorix-developer/testtemplates";
    my $config="/etc/linuxmuster/sophomorix";

    # webui permissions test file
    system("install -d -m755 -oroot -groot /usr/lib/linuxmuster-webui/etc/");
    system("cp /usr/share/sophomorix/config-templates/test-ui-perm.ini /usr/lib/linuxmuster-webui/etc/default-ui-permissions.ini");


    # sophomorix.conf
    system("install -d -m755 -oroot -groot $config");
    system("install -v -oroot -groot --mode=0644 $templates/sophomorix.conf.template.ini.test $config/sophomorix.conf");
    # share for global exists

    # abc.school.conf 
    system("install -d -m755 -oroot -groot $config/abc");
    system("install -v -oroot -groot --mode=0644 $templates/abc.school.conf.template.ini.test $config/abc/abc.school.conf");
    system("mkdir -p /srv/samba/schools/abc");
    system("net  conf addshare abc /srv/samba/schools/abc writeable=y guest_ok=y 'Share for school abc'");

    # bsz.school.conf 
    system("install -d -m755 -oroot -groot $config/bsz");
    system("install -v -oroot -groot --mode=0644 $templates/bsz.school.conf.template.ini.test $config/bsz/bsz.school.conf");
    system("mkdir -p /srv/samba/schools/bsz");
    system("net  conf addshare bsz /srv/samba/schools/bsz writeable=y guest_ok=y 'Share for school bsz'");

    # school.conf
    system("install -d -m755 -oroot -groot $config/default-school");
    system("install -v -oroot -groot --mode=0644 $templates/school.conf.template.ini.test $config/default-school/school.conf");
    # share exists

    # ghs.school.conf
    system("install -d -m755 -oroot -groot $config/ghs");
    system("install -v -oroot -groot --mode=0644 $templates/ghs.school.conf.template.ini.test $config/ghs/ghs.school.conf");
    system("mkdir -p /srv/samba/schools/ghs");
    system("net  conf addshare ghs /srv/samba/schools/ghs writeable=y guest_ok=y 'Share for school ghs'");

    # lin.school.conf
    system("install -d -m755 -oroot -groot $config/lin");
    system("install -v -oroot -groot --mode=0644 $templates/lin.school.conf.template.ini.test $config/lin/lin.school.conf");
    system("mkdir -p /srv/samba/schools/lin");
    system("net  conf addshare lin /srv/samba/schools/lin writeable=y guest_ok=y 'Share for school lin'");

    # uni.school.conf
    system("install -d -m755 -oroot -groot $config/uni");
    system("install -v -oroot -groot --mode=0644 $templates/uni.school.conf.template.ini.test $config/uni/uni.school.conf");
    system("mkdir -p /srv/samba/schools/uni");
    system("net  conf addshare uni /srv/samba/schools/uni writeable=y guest_ok=y 'Share for school uni'");

    # quotacheck
    system("quotaoff -a");
    system("quotacheck -cvugafR");
    system("quotaon -a");
}



sub reset_smb {
    &printline();
    print "Resetting samba for test ... \n";
    # clear scchool gpo's
    system("sophomorix-school --gpo-kill abc");
    system("sophomorix-school --gpo-kill bsz");
    system("sophomorix-school --gpo-kill default-school");
    system("sophomorix-school --gpo-kill ghs");
    system("sophomorix-school --gpo-kill lin");
    system("sophomorix-school --gpo-kill uni");
    # recreate shares
    system("net conf delshare linuxmuster-global");
    system("net conf delshare abc");
    system("net conf delshare bsz");
    system("net conf delshare default-school");
    system("net conf delshare ghs");
    system("net conf delshare lin");
    system("net conf delshare uni");
    system("rm -rf /etc/linuxmuster/sophomorix");
    system("sophomorix-postinst");
    system("net conf addshare linuxmuster-global /srv/samba/global writeable=y guest_ok=y 'Share for school global'");
    system("net conf addshare default-school /srv/samba/schools/default-school writeable=y guest_ok=y 'Share for school default-school'");
    system("sophomorix-samba --restore-samba without-sophomorix-schema");
    system("sophomorix-samba --schema-load");
    system("rm -rf /srv/samba");
    sleep 5;
    # resetting passwords
    system("samba-tool user setpassword administrator --newpassword `cat /etc/linuxmuster/.secret/administrator`");
    system("samba-tool domain passwordsettings set --complexity=default");
    system("samba-tool domain passwordsettings set --min-pwd-length=default");
    # removing old log files
    system("rm -f /var/log/sophomorix/user/*");
    system("rm -f /var/log/sophomorix/command.log");
    system("rm -f /var/log/sophomorix/user-add.log");
    system("rm -f /var/log/sophomorix/user-update.log");
    system("rm -f /var/log/sophomorix/user-kill.log");
    system("rm -rf /var/log/sophomorix/userlog");
    system("rm -rf /var/log/sophomorix/grouplog");
    system("rm -rf /var/log/sophomorix/newfiles");
    system("rm -rf /var/log/sophomorix/etcfiles");
    system("rm -rf /var/log/sophomorix/user-modify-archive.log.migrated");
    system("rm -rf /var/log/sophomorix/user-modify-archive.log.migrated.orig");
    # removing old lib files
    system("rm -f /var/lib/sophomorix/check-result/*");
    system("rm -f /var/lib/sophomorix/tmp/*");
    system("rm -f /var/lib/sophomorix/print-data/*");
    system("rm -rf /var/lib/sophomorix/tmp_collect");
    system("rm -rf /var/lib/sophomorix/tmp_pwdupdate");
    print "... done resetting samba for test\n";
    # quotacheck on global and default-school
    system("quotaoff -a");
    system("quotacheck -cvugafR");
    system("quotaon -a");
    &printline();
}



sub printline {
    print "######################################################################\n";
}
