=pod
 Usage              : arc_3rd_party_prods_patch_script_02.pl
 Pre Requisites     : The following file must be on the scripts' directory
                     (used as input): 
                     - 'arc_3rd_party_products_mft' 

 Output of this script:
                     - 'arc_3rd_party_mft_patch.log' - Log of the actions done by this script.
                     - 'arc_3rd_party_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 = 'arc_3rd_party_mft_patch.log'; 
open(my $log_fh, '>>', $log_file) 
  or die "Could not create the log '$log_file' $!"; 

my $revert_files_log_file = 'arc_3rd_party_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_source_path = Cwd::cwd(); 
print $log_fh "----------------------------------------\n"; 
print $log_fh "$orig_source_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"; 

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

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

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

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

#####

my $ssh_key_file_3rd_prt_mft = "arc_3rd_party_products_mft"; 

print "$orig_source_path \n\n"; 
print $log_fh "$orig_source_path \n\n"; 
my $ssh_key_file_3rd_prt_mft_in_orig_source_path = "$orig_source_path/$ssh_key_file_3rd_prt_mft"; # "arc_3rd_party_products_mft" - current Source Dir

my $arc_crn_ssh_dir = '/exlibris/crn/.ssh';  # Target Dir. 
my $ssh_key_file_3rd_prt_mft_in_dest_dir = "$arc_crn_ssh_dir/$ssh_key_file_3rd_prt_mft"; # "arc_3rd_party_products_mft" - TARGET Dir 


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


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

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

    if ( -x $arc_crn_ssh_dir){  # Directory exists + writable + executable 
      print "INFO Directory is already Executable. :) \n"; 
      print $log_fh "INFO Directory is already Executable. :) \n"; 
    } 
  } 
  elsif ( !-w $arc_crn_ssh_dir || !-x $arc_crn_ssh_dir) { # Exists, But not writable or no exe rights. 
    chmod(0700, $arc_crn_ssh_dir)  # chmod - sets permissions / rights for Owner (u): Read (4) + Write (2) + Exe (1) 
      or die "Could not chmod $arc_crn_ssh_dir : $!"; 
    print "INFO Made the Directory Executable. \n"; 
    print $log_fh "INFO Made the Directory Executable. \n"; 
  } 
} 
elsif ( !-d $arc_crn_ssh_dir){ # Does 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 $arc_crn_ssh_dir ; # Create the Dir. 

  chmod(0700, $arc_crn_ssh_dir) # chmod - sets permissions / rights for Owner (u): Read (4) + Write (2) + Exe (1) 
    or die "Could not chmod $arc_crn_ssh_dir : $!"; 
} 

if (-e $ssh_key_file_3rd_prt_mft_in_dest_dir && -f _ && -r _ ){
  print "INFO File $ssh_key_file_3rd_prt_mft_in_dest_dir exists and readable\n"; 
  print $log_fh "INFO File $ssh_key_file_3rd_prt_mft_in_dest_dir exists and readable\n"; 
} 
 
my $ssh_key_file_mode = (stat($ssh_key_file_3rd_prt_mft_in_dest_dir))[2]; 
my $the_file_mode = sprintf "%04o", S_IMODE($ssh_key_file_mode), " "; 
# print "  mode $the_file_mode\n\n"; 

if (-e $ssh_key_file_3rd_prt_mft_in_dest_dir && $the_file_mode !~ /\Q400/){ 
  chmod(0400, $ssh_key_file_3rd_prt_mft_in_dest_dir)  # chmod - sets permissions / rights for Owner (u): Read (4) 
      or die "Could not chmod $ssh_key_file_3rd_prt_mft_in_dest_dir : $!"; 
  print "INFO: chmod 400 the SSH key file (to be Read Only) '$ssh_key_file_3rd_prt_mft_in_dest_dir' \n"; 
  print $log_fh "INFO: chmod 400 the SSH key file (to be Read Only) '$ssh_key_file_3rd_prt_mft_in_dest_dir' \n"; 
} 

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

  chmod(0400, $ssh_key_file_3rd_prt_mft_in_dest_dir)  # chmod - sets permissions / rights for Owner (u): Read (4) 
    or die "Could not chmod $ssh_key_file_3rd_prt_mft_in_dest_dir : $!"; 

  print "INFO: chmod 400 the SSH key file (to be Read Only) '$ssh_key_file_3rd_prt_mft_in_dest_dir' \n"; # 
  print $log_fh "INFO: chmod 400 the SSH key file (to be Read Only) '$ssh_key_file_3rd_prt_mft_in_dest_dir' \n"; 
} 

# Print files and permissions. For information. 

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

while (my $f = </exlibris/crn/.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_3rd_prt_mft/ ){ 
    print "\n"; 
    my $mode2 = (stat($f))[2]; 
    printf "%04o", S_IMODE($mode2), " "; 
    print " $f\n"; 
    
    my $the_file_mode2 = sprintf "%04o", S_IMODE($mode2), " "; 
    print "  mode $the_file_mode2\n\n"; 

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

#################################################
#################################################
chdir $orig_source_path ; 

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

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

