PCI_CLASS=60000
PCI_ID=8086:1237
PCI_SUBSYS_ID=1AF4:1100
PCI_SLOT_NAME=0000:00:00.0
MODALIAS=pci:v00008086d00001237sv00001AF4sd00001100bc06sc00i00
 ,ÿ    #!/usr/local/cpanel/3rdparty/bin/perl
# cpanel - ea_current_to_profile                  Copyright(c) 2022 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

use strict;
use warnings;

package ea_cpanel_tools::ea_current_to_profile;

use Cpanel::PackMan;
use Cpanel::Config::Httpd;
use Cpanel::JSON;
use Cpanel::Time::Local;
use Path::Tiny 'path';

our $ea4_profiles_dir = '/etc/cpanel/ea4/profiles';
our $_max_attempts    = 20;

our $manifest_file     = "/etc/cpanel/ea4/profiles/pkg-manifest.json";
our $ea4_metainfo_file = '/etc/cpanel/ea4/ea4-metainfo.json';
our $pkg_renames_file  = '/etc/cpanel/ea4/target-os-pkg-renames.json';

our %safe_to_ignore = (
    'ea-brotli'            => 1,
    'ea-brotli-devel'      => 1,
    'ea-libargon2'         => 1,
    'ea-libargon2-devel'   => 1,
    'ea-libcurl'           => 1,
    'ea-libcurl-devel'     => 1,
    'ea-libnghttp2'        => 1,
    'ea-libxml2'           => 1,
    'ea-libzip'            => 1,
    'ea-nghttp2'           => 1,
    'ea-oniguruma'         => 1,
    'ea-oniguruma-devel'   => 1,
    'ea-openssl11'         => 1,
    'ea-openssl11-devel'   => 1,
    'ea-openssl11-libs'    => 1,
    'ea-openssl'           => 1,
    'ea-openssl-devel'     => 1,
    'ea-openssl-libs'      => 1,
    'ea-php73-libc-client' => 1,
    'ea-php74-libc-client' => 1,
    'ea-php80-libc-client' => 1,
    'ea-php81-libc-client' => 1
);

our %os_type = (
    "CentOS_7"      => "rpm",
    "CentOS_8"      => "rpm",
    "CentOS_9"      => "rpm",
    "xUbuntu_20.04" => "deb",
    "xUbuntu_22.04" => "deb",
);

exit( script(@ARGV) ) unless caller();

sub script {
    my (@args) = @_;

    my $custom_json;
    my $new_name;
    my $target_os;
    my $manifest_os;
    my $modified = 0;
    my $source_pkmgr = -x '/usr/bin/apt' ? 'deb' : 'rpm';

    # We need to get OS aliases
    my %ea4_metainfo        = %{ Cpanel::JSON::LoadFile($ea4_metainfo_file) };
    my %obs_project_aliases = %{ $ea4_metainfo{obs_project_aliases} };
    die "obs_project_aliases is empty" if ( !%obs_project_aliases );

    foreach my $arg (@args) {
        if ( $arg eq "--help" ) {
            my $os_string = join( ' ', keys %obs_project_aliases );

            print <<USAGE;
    $0 [--help] [--output=profile_file] [--target-os=os]
        Take the current ea rpms and create a custom profile.
        Profile is written to /etc/cpanel/ea4/profiles/custom

        --output=profile_file, where the profile is written to.
           Note: forcefully overwrites the profile_file if it already
           exists.

        --target-os=os [$os_string]

USAGE

            exit 0;
        }
        elsif ( $arg =~ m/^--output=(.*)$/ ) {
            $custom_json = $1;
            my @path = split( /\//, $custom_json );
            $new_name = $path[-1];
        }
        elsif ( $arg =~ m/^--target-os=(.*)$/ ) {
            $target_os = $1;
            die "Not a valid OS ($target_os)" if !exists $obs_project_aliases{$target_os};
            $manifest_os = $obs_project_aliases{$target_os};
        }
        else {
            die "Unknown argument “$arg”\n";
        }
    }

    my @addl_prefixes = eval {
        map { $_->basename } path("/etc/cpanel/ea4/additional-pkg-prefixes/")->children;
    };

    die "May only be run if you are using EasyApache 4" if ( !Cpanel::Config::Httpd::is_ea4() );
    my @pkgs_have = Cpanel::PackMan->instance->list( state => "installed", 'prefix' => 'ea-' );
    @pkgs_have = grep !/ea-profiles-cpanel/, @pkgs_have;

    my $prefix_piped = "ea";
    for my $prefix (@addl_prefixes) {
        push @pkgs_have, Cpanel::PackMan->instance->list( state => "installed", 'prefix' => "$prefix-" );

        for my $ig ( keys %safe_to_ignore ) {
            $ig =~ s/^e