#
# Change Telephon Numbers
#
# in the CSV-file the divider of filed should be ";"
# in the CSV-file this divider of text should be ":"
# text in the CP1251 codepage
use Net::LDAP;
use Net::LDAP::LDIF;
use Encode 'from_to';

my $INTLFILE="sys:\\a.csv";

my $cmd1;
my $cmdt;
my @massiv;
my @am;
my $ms0;

my $flagfound;
my $tres;

my $lname;
my $fname;
my $sname;
my $telnum;

# For LDAP
$HOST = 'XXX.XXX.XXX.XXX';
$PORT = 389;
$SSL_PORT = 636;
$PASSWD   = 'password';
$BASEDN   = "o=org";
$ADMIN= "cn=admin,o=org";
# end for LDAP


$log_dir="SYS:results\\chngtlph";

# Exit if the log directory's not there.
die "Log folder $log_dir does not exist" if (not -d $log_dir);

# The overall log file. Indicidual job logs will have incremental numbers
$log_file="$log_dir\\chngtlph.log";

mylog (" \n", $log_file);
mylog("***************************************************", $log_file);
mylog("                             Start ", $log_file);

open (FL1, "<$INTLFILE") || die("Did not Open Source File !!");
$cmd1="";
$cmd1=readline(FL1);
$cmd1 =~ s/\n//g;

while (not eof(FL1) ) 
{
#    print "$cmd1\n";    
# if first = ";" - skip this string
    if (substr($cmd1,0,1) eq "\;") {
#        print "Found with\;\n";
        ;
    }
    else {
#        print "$cmd1\n";
	$massiv[0]="";
	$massiv[1]="";
	$massiv[2]="";
	$massiv[3]="";
        @massiv=split(/\;/,$cmd1);
	$massiv[0] =~ s/\"//g;
	$massiv[1] =~ s/\"//g;
	$massiv[1] =~ s/\.//g;
	$massiv[2] =~ s/\"//g;
	$massiv[2] =~ s/\.//g;
#        print "ms0=($massiv[0])\n";
#        print "ms1=(".$massiv[1].")\n";
#        print "ms2=(".$massiv[2].")\n";
#        print "ms3=(".$massiv[3].")\n";
	$_ = $massiv[0]; # 
	$_ =~ s| {2,}| |g; #
	$ms0 = $_; # 
	$am[0]="";
	$am[1]="";
	(@am) = split(/ /, $ms0); #
	$lname=$am[1];
	$fname=$am[2];
	$sname=$am[3];
	$telnum=$massiv[1];
	print "+++++++++++++++++\n";
	print "LastName($lname)\n";
	print "FirstName($fname)\n";
	print "SecondName($sname)\n";
	$lnameutf=$lname;
	$lnameutf .= "\ ";	
	$lnameutf .= $fname;
	from_to($lnameutf,"cp1251","utf-8");
	print "UTF($lnameutf)UTF\n";
	$flagfound=0;
# will be serach in edir this user
	$searchstring="(&(objectclass=user)(fullName=*$lnameutf*))";
	$attnames=["dn","cn","telephoneNumber","fullName"];
#connect to the server
	until($ldap = Net::LDAP->new($HOST, port => $PORT)) {
		die "Can not connect to ldap://$HOST:$PORT/" if ++$count > 10;
		sleep 1;
	}
   
	$r = $ldap->bind($ADMIN, password => $PASSWD, version=>3);
	die $r->error if $r->code;
    
	$r = $ldap->search(	base 	=> 	$BASEDN,
				scope	=>	'sub',
				filter 	=> 	$searchstring,
				attrs	=>	$attnames);
#---------------
	$DnTmp="empt";
	$TimeExp=0;
	$TimeTmp=0;
	$lgr=0;
	$lgl=0;
	print "Begin\n";
	foreach $entry ($r->entries){
		my @attrs = $entry->attributes;
		$DnTmp="empt";
		$TimeExp=0;
		$TimeTmp=0;
#		foreach my $attr (@attrs) {
#			my @value = $entry->get_value($attr);
#			foreach my $value (@value){
#				$tmp1=0;
#				$tmp1=$value;
#				from_to($tmp1,"utf-8","cp866");
#				$tmp1="\L$tmp1";
#				print "[$attr]=[$value]\n";
#				print "[$attr]=[$tmp1]\n";
#			}
#		}
		$DnTmp = $entry->dn;
        	$na = $entry->get_value('fullName');
        	$na1=$na;
		from_to($na1,"utf-8","cp1251");
		print "User Dn=[".$DnTmp."]\n";
		print "User Fullname=[".$na1."]\n";
	$r = $ldap->modify($entry->dn, replace => { telephoneNumber => $telnum });
	die $r->error if $r->code;
	$flagfound=1;
#	$r = $ldap->modify($entry->dn, replace => { L => $latm1 });
#	die $r->error if $r->code;


#	$na = $entry->get_value('networkAddress');
#	print "\nUser Na-1=[".$na."]\n";        
#	$na1=LDAPNetAddr($na);
#	print "+++++[".$na1."+++++\n";
#
	$DnTmp="empt";
	$TimeExp=0;
	$TimeTmp=0;
	#	print "</p>\n";
    }
undef($ldap);

#---------------
    }

    if ($flagfound == 0 ) {
	$tres = $lname;
	$tres .= " ";
	$tres .= $fname;
	$tres .= " ";
	$tres .= $sname;
	$tres .= "=";
	$tres .= $massiv[2];
	$tres .= "=";
	mylog ("!!!-NOT FOUND FOR($tres)",$log_file)
	}	
    $cmd1=readline(FL1);
    $cmd1 =~ s/\n//g;
}
mylog("                             END ", $log_file);
mylog("***************************************************\n", $log_file);
print "Press Enter\n";
<>;






sub get_module_status {

  # Check whether a nlm is loaded. Returns 0 if not running, >=1 otherwise
  # Relies on NRM XML files
  #
  # Input:  Name of NLM to test (case sensitive)
  # Outout: Number of instances found (zero if not found)
  
#  my $COUNT=0;
#  my $ETALON=">".$_[0]."<";
#  open nrmfile, "<$nrm_file"  or die "Unable to open $nrm_file. Is NSS loaded?";
#  while (<nrmfile>) {if (/$ETALON/) {++$COUNT}}
#  close nrmfile;
#  return $COUNT;


 my $COUNT=0;
  my $ETALON=">".$_[0]."<";
  my $ff;
  open nrmfile, "<$nrm_file"  or die "Unable to open $nrm_file. Is NSS loaded?";
  while (<nrmfile>) 
    {
      $ff=<nrmfile>;    
      while ($ff =~ /$ETALON/g) {++$COUNT;}
    }
  close nrmfile;
  return $COUNT;


  }


sub check_stop {
  # Check to see whether the 'abort' file exists and exit if it does

  if (-e $stop_file) {
    mylog("STOP file ($stop_file) exists. Terminating.\n", $log_file);
    exit;
    }
  }

sub mylog {

   # log output to screen and (optionally) to file
   # $_[0] is the string to be logged, $_[1] is the logfile

   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime;

   $out=sprintf("%02d",$mday);
   $out.="/";
   $out.=sprintf("%02d",$mon+1);
   $out.="/";
   $out.=1900+$year;
   $out.=" ";
   $out.=sprintf("%02d",$hour);
   $out.=":";
   $out.=sprintf("%02d",$min);
   $out.=":";
   $out.=sprintf("%02d",$sec);
   $out.=" $_[0]";
   $out.="\n";
   print $out;
   if ($_[1]) {
      $LOGFILE=">>" . $_[1];
      open LOGFILE or die "Can't open log file $_[1]: $!";
      print LOGFILE $out;
      close LOGFILE;
      }
   }

