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

Output of this script:
                     - 'arc_3rd_party_prods_patch.log' - Log of the actions done by this script.
                     - 'arc_3rd_party_prods_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 = 'arc_3rd_party_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 = 'arc_3rd_party_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 $arc_dev   = $ENV{'arc_dev'};    print $log_fh "$arc_dev\n";
my $arcm_proc = $ENV{'arcm_proc'};  print $log_fh "$arcm_proc\n";

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

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

########################################
## files

my $list_of_files = 'arc_3rd_party_mft_util_sp_files_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

########################################
# 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 = "$arc_dev"."$file_name";
    # print "DEBUG $full_path\n";

    $full_path =~ s|\./arc|/arc|;
    # 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 = 'arc_3rd_party_util_sp_files_mft.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 arc_3rd_party_util_sp_files_mft.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 arc_3rd_party_util_sp_files_mft.tar.gz`;
## print `tar -xvzf arc_3rd_party_util_sp_files_mft.tar.gz -C $arc_dev`;
print $log_fh `tar -xvzf arc_3rd_party_util_sp_files_mft.tar.gz -C $arc_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
########################################

