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


# ===========================================================================
# Bibliotheken
# ===========================================================================
use strict;
use Getopt::Long;
Getopt::Long::Configure ("bundling");
use Sophomorix::SophomorixConfig;
use Sophomorix::SophomorixBase;
use Sophomorix::SophomorixAPI;
use Sophomorix::SophomorixPgLdap;
use DBI;
use Net::LDAP;

my @arguments = @ARGV;

# ===========================================================================
# Optionen verarbeiten
# ==========================================================================
# Variablen für Optionen
$Conf::log_level=1;
my $help=0;
my $info=0;
my $class="";
my $reset_class="";
my $hide="";
my $quota;
my $mailquota;
my $mailalias;
my $maillist;
$Conf::log_level=1;

# Parsen der Optionen
my $testopt=GetOptions(
           "help|h" => \$help,
           "info|i" => \$info,
           "class|c=s" => \$class,
           "reset-class=s" => \$reset_class,
           "hide!" => \$hide,
           "quota=s" => \$quota,
           "mailquota=i" => \$mailquota,
           "mailalias!" => \$mailalias,
           "maillist!" => \$maillist,
           "verbose|v+" => \$Conf::log_level,
          );

# Prüfen, ob Optionen erkannt wurden
&check_options($testopt);
&check_connections();

if (not defined $quota 
    and not defined $mailquota
    and $info==0 
    and not defined $mailalias
    and not defined $maillist
    and $class eq ""
    and $reset_class eq ""){
    $help=1;
}


# --help
if ($help==1) {
   # Scriptname ermitteln
   my @list = split(/\//,$0);
   my $scriptname = pop @list;
   # Befehlbeschreibung
   print('
sophomorix-class sets properties of a class in the database

Options
  -h  / --help
  -v  / --verbose
  -vv / --verbose --verbose
  -i  / --info
  -c adminclass, / --class adminclass
    (can set values also for the group teachers with --class teachers)
  --hide --class adminclass, --nohide --class hiddenclass 
  --quota quotastring
  --mailquota number
  --mailalias, --nomailalias
  --maillist, --nomaillist
  --reset-class class

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

############################################################
# config values
############################################################



# --info
if ($info==1 and $reset_class eq ""){
   &show_class_list();
   exit;
}

&log_script_start(@arguments);


# --mailalias
if (defined $mailalias){
    if ($class eq ""){
        &log_script_exit("ERROR: You must give a class to use --mailalias",
                         1,1,0,@arguments);
    }
    if ($mailalias==0){
        &update_class_db_entry($class,"Mailalias=FALSE");
    } elsif ($mailalias==1){
        &update_class_db_entry($class,"Mailalias=TRUE");
    }
}


# --maillist
if (defined $maillist){
    if ($class eq ""){
        print "   You must give a class to use --maillist\n";
        &log_script_exit("ERROR: You must give a class to use --maillist",
                         1,1,0,@arguments);
    }
    if ($maillist==0){
        &update_class_db_entry($class,"Maillist=FALSE");
    } elsif ($maillist==1){
        &update_class_db_entry($class,"Maillist=TRUE");
    }
}


# --hide
if ($hide ne "" and $class ne ""){
    if ($hide==0){
        print "Unhiding class $class\n";
        &update_class_db_entry($class,"Type=adminclass");
    }
    if ($hide==1){
        print "Hiding class $class\n";
        &update_class_db_entry($class,"Type=hiddenclass");
    }
}





# --quota
if (defined $quota){
    if ($class eq ""){
        print "   You must give a class to use --quota\n";
        &log_script_exit("You must give a class to use --quota",
                         1,1,0,@arguments);
    }
    if ($quota eq "" or 
        $quota eq "quota" or 
        $quota eq "standard" or
	$quota eq "default"){
        &update_class_db_entry($class,"Quota=quota");
    } else {
        &update_class_db_entry($class,"Quota=$quota");
    }
}


# --mailquota
if (defined $mailquota){
    if ($class eq ""){
        &log_script_exit("ERROR: You must give a class to use --mailquota",
                         1,1,0,@arguments);
    }
    &update_class_db_entry($class,"MailQuota=$mailquota");
}


# --reset-class
if ($reset_class ne ""){
    if ($reset_class eq ${DevelConf::teacher}){
        &log_script_exit("ERROR: This option cannot be used with $reset_class",
                         1,1,0,@arguments);
    }

    my ($group_type)=&pg_get_group_type($reset_class);

    if ($group_type ne "adminclass"
        and $group_type ne "hiddenclass"){
        print "\nWARNING: Type of ${reset_class}: $group_type\n";
        print   "         The option --reset-class can only be used\n",
                "         with type adminclass/hiddenclass\n\n";
        &log_script_exit("ERROR: option works only with admin/hiddenclass",
                         1,1,0,@arguments);
    }

    print "Type: $group_type\n";

    my @students=&fetchstudents_from_adminclass($reset_class);

    # repair.directories einlesen
    &get_alle_verzeichnis_rechte();
    # fetch permission for all homes
    &fetch_repairhome();

    # remove data from __vorlagen ($tasks_rooms)
    my $command_1="rm -rf ${DevelConf::tasks_classes}/${reset_class}/*";
    my $command_2="rm -rf ${DevelConf::share_classes}/${reset_class}/*";

    if ($info==0){
        print "Removing common stuff:\n";
        print "   $command_1\n";
        system("$command_1");
        print "   $command_2\n";
        system("$command_2");
    } else {
        print "   I would: $command_1\n";
        print "   I would: $command_2\n";
    }

    print "Resetting all users of class $reset_class:\n";
    foreach my $user (@students){
        if ($info==0){
            &reset_user($user);
        } else {
            print "   I would reset $user\n";
        }
    }
    if ($info==1){
        print "\nWARNING: To actually reset a class remove the --info/-i\n\n";
    }
}




# Setting Quota if necessary
if ($Conf::use_quota eq "yes" 
    and $class ne "" 
    and (defined $quota or defined $mailquota)
    ) {
    system("${DevelConf::executable_pfad}/sophomorix-quota --class $class  --noninteractive");
    &nscd_stop();
} else {
    &titel("NOT setting quota (nothing quota related changed)");
}





# Creating Mailing Aliases and Lists
if ($class ne ""
    and (defined $mailalias or defined $maillist)
   ) {
    system("${DevelConf::executable_pfad}/sophomorix-mail");
    &nscd_stop();
} else {
    &titel("NOT creating mailaliases/lists (nothing mail related changed)");
}


# ===========================================================================
# Ende des Scripts
# ===========================================================================
&log_script_end(@arguments);







