#!/usr/local/cpanel/3rdparty/bin/perl
# cpanel - autofixer2/mysql_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::mysql_gpg_key;

use strict;
use warnings;
use Carp;
use HTTP::Tiny;
use File::Temp;
use Cpanel::SafeRun::Simple;

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

exit run() unless caller;

sub run {

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

    my $old_gpgkey;
    my $new_gpgkey;
    if ( -f '/etc/yum.repos.d/Mysql57.repo' || -f '/etc/yum.repos.d/Mysql80.repo' ) {    # Centos
        # CentOS 7 old : gpgkey=http://repo.mysql.com/RPM-GPG-KEY-mysql
        # CentOS 7 new : gpgkey=https://repo.mysql.com/RPM-GPG-KEY-MySQL-2022
        $old_gpgkey = quotemeta "gpgkey=http://repo.mysql.com/RPM-GPG-KEY-mysql";
        $new_gpgkey = "gpgkey=https://repo.mysql.com/RPM-GPG-KEY-MySQL-2022";
    } elsif ( -f '/etc/yum.repos.d/mysql-community-source.repo' || -f '/etc/yum.repos.d/mysql-community.repo') { # CloudLinux
        # CloudLinux 6 old : gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
        # CloudLinux 6 new : gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
        $old_gpgkey = quotemeta "gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql";
        $new_gpgkey = "gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022";
        # and, retrieve the keyfile
        
        my $url        = 'https://repo.mysql.com/RPM-GPG-KEY-mysql-2022';
        my $keyfile    = '/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022';
        my $gpgkey_req = HTTP::Tiny->new()->mirror($url,$keyfile);
        croak "Failed [resp status: " . $gpgkey_req->{status} . "]!\n" unless $gpgkey_req->{success};
    }

    if ( -f '/etc/yum.conf' ) {    # RPM-based (CentOS? CloudLinux? AlmaLinux?)
        my @repo_files = ( '/etc/yum.repos.d/Mysql57.repo', '/etc/yum.repos.d/Mysql80.repo', '/etc/yum.repos.d/mysql-community-source.repo', '/etc/yum.repos.d/mysql-community.repo' );
        foreach my $repo_file (@repo_files) {
            next if !-f $repo_file;
            open( my $repo_fh, '<', $repo_file ) or croak "Cannot open $repo_file for reading: $!\n";
            my @repo_contents = (<$repo_fh>);
            close($repo_fh);
            open( my $repo_fh_out, '>', $repo_file ) or croak "Cannot open $repo_file for writing: $!\n";
            foreach my $line (@repo_contents) {
                $line =~ s/$old_gpgkey/$new_gpgkey/;
                print {$repo_fh_out} $line;
            }
            close($repo_fh_out);
        }
    }
    else {    # DEB-based (Ubuntu?)
        my ( $fh, $tmp_keyfile ) = File::Temp::tempfile( "AUTOFIXSQL_XXXXXXX", TMPDIR => 1, UNLINK => 1, DIR => '/root' );
        my $url        = 'https://repo.mysql.com/RPM-GPG-KEY-mysql-2022';
        my $gpgkey_req = HTTP::Tiny->new()->get($url);
        my $gpgkey     = $gpgkey_req->{'content'};
        open( my $key_fh_w, '>', $tmp_keyfile ) or croak "Cannot open $tmp_keyfile: $!";
        print {$key_fh_w} $gpgkey               or croak "Cannot write to $tmp_keyfile: $!";
        close($key_fh_w)                        or croak "Cannot close $tmp_keyfile: $!";

        croak "Failed [resp status: " . $gpgkey_req->{status} . "]!\n" unless $gpgkey_req->{success};

        # We've got our gpgkey info, add it to the system with apt-key (for now)
        my $apt_key_output = Cpanel::SafeRun::Simple::saferun( 'apt-key', 'add', $tmp_keyfile );
        print "$apt_key_output\n";
        unlink $tmp_keyfile or warn "Could not unlink $tmp_keyfile: $!";

    }

    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;
