=pod
 Usage              : aleph23_mft_patch_script_00.pl 
 Pre Requisites     : The following 2 files must be on the scripts' directory
                     (used as input):
                     - 'aleph23_mft_patch_files.tar.gz' - tar.gz of the patched files and programs.
                     - 'aleph23_mft_patch_file_list.txt' - a list of patched files and programs

 Output of this script:
                     - 'aleph23_mft_patch.log' - Log of the actions done by this script.
                     - 'aleph23_mft_patch_revert_files.log' - Log to enable easy way to revert and bring back the previous files / programs.

=cut
#########################################################
use File::Basename; 
use File::Copy; 

use File::Slurp; 
use File::Spec; 
use IO::All; 
use POSIX 'strftime'; 
use Cwd qw(); 


########################################

########################################
# Start log 
my $date_time = strftime '%Y%m%d%H%M', localtime;  

my $log_file = 'aleph23_mft_patch.log'; 

#### Move aside old logs - if exist. 
if (-e $log_file) { 
  my $log_file_backup = "$log_file.$date_time"; 
  print "Moving existing log file '$log_file' to $log_file_backup\n"; 
  rename $log_file,$log_file_backup ;
} 

my $revert_files_log_file = 'aleph23_mft_patch_revert_files.log'; 
if ( -e $revert_files_log_file ) {
  my $revert_files_log_file_backup = "$revert_files_log_file.$date_time"; 
  print "Moving existing log file '$revert_files_log_file' to $revert_files_log_file_backup\n\n\n"; 
} 

### Open log files. 
open(my $log_fh, '>', $log_file) 
  or die "Could not create the log '$log_file' $!"; 


open(my $revert_files_log_fh, '>', $revert_files_log_file) 
  or die "Could not create the log '$revert_files_log_file' $!"; 

print $log_fh "\$date_time $date_time \n\n"; 

### 
print "\n\n"; 
print "==================================================\n"; 
print " 0.  Updating 'util SP' program - files . \n"; 
print "==================================================\n"; 

print $log_fh "------------------------------\n"; 
print $log_fh "0. Updating 'util SP' program - files . \n"; 
print $log_fh "------------------------------\n\n\n"; 

#
########################################

########################################
# Current dir
my $orig_path = Cwd::cwd(); 
print $log_fh "----------------------------------------\n"; 
print $log_fh "$orig_path\n\n"; 

my $aleph_dev   = $ENV{'aleph_dev'};    print $log_fh "$aleph_dev\n"; 
my $alephm_proc = $ENV{'alephm_proc'};  print $log_fh "$alephm_proc\n"; 

# Get Aleph Version 
my $aleph_version = $ENV{'ALEPH_VERSION'}; print $log_fh "ALEPH_VERSION $aleph_version\n"; 
print "ALEPH_VERSION $aleph_version\n"; 
print $log_fh "ALEPH_VERSION $aleph_version\n"; 

#
########################################

########################################
## files
my $list_of_files = 'aleph23_mft_patch_file_list.txt';

my @names_of_files = read_file( "$list_of_files" ) ;

my $file_name = ''; # The program changed.
my $full_path = '';

##
########################################

########################################
# Backup.
my $f_num = "0"; # Count number of files 

foreach  $file_name (@names_of_files) { 
    chomp $file_name; 
    print $log_fh  "$file_name\n"; print "$file_name\n"; 
    $full_path = "$aleph_dev"."$file_name";
    # print "DEBUG $full_path\n";

    $full_path =~ s|\./aleph|/aleph|; 
    # print "DEBUG $full_path\n"; 

    if (-e $full_path) { 
         ## print " Back up : $full_path to: $file_name.$date_time\n"; 
         print $log_fh "Copied $full_path to $full_path.$date_time\n"; # print to log
         print $revert_files_log_fh " mv $full_path.$date_time $full_path\n"; # Log for reverting 

         ##### HERE'S COMES THE COPY
         copy("$full_path","$full_path.$date_time") or die "Copy failed: $!"; 
    } 

    $f_num = $f_num +1 ; 
}
# End backup       #####################
########################################


print $log_fh "------------------------------\n"; 
print $log_fh "----- Backup ended -----------\n";
print $log_fh "--- Backed up $f_num files ---\n";
print $log_fh "------------------------------\n"; 
print $log_fh "------------------------------\n\n"; 

# print $log_fh "------------------------------\n"; 
# print $log_fh "\n\n\n\n\n"; print "\n\n\n\n\n"; 



print "------------------------------\n"; 
print "----- Backup ended -----------\n";
print "--- Backed up $f_num files ---\n";
print "------------------------------\n"; 
print "------------------------------\n\n"; 

# print "------------------------------\n"; 
# print "\n\n\n\n\n"; print "\n\n\n\n\n"; 

########################################
########################################
## Extract new sources from the tar.gz

my $tar_gz_filename = 'aleph23_mft_patch_files.tar.gz'; 
die "The file $tar_gz_filename does not exist, and I can't go on without it.\n\n" unless -e $tar_gz_filename; 

my @names_of_files_in_tar_gz = `tar -tz -f aleph23_mft_patch_files.tar.gz` ; 

print $log_fh "------------------------------\n";
print "Extracting files from: $tar_gz_filename\n";
print $log_fh "Extracting files from: $tar_gz_filename\n\n";

## `tar -tz -f aleph23_mft_patch_files.tar.gz`; 
## print `tar -xvzf aleph23_mft_patch_files.tar.gz -C $aleph_dev`; 
print $log_fh `tar -xvzf aleph23_mft_patch_files.tar.gz -C $aleph_dev`; 

print $log_fh "---- Extract tar.gz ended ---\n"; 

print $log_fh "------------------------------\n";
print $log_fh "------------------------------\n\n";
# End extract new sources from the tar.gz
#########################################

# print "\n\n";
print "\t\t\t----> Extract tar.gz ended <---\n"; 

## print "\n\n\n";
print $log_fh "\n\n\n"; 

########################################
### Close log 
close $log_fh; 
close $revert_files_log_fh; 

### End close log
########################################


