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



# Usage:
# set_passwords
#   -> set passwords from user_db (user.protokoll from old server)
#
# set_passwords --kill
#   -> mark accounts as killable where no password can be found in
#      user_db (user.protokoll)


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

use Sophomorix::SophomorixPgLdap qw(show_modulename
             fetchstudents_from_adminclass

                                   );
my ($opt)=@ARGV;
if (not defined $opt){
    $opt="";
}

my @teachers=&fetchstudents_from_adminclass("teachers");

my @students=&fetchstudents_from_school();

my @users=(@teachers,@students);

my $file="/root/sophomorix-vampire/user_db";

my %hash=();

open(FILE, "<$file");
while(<FILE>) {
    chomp();
    my @line=split(/;/);
    #print "$line[2] -> $line[3] \n";
    $hash{$line[2]}="$line[3]";
}
close(FILE);



foreach my $user (@users){
    if (exists $hash{$user}){
       if ($opt eq "--kill"){
           next;
       }
       my $command="sophomorix-passwd -u $user --pass $hash{$user}";
       print "$command \n";
       system("$command");
   } else {
       # revove this accounts when option --kill
       print "\nnopw for $user\n\n";
       if ($opt eq "--kill"){
          my $command="sophomorix-user -K $user";
          print "$command \n";
          system("$command");
       }
   }
}

