#!/usr/local/cpanel/3rdparty/bin/perl

package autofixer2::remove_mysql84_versionlock;

#                                      Copyright 2026 WebPros International, LLC
#                                                           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;

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

my $SCRIPT = 'remove_mysql84_versionlock';
my $TAG    = 'cpanel:mysql84-versionlock';

exit run() unless caller;

# Undo the exclude added by mysql97_lts_repo_disable.
# That autofixer appended "mysql*-community-release # cpanel:mysql84-versionlock"
# to the exclude= line in dnf.conf / yum.conf.  This autofixer strips exactly
# that tagged portion, leaving any other excludes intact.  If the exclude= line
# becomes empty after removal, the line is deleted entirely.

sub run {
    return 0 unless supported_on_this_major( 118, 136 );

    # Only applies to RPM-based systems.
    return 0 unless -e '/usr/bin/rpm' || -e '/bin/rpm';

    # Find which config files exist and contain our tag.
    my @confs = grep { -f $_ } ( '/etc/dnf/dnf.conf', '/etc/yum.conf' );
    return 0 unless @confs;

    my @tagged = grep { _file_contains_tag($_) } @confs;
    unless (@tagged) {
        print "$SCRIPT: No tagged exclude found; nothing to do.\n";
        return 0;
    }

    my $changed = 0;
    for my $conf (@tagged) {
        my $rc = _remove_tagged_exclude($conf);
        $changed = 1 if $rc;
    }

    if ($changed) {
        print "$SCRIPT: Removed mysql*-community-release exclude (marker: $TAG).\n";
    }

    return 0;
}

sub _file_contains_tag {
    my ($conf) = @_;

    open( my $fh, '<', $conf ) or return 0;
    while ( my $line = <$fh> ) {
        if ( $line =~ /\Q$TAG\E/ ) {
            close $fh;
            return 1;
        }
    }
    close $fh;
    return 0;
}

sub _remove_tagged_exclude {
    my ($conf) = @_;

    open( my $fh, '<', $conf ) or return 0;
    my @lines = <$fh>;
    close $fh;

    my $modified = 0;
    my @out;

    for my $line (@lines) {
        # Only touch lines that contain our tag.
        if ( $line =~ /\Q$TAG\E/ ) {

            # Strip our tagged portion:
            #   " mysql*-community-release # cpanel:mysql84-versionlock"
            # The leading whitespace may or may not be present (new-line case).
            ( my $cleaned = $line ) =~ s/\s*mysql\*-community-release\s*#\s*\Q$TAG\E\s*$//;

            # If nothing meaningful remains (bare "exclude=" or blank), drop the line.
            if ( $cleaned =~ /^\s*exclude\s*=\s*$/i || $cleaned =~ /^\s*$/ ) {
                $modified = 1;
                next;    # skip this line entirely
            }

            # Otherwise keep the line with just our portion removed.
            $cleaned .= "\n" unless $cleaned =~ /\n$/;
            push @out, $cleaned;
            $modified = 1;
        }
        else {
            push @out, $line;
        }
    }

    return 0 unless $modified;

    open( my $wfh, '>', $conf ) or do {
        warn "$SCRIPT: Cannot write $conf: $!\n";
        return 0;
    };
    print $wfh @out;
    close $wfh;

    return 1;
}

# ---- Standard autofixer helpers (from template_autofixer) ----

sub supported_on_this_major {
    my ( $min, $max ) = @_;

    my $major = get_major_version();
    return 0 if !$major;
    return 0 if $major < $min || $major > $max;
    return 1;
}

sub get_major_version {
    open( my $fh, '<', '/usr/local/cpanel/version' ) or return 0;
    my $version = readline($fh);
    close $fh;
    chomp $version if $version;

    my ($major) = $version =~ m/^11\.(\d+)\./a;
    return $major || 0;
}

1;
