#!/usr/local/cpanel/3rdparty/bin/perl
# cpanel   -    ensure_libnghttp2_to_cover_oddball_repos_that_break_curl_and_yum
#                                                    Copyright 2018 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

package autofixer2::ensure_libnghttp2_to_cover_oddball_repos_that_break_curl_and_yum;

use strict;
use warnings;

exit(0) if $ENV{CPANEL_BASE_INSTALL};    # Don't run on new installs

my $symso_lib64 = "/usr/lib64/libnghttp2.so.14";
my $symso_lib   = "/usr/lib/libnghttp2.so.14";
my $cptarget    = "/opt/cpanel/nghttp2/lib/libnghttp2.so.14.13.0";
exit( run('libnghttp2') ) if !caller();

###############
#### helpers ##
###############

sub run {
    my ($pkg) = @_;

    # return exit() value

    return 0 if scalar(`/usr/bin/curl -h 2>&1`) !~ m/libnghttp2\.so\.14/;
    return 0 if !eval "require Cpanel::PackMan;1";                          ## no critic qw(BuiltinFunctions::ProhibitStringyEval)

    for my $symso ( $symso_lib64, $symso_lib ) {
        unlink $symso;
        symlink( $cptarget, $symso );                                       # So that yum can run in this state on Amazon Linux
    }

    my $pkm = Cpanel::PackMan->instance();
    eval { $pkm->sys->install($pkg) };
    my $err = $@;
    if ( !$err ) {
        if ( !$pkm->is_installed($pkg) ) {

            # we've hit the bug noted in ZC-2763, fixed in 66 on
            if ( system( "yum", "install", "-y", $pkg ) != 0 ) {
                _clean_symlinks();
                _print_repo_msg($pkg);
                return 1;
            }
        }
    }
    else {
        _clean_symlinks();
        _print_repo_msg($pkg);
        die $err;
    }

    _clean_symlinks();

    if ( scalar(`/usr/bin/curl -h 2>&1`) =~ m/libnghttp2\.so\.14/ ) {
        die "The problem still appears to exist but no errors were detected in the resolution. If this problem persists please contact cPanel support\n";
    }

    # Whichever $symso’s the package owns are updated by the package to point to the now installed system .so, so no need to try and restore the target
    return 0;
}

sub _clean_symlinks {
    for my $symso ( $symso_lib64, $symso_lib ) {
        if ( readlink($symso) eq $cptarget ) {
            unlink $symso;
        }
    }

    return;
}

sub _print_repo_msg {
    my ($pkg) = @_;

    warn <<"REPO";

#######
Could not install libnghttp2 using any enabled yum repos.

Running `yum install -y $pkg` may give you an idea of what went wrong.

You will need to:

    1. Find what yum repo for our OS has $pkg – e.g. CentOS it is epel
    2. Install/enable that yum repo (temporarily anyway) – e.g. for epel you would run: yum install -y epel-release
    3. Install $pkg – `yum install -y $pkg`

If you can’t do that because `yum install -y $pkg` fails with a `libnghttp2.so.14` you will need to either:

    a. Find a libnghttp2 RPM for your OS and use `rpm` to install it.
    b. Build libnghttp2 from source.
#######

REPO

    return;
}

1;
