#!/usr/bin/perl -w
# $Id$
# This script (sophomorix-room) 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 $reset_room="";
my $reset_all_rooms=0;
$Conf::log_level=1;

# Parsen der Optionen
my $testopt=GetOptions(
           "help|h" => \$help,
           "info|i" => \$info,
           "reset-room=s" => \$reset_room,
           "reset-all-rooms" => \$reset_all_rooms,
           "verbose|v+" => \$Conf::log_level,
          );

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


if ($reset_room eq ""
    and $info==0
    and $reset_all_rooms==0){
    $help=1;
}



# --help
if ($help==1) {
   # Scriptname ermitteln
   my @list = split(/\//,$0);
   my $scriptname = pop @list;
   # Befehlbeschreibung
   print('
sophomorix-room shows rooms and resets the workstation accounts

Options
  -h  / --help
  -v  / --verbose
  -vv / --verbose --verbose
  -i  / --info
  --reset-room room
  --reset-all-rooms
Please see the sophomorix-room(8) man pages for full documentation
');
   print "\n";
   exit;
}

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

&check_connections();


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

&log_script_start(@arguments);


# --reset-room
if ($reset_room ne ""){
    print "Resetting room ${reset_room}:\n";
    my @workstations=&fetchworkstations_from_room($reset_room);
    # repair.directories einlesen
    &get_alle_verzeichnis_rechte();
    # fetch permission for all homes
    &fetch_repairhome();
    foreach my $ws (@workstations){
	print "   Resetting $ws\n";
        &reset_user($ws);
    }

    # remove data from __vorlagen ($tasks_rooms)
    my $command="rm -rf ${DevelConf::tasks_rooms}/${reset_room}/*";
    print "Removing common ${Language::task_dir} in room ${reset_room}:\n";
    print "   $command\n";
    system("$command");
}



# --reset-all-rooms
if ($reset_all_rooms==1){
    print "Resetting all rooms:\n";
    my @workstations=&fetchworkstations_from_school();
    # repair.directories einlesen
    &get_alle_verzeichnis_rechte();
    # fetch permission for all homes
    &fetch_repairhome();
    foreach my $ws (@workstations){
	print "   Resetting $ws\n";
        &reset_user($ws);
    }
    
    # remove data from __vorlagen
    my @rooms=&fetchrooms_from_school();
    foreach my $room (@rooms){
        my $command="rm -rf ${DevelConf::tasks_rooms}/${room}/*";
        print "Removing common ${Language::task_dir} in room ${room}:\n";
        print "   $command\n";
        system("$command");
    }
}




&log_script_end(@arguments);
