#!/usr/bin/perl

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

print "Content-type: text/html\n\n";

use CGI::Carp qw(fatalsToBrowser);
use Fcntl qw(:flock);

require cfg ;

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

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

foreach $ARG (@ARGV) { ($par,$val) = split(/\=/,$ARG) ; $val  =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $param{$par} = $val ; }

our $debug = $ARGV[0] ;

# e.g. https://itvadmin.co.za/cgi-bin/scripts/get/get_correct_camera_nr.pl?1
# our $debug = 1 ;

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

&load_json ;

exit ;

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

sub load_json {
	
	&db_min_ro('cameras_legacy','camera_nr,id,quote_nr,quote_cam_num','','','') ; 
	
	# my %seen_quote_nr = () ; my %max_quote_cam_num = () ;
	
	# foreach (sort {$db{cameras_legacy}{$b}{quote_cam_num} <=> $db{cameras_legacy}{$a}{quote_cam_num}} keys %{$db{cameras_legacy}}) {
			
		# # print "\n quote_nr : $db{cameras_legacy}{$_}{quote_nr} , $db{cameras_legacy}{$_}{quote_cam_num}" if $db{cameras_legacy}{$_}{quote_nr} eq '1236' ;
		# next if $seen_quote_nr{$db{cameras_legacy}{$_}{quote_nr}} ;
		# $seen_quote_nr{$db{cameras_legacy}{$_}{quote_nr}} = 1 ;
		# $max_quote_cam_num{$db{cameras_legacy}{$_}{quote_nr}} = $db{cameras_legacy}{$_}{quote_cam_num} ; 
	
	# }
	
	my $quote_cam_num = 0 ; my %abc = () ; my %cam_nr_quote_nr = () ; 
	
	&db_min_ro('cameras','camera_nr,id,quote_nr','','','') ; 
	
	foreach (sort {$a cmp $b} keys %{$db{cameras}}) {	
		next unless $db{cameras}{$_}{quote_nr} ;
		my @split_cam = split("-",$_) ; 
		my $count2 = 0 ;
		$count2 = 1 if substr($_,0,5) eq 'Score' or substr($_,0,2) eq 'GP' ;
		$abc{$db{cameras}{$_}{quote_nr}}{$split_cam[-1]}{$count2}{$_} = 1 ;
		# print "\n quote_nr : $db{cameras}{$_}{quote_nr} , camera_nr : $_" ;
		$cam_nr_quote_nr{$_} = $db{cameras}{$_}{quote_nr} ; 
	}
	
	foreach my $quote_nr (sort {$a cmp $b} keys %abc) {
		$quote_cam_num = 0 ;
		foreach my $count (sort {$a cmp $b} keys %{$abc{$quote_nr}}) {
			foreach my $count2 (sort {$a cmp $b} keys %{$abc{$quote_nr}{$count}}) {
				foreach my $camera_nr (sort {$a cmp $b} keys %{$abc{$quote_nr}{$count}{$count2}}) {
					
					my $other_cam_nr1 = substr($camera_nr,3) ;
					my $other_cam_nr2 = substr($camera_nr,5) ;
					my $other_cam_nr3 = "VPU$camera_nr" ;

					next if substr($camera_nr,0,3) eq 'VPU' and $cam_nr_quote_nr{$other_cam_nr1} eq $quote_nr ;
					next if substr($camera_nr,0,5) eq 'Score' and $cam_nr_quote_nr{$other_cam_nr2} eq  $quote_nr ;
					
					%i = () ;
					$quote_cam_num++ ; 
					$i{quote_cam_num} = $quote_cam_num ;
					# next if $i{quote_cam_num} eq $db{cameras}{$camera_nr}{quote_cam_num} and $db{cameras}{$camera_nr}{quote_nr} eq $quote_nr ;
					print "\nUPDATE `cameras` SET `quote_cam_num` = '$i{quote_cam_num}' WHERE `camera_nr` = '$camera_nr'\t AND `quote_nr` = '$quote_nr' ; " ;				
					# &db_min_upd('cameras',"`camera_nr` = '$camera_nr' AND `quote_nr` = '$quote_nr'") ; 
					
					if ((substr($camera_nr,0,1) eq 'S' or substr($camera_nr,0,1) eq 'C') and $cam_nr_quote_nr{$other_cam_nr3} eq $quote_nr) {		 
						%i = () ;
						$quote_cam_num++ ; 
						$i{quote_cam_num} = $quote_cam_num ;	
						print "\nUPDATE `cameras` SET `quote_cam_num` = '$i{quote_cam_num}' WHERE `camera_nr` = 'VPU$camera_nr'\t AND `quote_nr` = '$quote_nr' ; " ;
						# &db_min_upd('cameras',"`camera_nr` = 'VPU$camera_nr' AND `quote_nr` = '$quote_nr'") ; 
					 }
					 
					 if (substr($camera_nr,0,3) eq 'GP-') {
						 
						%i = () ;
						$quote_cam_num++ ; 
						$i{quote_cam_num} = $quote_cam_num ;			
						$camera_nr = "Score" . substr($camera_nr,2) ;
						# &db_min_upd('cameras',"`camera_nr` = '$camera_nr' AND `quote_nr` = '$quote_nr'") ; 
						print "\nUPDATE `cameras` SET `quote_cam_num` = '$i{quote_cam_num}' WHERE `camera_nr` = '$camera_nr'\t AND `quote_nr` = '$quote_nr' ; " ;				 
						 
					 }
				}
			}
		}
	}
	
	# foreach (sort {$a cmp $b} keys %{$db{cameras}}) {
		
		# %i = () ;
		
		# $quote_cam_num++ ; 
		
		# $i{quote_cam_num} = $quote_cam_num ;
		
		# # if (not $db{cameras_legacy}{$_}{quote_nr} and $db{cameras}{$_}{quote_nr}) {		
			# # $max_quote_cam_num{$db{cameras}{$_}{quote_nr}}++ ;		
			# # $i{quote_cam_num} = $max_quote_cam_num{$db{cameras}{$_}{quote_nr}} ;
	
		# # } elsif ($db{cameras_legacy}{$_}{quote_cam_num} and $db{cameras}{$_}{quote_nr} and not $db{cameras}{$_}{quote_cam_num}) {			
			# # $i{quote_cam_num} = $db{cameras_legacy}{$_}{quote_cam_num} ;			
		# # } else {
			# # next ;
		# # }
		
		# print "\nUPDATE `cameras` SET `quote_cam_num` = '$i{quote_cam_num}' WHERE `camera_nr` = '$_' AND `quote_nr` = '$db{cameras}{$_}{quote_nr}'" ;
		# # &db_min_upd('cameras',"`camera_nr` = '$_' AND `quote_nr` = '$db{cameras_legacy}{$_}{quote_nr}'") ; 
		
	# }
	
} #------------------------------------------------------------------------------------------       

use db ;
use common ;

1;