#!/usr/bin/perl

BEGIN { use lib '/usr/home/cfg' ; require push_inc ; }
    
require cfg ;

use CGI::Carp qw(fatalsToBrowser);
use CGI;
use Crypt::PasswdMD5;


&today ;

#------------------------------------------------------------------------------------------

@ARGV = split(/\\*\&/, $ENV{'QUERY_STRING'});

foreach $ARG (@ARGV) { ($par,$val) = split(/\=/,$ARG) ; $param{$par} = $val ; }
	
# https://itvadmin.co.za/cgi-bin/scripts/admin/oneoff/update_user_password_from_excel.pl?1

our $debug    = $ARGV[0] ;
our $username = 'rory' ;

# print "\n debug : $debug \n" ; 

if ($debug) { print "Content-type: text/html\n\n"; }

&common_debug("\n 1. START") ; 

#------------------------------------------------------------------------------------------

&today ;
&common_debug("\n 2. START") ; 
# &set_delete_date ;
&read_csv ;

if ($debug) { print "Done."; }

exit;


#------------------------------------------------------------------------------------------

sub read_csv {
	
	
	# my $crypted_password = apache_md5_crypt($i{password},$useropts{salt}) ;
	
	my $file_path = "$htmlpath/csv/operator_list_csv.csv"  ;
	
	&common_debug("file_path : $file_path") if -f $file_path ;
	
	# Open the file for reading

	unless (open(CSV_FILE, '<', $file_path)) {
		die "Unable to open the file '$file_path' for reading: $!";
	}

	my $header_line = <CSV_FILE> ; my @fields = () ; 

	while (my $line = <CSV_FILE>) {
 
		@fields = () ; @fields = split(',',$line) ; 
		
		my $id = $fields[0] ; 
		my $password = $fields[5] ;
		$password =~ s/\n//g; $password =~ s/ //g;
		my $length = length ($password) ;
		$password = substr($password,0,$length-1) ;
		
		my $crypted_password = apache_md5_crypt($password,$useropts{salt}) ;
		
		&common_debug("UPDATE `users` SET `password` = '$crypted_password' WHERE `id` = '$id' ;") ;
		
		# # $fields[4] =~ s\/'\\gi ;	
		# $fields[0] =~ s\/'\\gi ;	
		
		# $fields[3] =~ s/\n//g;

		# my $region_id = 0 ; my $city_id = 0 ; 
		# $region_id = 4 if $fields[3] eq 'KZN' ; 
		# $region_id = 9 if $fields[3] eq 'CPT' ; 
		# $region_id = 2 if $fields[3] eq 'Bloem' ; 
		# $region_id = 3 if $fields[3] eq 'JHB' ; 
		
		# $city_id = 1 if $fields[3] eq 'JHB' ; 
		# $city_id = 10 if $fields[3] eq 'CPT' ; 
		# $city_id = 20 if $fields[3] eq 'Bloem' ; 
		
		# print "\n;UPDATE `users` SET `region_ids` = '$region_id', `city_id` = '$city_id' WHERE region_ids = '0' AND `name` LIKE '$fields[0]%' AND `name` LIKE '%$fields[1]'" ;
		
		# print "\n ;UPDATE `users` SET email = '$fields[2]' WHERE region_ids = '0' AND name LIKE '$fields[0]%' AND name LIKE '%$fields[1]' AND (`email` = '' OR `email` IS NULL) ;" ;

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

use db ;
use today ;
use common ;
use common_min ;
