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

use strict;
use warnings;

use Cpanel::Logger ();
use File::Glob     ();
use Cpanel::JSON   ();
use YAML::Syck     ();

sub run {

    # To detect whether we need this autofixer or not, we look
    # into the /var/log/yum.log* or /var/log/dnf.log* to find
    # the offending rpm

    if ( !`grep -s ea-apache24-config-runtime-1.0-171 /var/log/yum* /var/log/dnf*` ) {    ## no critic qw(Cpanel::ProhibitQxAndBackticks)
        _append_log_lines(
            'repair_fpm_ea_apache2_config_171',
            "This server is not a candidate for repair_fpm_ea_apache2_config"
        );

        return 0;
    }

    _append_log_lines(
        'repair_fpm_ea_apache2_config_171',
        "This server had ea-apache2-config-runtime-1.0-171 installed, attempting repairs."
    );

    # find candidates

    my @candidates = File::Glob::bsd_glob("/var/cpanel/userdata/*/*fpm.cache");
    my $repaired   = 0;

    foreach my $cache (@candidates) {
        my $yaml = $cache;
        $yaml =~ s/fpm\.cache/fpm\.yaml/;

        if ( !-e $yaml ) {
            _append_log_lines(
                'repair_fpm_ea_apache2_config_171',
                "File $yaml is missing but $cache is present, repairing"
            );

            $repaired += _create_yaml_from_cache( $cache, $yaml );
        }
    }

    if ($repaired) {
        my @lines = `/usr/local/cpanel/scripts/php_fpm_config --rebuild`;    ## no critic qw(Cpanel::ProhibitQxAndBackticks)

        _append_log_lines(
            'repair_fpm_ea_apache2_config_171',
            "scripts/php_fpm_config called",
            @lines
        );
    }

    _append_log_lines(
        'repair_fpm_ea_apache2_config_171',
        "Completed"
    );

    return 0;
}

#######################################################
#
# Helpers
#
#######################################################

my $logger;

sub _append_log_lines {
    my ( $service, @lines ) = @_;

    $logger ||= Cpanel::Logger->new();

    chomp(@lines);
    foreach my $line (@lines) {
        $logger->logger(
            {
                message   => $line,
                level     => 'info',
                service   => $service,
                output    => 0,
                backtrace => 0,
            }
        );
    }

    return;
}

sub _create_yaml_from_cache {
    my ( $cache, $yaml ) = @_;

    my $hr        = Cpanel::JSON::LoadFile($cache);
    my $yaml_data = YAML::Syck::Dump($hr);

    if ( open my $fh, '>', $yaml ) {
        print $fh $yaml_data;
        close $fh;

        _append_log_lines(
            'repair_fpm_ea_apache2_config_171',
            "$yaml created"
        );

        return 1;
    }
    else {
        _append_log_lines(
            'repair_fpm_ea_apache2_config_171',
            "Unable to repair $cache, could not open $yaml"
        );

        return 0;
    }
}

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

1;

