=pod
 Usage              : aleph23_mft_patch_script_02.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(); 
use strict; 
use Fcntl ':mode'; 

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

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

my $revert_files_log_file = 'aleph23_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 = 'aleph23_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 = '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\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\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 AIN: M4021/implement_02.pl
########################################

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

#####
print "\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_23 = "aleph_23_sp_mft"; 
my $ssh_key_file_23_pub = "aleph_23_sp_mft.pub"; 

# my $src_dir_in_sp_ain = "$ENV{'uk_root'}/conf/general/$step_number"; 
# my $ssh_key_file_23_in_ain = "$src_dir_in_sp_ain/aleph_23_sp_mft"; 
# my $ssh_key_pub_file_23_in_ain = "$src_dir_in_sp_ain/aleph_23_sp_mft.pub"; 


print "$orig_path \n\n";    # Where we running. Curren original dir.  
print $log_fh "$orig_path \n\n"; 
my $ssh_key_file_23_in_orig_path = "$orig_path/aleph_23_sp_mft";
my $ssh_key_pub_file_23_in_orig_path = "$orig_path/aleph_23_sp_mft.pub";


#################################################
#################################################
# Destination for the SSH key files. 
my $aleph_ssh_dir = '/exlibris/aleph/.ssh';


####################
#####  Get the HOME dir of user aleph. 

my $aleph_home_dir_getpwnam = ((getpwnam('aleph'))[7]);
my $aleph_home_dir_getpwnam_ssh = "$aleph_home_dir_getpwnam/.ssh";

if (-e $aleph_home_dir_getpwnam_ssh and -d $aleph_home_dir_getpwnam_ssh ) {
  print "$aleph_home_dir_getpwnam_ssh \n" ;
  if ($aleph_home_dir_getpwnam_ssh eq $aleph_ssh_dir) {
    #  This is good,  we are in '/exlibris/aleph/.ssh' and stay in it, 
    # so Do nothing. 
    print "    \n";
  }
  if ($aleph_home_dir_getpwnam_ssh ne $aleph_ssh_dir) {
    # We need to copy to the HOME of aleph user, to: ~aleph/.ssh/ 
    # and change the $aleph_ssh_dir - used later in the script. 

    $aleph_ssh_dir = $aleph_home_dir_getpwnam_ssh ;
    print "$aleph_home_dir_getpwnam_ssh \t $aleph_ssh_dir \n" ;
    print "    \n";
    print $log_fh "$aleph_home_dir_getpwnam_ssh \t $aleph_ssh_dir \n\n";
  }
}

##        
#################### 
 
my $ssh_key_file_23_in_dir = "$aleph_ssh_dir/aleph_23_sp_mft"; 
my $ssh_key_pub_file_23_in_dir = "$aleph_ssh_dir/aleph_23_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_23_in_dir && -f _ && -r _ ){
  print "INFO File $ssh_key_file_23_in_dir exists and readable\n"; 
  print $log_fh "INFO File $ssh_key_file_23_in_dir exists and readable\n"; 
} 

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

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

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

if (-e $ssh_key_file_23_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_23_in_dir) or die "Could not chmod  $ssh_key_file_23_in_dir : $!"; 
} 

if (-e $ssh_key_pub_file_23_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_23_in_dir) or die "Could not chmod $ssh_key_pub_file_23_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_23/  || $f =~ /\Q$ssh_key_file_23_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 
########################################

