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

package autofixer2::mariadb_gpg_key;

use strict;
use warnings;
use Carp;
use File::Slurp;
use Cpanel::SafeRun::Errors;

BEGIN { unshift @INC, '/usr/local/cpanel'; }

exit run() unless caller;

sub run {
    return 0 unless supported_on_this_major( 86, 110 );    # Set this to the Min/Max we support this autofixer on.

    my $is_ubuntu = -f '/etc/os-release' ? grep { m/^NAME="Ubuntu"/ } File::Slurp::read_file('/etc/os-release') : 0;
    return 0 if $is_ubuntu;                                # Run only on RPM-based systems

    my $rpm_output = Cpanel::SafeRun::Errors::saferunallerrors( 'rpm', '--import', 'https://supplychain.mariadb.com/MariaDB-Server-GPG-KEY' );
    croak $rpm_output if $rpm_output;                      # Only outputs on error.

    return 0;                                              # exit(0);
}

# Do we run this code?
sub supported_on_this_major {
    my ( $min_ver, $max_ver ) = @_;

    my $major = get_major_version();

    return 0 if $major < $min_ver;
    return 0 if $major > $max_ver;

    return 1;
}

sub get_major_version {
    my $major_version;

    if ( open( my $fh, '<', '/usr/local/cpanel/version' ) ) {
        my $full_version = <$fh>;
        chomp $full_version;
        close($fh);
        ($major_version) = $full_version =~ /^[0-9]+\.([0-9]+)/;
    }

    # Safe default.
    return $major_version || 30;
}

1;
