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

 Output of this script:
                     - 'aleph22_mft_patch.log' - Log of the actions done by this script.
                     - 'aleph22_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();
use strict;
use Fcntl ':mode';

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

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

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

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

########################################
# 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";

my $date_time = strftime '%Y%m%d%H%M', localtime;
print $log_fh "\$date_time $date_time \n\n";

my $aleph_version = $ENV{'ALEPH_VERSION'}; print $log_fh "ALEPH_VERSION $aleph_version\n";
print "ALEPH_VERSION $aleph_version\n";

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

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

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

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


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


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

my $tar_gz_filename = 'aleph22_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 aleph22_mft_patch_files.tar.gz` ;

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

## `tar -tz -f aleph22_mft_patch_files.tar.gz`;
## print `tar -xvzf aleph22_mft_patch_files.tar.gz -C $aleph_dev`;
print $log_fh `tar -xvzf aleph22_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\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";


########################################
# From v. 23 AIN: M4021/implement_02.pl (M2514 in V. 22)
########################################

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

#####
print "\n\n";
print "==================================================\n";
print "2. Handling /exlibris/aleph/.ssh/ . \n";
print "   put SSH key files\n";
print "==================================================\n";
print "\n\n";

print $log_fh "------------------------------\n";
print $log_fh "2. Handling /exlibris/aleph/.ssh/ . \n";
print $log_fh "   put SSH key files\n";
print $log_fh "------------------------------\n\n\n";

#####

#####

my $ssh_key_file_22 = "aleph_22_sp_mft";
my $ssh_key_file_22_pub = "aleph_22_sp_mft.pub";

# my $src_dir_in_sp_ain = "$ENV{'uk_root'}/conf/general/$step_number";
# my $ssh_key_file_22_in_ain = "$src_dir_in_sp_ain/aleph_22_sp_mft";
# my $ssh_key_pub_file_22_in_ain = "$src_dir_in_sp_ain/aleph_22_sp_mft.pub";


print "$orig_path \n\n";
print $log_fh "$orig_path \n\n";
my $ssh_key_file_22_in_orig_path = "$orig_path/aleph_22_sp_mft";
my $ssh_key_pub_file_22_in_orig_path = "$orig_path/aleph_22_sp_mft.pub";

my $aleph_ssh_dir = '/exlibris/aleph/.ssh';
my $ssh_key_file_22_in_dir = "$aleph_ssh_dir/aleph_22_sp_mft";
my $ssh_key_pub_file_22_in_dir = "$aleph_ssh_dir/aleph_22_sp_mft.pub";


## See the Perl operators list:
## https://perldoc.perl.org/functions/-X.html

if ( -d $aleph_ssh_dir){
  print "INFO Directory exists. \n";
  print $log_fh "INFO Directory exists. \n";

  if ( -w $aleph_ssh_dir){  # Directory exists + writable
    print "INFO Directory writable. :) \n";
    print $log_fh "INFO Directory writable. :) \n";

    if ( -x $aleph_ssh_dir){
      print "INFO Directory is already Executable. :) \n";
      print $log_fh "INFO Directory is already Executable. :) \n";
    }
  }
  elsif ( !-w $aleph_ssh_dir || !-x $aleph_ssh_dir) { # Exists but not writable
    chmod(0700, $aleph_ssh_dir) or die "Could not chmod $aleph_ssh_dir : $!";
    print "INFO Made the Directory Executable. \n";
    print $log_fh "INFO Made the Directory Executable. \n";
  }
}
elsif ( !-d $aleph_ssh_dir){ # Not Exist.
  print "INFO Directory DOES _NOT_ exist. \n";
  print "INFO Creating directory. \n";
  print $log_fh "INFO Directory DOES _NOT_ exist. \n";
  print $log_fh "INFO Creating directory. \n";

  mkdir $aleph_ssh_dir ;

  chmod(0700, $aleph_ssh_dir) or die "Could not chmod $aleph_ssh_dir : $!";
}


if (-e $ssh_key_file_22_in_dir && -f _ && -r _ ){
  print "INFO File $ssh_key_file_22_in_dir exists and readable\n";
  print $log_fh "INFO File $ssh_key_file_22_in_dir exists and readable\n";
}

if (-e $ssh_key_pub_file_22_in_dir && -f _ && -r _ ){
  print "INFO File $ssh_key_pub_file_22_in_dir exists and readable\n";
  print $log_fh "INFO File $ssh_key_pub_file_22_in_dir exists and readable\n";
}

if (!-e $ssh_key_file_22_in_dir){
  copy ($ssh_key_file_22_in_orig_path, $ssh_key_file_22_in_dir) or die "Copy failed: $!" ;
  print "INFO Put: Copied SSH key file '$ssh_key_file_22_in_dir'  \n"; #
  print $log_fh "INFO Put: Copied SSH key file '$ssh_key_file_22_in_dir'  \n";
}

if (!-e $ssh_key_pub_file_22_in_dir){
  copy ($ssh_key_file_22_in_orig_path, $ssh_key_pub_file_22_in_dir) or die "Copy failed: $!" ;
  print "INFO Put: Copied SSH key '.PUB' file '$ssh_key_pub_file_22_in_dir)'  \n"; #
  print $log_fh "INFO Put: Copied SSH key '.PUB' file '$ssh_key_pub_file_22_in_dir)'  \n";
}

if (-e $ssh_key_file_22_in_dir){
  print "INFO Chmod SSH key file, to Read Only (0400) \n"; #
  print $log_fh "INFO Chmod SSH key file, to Read Only (0400) \n";

  chmod(0400, $ssh_key_file_22_in_dir) or die "Could not chmod  $ssh_key_file_22_in_dir : $!";
}

if (-e $ssh_key_pub_file_22_in_dir){
  print "INFO Chmod SSH - Public - key, to Read Only (0400) \n"; #
  print $log_fh "INFO Chmod SSH - Public - key, to Read Only (0400) \n";

  chmod(0400, $ssh_key_pub_file_22_in_dir) or die "Could not chmod $ssh_key_pub_file_22_in_dir : $!";
}

# Print files and permissions. For information.

print "\n\nSSH key files: \n"; 
print $log_fh "\n\nSSH key files: \n"; 

while (my $f = </exlibris/aleph/.ssh/*>){
    # See: https://perldoc.perl.org/functions/stat.html
    # or (Perl 5.8.9): https://perldoc.perl.org/5.8.9/functions/stat.html
  if ( $f =~ /\Q$ssh_key_file_22/  || $f =~ /\Q$ssh_key_file_22_pub/  ){ 
    print "\n"; 
    my $mode = (stat($f))[2];
    printf "%04o", S_IMODE($mode), " ";
    print " $f\n";

    printf $log_fh "%04o", S_IMODE($mode), " ";
    print $log_fh " $f\n"; 
  } 
}

#################################################
#################################################
chdir $orig_path ;

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

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

