=pod
 Usage              : aleph22_mft_patch_script_01.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.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();


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

########################################
# 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 = '';


########################################
# From AIN: M4021/implement_01.pl
########################################

print "\n\n";
print "==================================================\n";
print " 1. Handling sp_config file . \n";
print "==================================================\n";

print $log_fh "------------------------------\n";
print $log_fh "1. Handling sp_config file . \n";
print $log_fh "------------------------------\n\n\n";

#####
my $ae_root = $ENV{alephe_root};

#####
## In the U tree.
#                 $alephe_root/sp.config
# /exlibris/aleph/u22_1/alephe/sp.config
my $sp_config_file_aer = "$ae_root/sp.config";
#####

if ( ! -e $sp_config_file_aer) {
    print "ERROR Can't open file $sp_config_file_aer \n Please use util SP / 1 - Prepare/Update Environment \n";
    print $log_fh "ERROR Can't open file $sp_config_file_aer \n Please use util SP / 1 - Prepare/Update Environment \n";
    exit ; # die "$!";
}

#### print `csh -f source/proc/backup_file.csh $sp_config_file_aer`;  ## Doing this down below.

my $all_lines     = io($sp_config_file_aer)->slurp;
#####

#####

my $old_ftp_exlibris_usa          = "ftp.exlibris-usa.com"; ## Should be replaced to:
my $new_ftp_downloads_na_hosted   = "downloads-na.hosted.exlibrisgroup.com";

my $old_ftp_exlibrisgroup_com     = "ftp.exlibrisgroup.com"; ## Should be replaced to ... the same as above
##  $new_ftp_downloads_na_hosted  = "downloads-na.hosted.exlibrisgroup.com";

my $old_ftp_exl_de                = "ftp.exl.de"; ## Should be replaced to:
my $new_ftp_downloads_eu_hosted   = "downloads-eu.hosted.exlibrisgroup.com";

my $old_ftp_exl_apac              = "ftp-apac.hosted.exlibrisgroup.com"; ## Should be replaced to:
my $new_ftp_downloads_apac_hosted = "downloads-ap.hosted.exlibrisgroup.com";

my $old_ftp_exlibris_co_il        = "ftp.exlibris.co.il";
## my $new_ftp_downloads_eu_hosted   = "downloads-eu.hosted.exlibrisgroup.com";


my $old_ftp_il_ftp_01             = "il-ftp01"; ## Should be replaced to:
my $new_ftp_il_downloads_master   = "il-download-master.int.hosted.exlibrisgroup.com";
## The above on is for internal use only!


my $found_old_ftp_exlibris_usa      = "N";
my $found_old_ftp_exlibrisgroup_com = "N";
my $found_old_ftp_exlibris_co_il    = "N";
my $found_old_ftp_exl_de            = "N";
my $found_old_ftp_exl_apac          = "N";
my $found_old_ftp_il_ftp_01         = "N";

my $found_new_ftp_server            = "N";
my $found_old_ftp_server            = "N";

if ($all_lines =~ /\Q$new_ftp_downloads_na_hosted/ ||
    $all_lines =~ /\Q$new_ftp_downloads_eu_hosted/ ||
    $all_lines =~ /\Q$new_ftp_downloads_apac_hosted/
     )
     {
        print "INFO The sp.config seems to have the correct 'ftp_server' line. \n";
        print $log_fh "INFO The sp.config seems to have the correct 'ftp_server' line. \n";
        $found_new_ftp_server            = "Y";
     }

if ($all_lines !~ /\Q$old_ftp_exlibris_usa/           &&
       $all_lines !~ /\Q$old_ftp_exlibrisgroup_com/   &&
       $all_lines !~ /\Q$old_ftp_exl_de/              &&
       $all_lines !~ /\Q$old_ftp_exl_apac/            &&
       $all_lines !~ /\Q$new_ftp_downloads_na_hosted/ &&
       $all_lines !~ /\Q$new_ftp_downloads_eu_hosted/ &&
       $all_lines !~ /\Q$new_ftp_downloads_apac_hosted/
    ) {
        print "NOTE Cannot find exactly where to add / replace 'set ftp_server = ...' lines. \n";
        print "NOTE PLEASE VERIFY. \n\n";
        print $log_fh "NOTE Cannot find exactly where to add / replace 'set ftp_server = ...' lines. \n";
        print $log_fh "NOTE PLEASE VERIFY. \n\n";

        $found_old_ftp_server            = "N";
       }


my $ftp_user_22_old = "ver22_sp";
my $ftp_user_22_new = 'aleph_22_sp@customers.gl';

my $found_new_ftp_user              = "N";
my $found_old_ftp_user              = "N";

if ($all_lines =~ /\Q$ftp_user_22_new/ ) {
    print "INFO The sp.config seems to have the correct 'ftp_user' line. \n";
    print $log_fh "INFO The sp.config seems to have the correct 'ftp_user' line. \n";
    $found_new_ftp_user              = "Y";
}

if ($all_lines =~ /\Q$ftp_user_22_old/ ){
    print "INFO The sp.config seems to have the OLD 'ftp_user' line. \n";
    print $log_fh "INFO The sp.config seems to have the OLD 'ftp_user' line. \n";
    $found_old_ftp_user              = "Y";
}

if (    $all_lines !~ /\Q$ftp_user_22_old/  &&
        $all_lines !~ /\Q$ftp_user_22_new/
    ) {
        $found_new_ftp_user              = "N";
        $found_old_ftp_user              = "N";

        print "NOTE Cannot find exactly where to add / replace 'set ftp_user = ...' line. \n";
        print "NOTE PLEASE VERIFY. \n";

        print $log_fh "NOTE Cannot find exactly where to add / replace 'set ftp_user = ...' line. \n";
        print $log_fh "NOTE PLEASE VERIFY. \n\n";
       }

if ($found_new_ftp_user  eq "Y"   && $found_old_ftp_user eq "N"  &&
    $found_new_ftp_server eq "Y"  &&  $found_old_ftp_server eq "N" )
    {
      print "INFO The sp.config seems to be updated. \n";
      print "INFO Exiting. \n\n";

      print $log_fh "INFO The sp.config seems to be updated. \n";
      print $log_fh "INFO Exiting. \n\n";

      exit ;
   }

#################################################
##########     REPLACEMENT:            ##########
#################################################
#

if ($all_lines =~ /\Q$old_ftp_exlibris_usa/) {
    $all_lines =~ s/\Q$old_ftp_exlibris_usa/$new_ftp_downloads_na_hosted/g; ## REPLACING!!
    #                ftp.exlibris-usa.com -> downloads-na.hosted.exlibrisgroup.com
    #
    print "INFO Changing '$old_ftp_exlibris_usa' To: '$new_ftp_downloads_na_hosted' \n";
    $found_old_ftp_exlibris_usa      = "Y";
}

if ($all_lines =~ /\Q$old_ftp_exlibrisgroup_com/) {
    $all_lines =~ s/\Q$old_ftp_exlibrisgroup_com/$new_ftp_downloads_na_hosted/g;
    #                  ftp.exlibrisgroup.com -> downloads-na.hosted.exlibrisgroup.com
    #
    print "INFO Changing '$old_ftp_exlibrisgroup_com' To: '$new_ftp_downloads_na_hosted' \n";
    print $log_fh "INFO Changing '$old_ftp_exlibrisgroup_com' To: '$new_ftp_downloads_na_hosted' \n";
    $found_old_ftp_exlibrisgroup_com = "Y";
}

if ($all_lines =~ /\Q$old_ftp_exl_de/) {
    $all_lines =~ s/\Q$old_ftp_exl_de/$new_ftp_downloads_eu_hosted/g;
    #                  ftp.exl.de -> downloads-eu.hosted.exlibrisgroup.com
    #
    print "INFO Changing '$old_ftp_exl_de' To: '$new_ftp_downloads_eu_hosted' \n";
    print $log_fh "INFO Changing '$old_ftp_exl_de' To: '$new_ftp_downloads_eu_hosted' \n";
    $found_old_ftp_exl_de            = "Y";
}

if ($all_lines =~ /\Q$old_ftp_exl_apac/) {
    $all_lines =~ s/\Q$old_ftp_exl_apac/$new_ftp_downloads_apac_hosted/g;
    #                  ftp-apac.hosted.exlibrisgroup.com -> downloads-ap.hosted.exlibrisgroup.com
    #
    print "INFO Changing '$old_ftp_exl_apac' To: '$new_ftp_downloads_apac_hosted' \n";
    print $log_fh "INFO Changing '$old_ftp_exl_apac' To: '$new_ftp_downloads_apac_hosted' \n";
    $found_old_ftp_exl_apac          = "Y";
}

if ($all_lines =~ /\Q$old_ftp_il_ftp_01/) {
    $all_lines =~ s/\Q$old_ftp_il_ftp_01/$new_ftp_il_downloads_master/g;
    #                  il-ftp01 -> il-download-master.int.hosted.exlibrisgroup.com
    #
    print "INFO Changing '$old_ftp_il_ftp_01' To: '$new_ftp_il_downloads_master' \n";
    print $log_fh "INFO Changing '$old_ftp_il_ftp_01' To: '$new_ftp_il_downloads_master' \n";
    $found_old_ftp_il_ftp_01         = "Y";
}


if ($all_lines =~ /\Q$old_ftp_exlibris_co_il/) {
    $all_lines =~ s/\Q$old_ftp_exlibris_co_il/$new_ftp_downloads_eu_hosted/g;
   #####

    $found_old_ftp_exlibris_co_il  = "Y";
    print "INFO Changing '$old_ftp_exlibris_co_il' To: '$new_ftp_downloads_eu_hosted' \n";
    print $log_fh "INFO Changing '$old_ftp_exlibris_co_il' To: '$new_ftp_downloads_eu_hosted' \n";

}

if ($found_old_ftp_exlibris_usa eq "Y" || $found_old_ftp_exlibrisgroup_com eq "Y" ||
     $found_old_ftp_exl_apac eq "Y" || $found_old_ftp_il_ftp_01 eq "Y"
   ||  $found_old_ftp_exlibris_co_il eq "Y"
   ){
      print "INFO Done changing FTP. \n\n";
      print $log_fh "INFO Done changing FTP. \n\n";
    }

if ($all_lines !~ /\Q$ftp_user_22_old/ && $all_lines !~ /\Q$ftp_user_22_new/ )
    {
      print "NOTE Cannot find exactly where to replace 'set ftp_user = ...' line. \n";
      print "NOTE PLEASE VERIFY. \n\n";
      print $log_fh "NOTE Cannot find exactly where to replace 'set ftp_user = ...' line. \n";
      print $log_fh "NOTE PLEASE VERIFY. \n\n";
}

if ($all_lines =~ /\Q$ftp_user_22_old/) {
    $all_lines =~ s/\Q$ftp_user_22_old/$ftp_user_22_new/g;
    print "INFO Changing '$ftp_user_22_old' To: '$ftp_user_22_new' \n";
    print $log_fh "INFO Changing '$ftp_user_22_old' To: '$ftp_user_22_new' \n";
}

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

#################################################
##########      Backup                 ##########
#################################################
# print `csh -f source/proc/backup_file.csh $sp_config_file_aer`; 

## print " Back up : $full_path to: $file_name.$date_time\n";
print $log_fh "Copy $sp_config_file_aer to $sp_config_file_aer.$date_time\n"; # print to log
print $revert_files_log_fh " mv $sp_config_file_aer.$date_time $sp_config_file_aer\n"; # Log for reverting

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

print "Copy $sp_config_file_aer to $sp_config_file_aer.$date_time\n";
print "\n";


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

print "Updating: $sp_config_file_aer\n\n";
print $log_fh "Updating: $sp_config_file_aer\n\n";

my $io_out = IO::All->new($sp_config_file_aer);
$all_lines > $io_out;



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

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

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

