#!/usr/bin/perl

# our $cron_loc = 'aisa' ;

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

our $cgipath  = "/home/aisa/libs" ;

use lib '/home/aisa/libs/modules' ;
	
our $htmlpath  = "/var/www/aisadmin.co.za/public_html" ;

use CGI::Carp qw(fatalsToBrowser);
use Date::Calc qw(:all);
use DBI;
use File::Copy;

# perl /usr/lib/cgi-bin/aisa/scripts/cron/sss_aisa_upload_check.pl 1

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

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

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

&today ;

&config_set_vars ;
&config_set_paths ;

&db_open_ro ;
    our $db_ignore_open_close = 1 ; # do one open and one close instead of repeating it
		&check_files ;
	$db_ignore_open_close = 0 ; # do one open and one close instead of repeating it
&db_close_conn ;

print "Done." if $debug ;

exit;

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

sub check_files {
	
	our $table_sss  = "event_quotes sss" ;
	our $table_aisa = "event_quotes" ;
	
	&db_switch_conn('sss');
	&db_min_ro($table_sss,"id,aisa_quote_nr,upload_1,upload_2,upload_3","aisa_quote_nr <> '0' AND ((upload_1 IS NOT NULL AND upload_1 <> '') OR (upload_2 IS NOT NULL AND upload_2 <> '') OR (upload_3 IS NOT NULL AND upload_3 <> ''))",'','') ;
	&db_switch_conn('aisa');
	&db_min_ro($table_aisa,"id,sss_quote_nr,upload_1,upload_2,upload_3","sss_quote_nr <> '0'",'','') ; 
	
	 # # AND ((upload_1 IS NOT NULL AND upload_1 <> '') OR (upload_2 IS NOT NULL AND upload_2 <> '') OR (upload_3 IS NOT NULL AND upload_3 <> ''))
	
	foreach our $event_id (sort {$b <=> $a} keys %{$db{$table_sss}}) {
		
		our $dir_from	= $htmlpath_sss . "/uploads/uploads/$event_id" ;
		our $dir_to		= $htmlpath . "/uploads/uploads/$db{$table_sss}{$event_id}{aisa_quote_nr}" ;
	
		&check_indidual_files("upload_1",$table_sss) ; 
		&check_indidual_files("upload_2",$table_sss) ; 
		&check_indidual_files("upload_3",$table_sss) ; 		
	}

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

sub check_indidual_files {
	
	my ($table_col_name,$table) = @_ ; 
	
	return unless $db{$table}{$event_id}{$table_col_name} ; 
	
	# if ($db{$table}{$event_id}{$table_col_name}) {
	
	my $from_file = $dir_from . '/' . $db{$table}{$event_id}{$table_col_name} ;
	my $to_file = $dir_to . '/' . $db{$table}{$event_id}{$table_col_name} ;

	my $from_file_exists = (-f $from_file) ? 1 : 0 ; 
	my $to_file_exists = (-f $to_file) ? 1 : 0 ; 
	my $to_dir_exists = (-d $dir_to) ? 1 : 0 ; 		
	my $from_dir_exists = (-d $dir_from) ? 1 : 0 ; 
	
	if ($from_file_exists && (!$to_file_exists || !$to_dir_exists)) {
		# $aisa_event_checked{$db{$table_sss}{$event_id}{aisa_quote_nr}} = 1 if $db{$table_sss}{$event_id}{aisa_quote_nr} && $table_sss eq $table ; 
		print "\n common_min_copy_files($dir_from,$dir_to,$db{$table_sss}{$event_id}{$table_col_name})" if $debug ;
		&common_min_copy_files($dir_from,$dir_to,"$db{$table_sss}{$event_id}{$table_col_name}") ;
	}
	
	if (!$db{$table_aisa}{$db{$table_sss}{$event_id}{aisa_quote_nr}}{$table_col_name} && $table eq $table_sss) {
		%i = () ; 
		$i{$table_col_name} = $db{$table}{$event_id}{$table_col_name} ;
		&db_min_upd($table_aisa,"`id`='$db{$table_sss}{$event_id}{aisa_quote_nr}'") ; 
	}
	
} #-------------------------------------------------------------------------------

use db ;
use today ;
use common ;
use cfg_paths ;
