package Cpanel::Easy::Apache::MPMItk;

# cpanel - Cpanel/Easy/Apache/MPMItk.pm              Copyright 2017 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;

use Cpanel::FileUtils ();

our $easyconfig = {
    'name'      => qq{MPM ITK},
    'version'   => q{2.2.17-01},
    'url'       => 'http://mpm-itk.sesse.net',
    'verify_on' => "Have you read the documentation, fully understand the implications of using this MPM, and are you in a position to handle problems it may cause? If you answer NO to any of those questions it is highly recommended not to choose an MPM.",
    'note'      => 'Please read the EasyApache documentation before using MPM ITK located at: http://go.cpanel.net/mpmitk.',
    'skip'      => 0,
    'hastargz'  => 1,
    'implies'   => {
        'Cpanel::Easy::Apache::Fastcgi'    => 0,
        'Cpanel::Easy::Apache::MPMEvent'   => 0,
        'Cpanel::Easy::Apache::MPMPrefork' => 0,
        'Cpanel::Easy::Apache::MPMWorker'  => 0,
        'Cpanel::Easy::Apache::PHPAsUser'  => 0,
        'Cpanel::Easy::ModMono'            => 0,
        'Cpanel::Easy::ModMono2'           => 0,
        'Cpanel::Easy::ModRuid2'           => 0,
        'Cpanel::Easy::Tomcat::7_0'        => 0,
        'Cpanel::Easy::EAccelerator'       => 0,
        'Cpanel::Easy::ModQos'             => 0,
    },
    'cpversion' => [

        # tie to these cpanel versions because of FB 103305
        # NOTE: This only takes into account customer versions of cPanel
        { 'tier' => '11.40', 'minversion' => '11.40.1.16' },
        { 'tier' => '11.42', 'minversion' => '11.42.1.19' },
        { 'tier' => '11.44', 'minversion' => '11.44.0.8' }
    ],
    'dryrun_fails_fatal' => 1,
    'dryrun'             => {

        # MPM ITK, as of right now, is just a set of patches that are applied to the
        # Apache source code.  The order of the patches is defined in the 'series' file.
        # The developer is looking at trying to make a self-contained module for Apache 2.4,
        # so this shouldn't just be blindly copied when that's complete.
        '0' => {
            'name'    => 'Applying ITK patches to Apache',
            'command' => sub {
                my $self    = shift;
                my $src_dir = sprintf( '%s/%s', $self->{'opt_mod_src_dir'}, $self->mpmitk()->{'dir'} );
                my $ap_dir  = $self->get_apache_src_dir();
                my @return  = $self->apply_mpmitk_patches( $src_dir, $ap_dir );
                Cpanel::FileUtils::touchfile("$ap_dir/server/mpm/experimental/itk/.deps");
                return @return;
            },
        },
        '1' => {
            'name'    => 'Applying EasyApache patches',
            'command' => sub {
                my $self      = shift;
                my $patch_dir = $self->{'patch_src_dir'};
                my $ap_dir    = $self->get_apache_src_dir();
                my @ret;
                for my $p (qw( 0001-easy-mpm-itk-configure.patch )) {
                    @ret = $self->apply_patch( "$patch_dir/$p", $ap_dir );
                    last unless @ret;
                }
                return @ret;
            },
        },
        '2' => {
            'name'    => 'Applying CloudLinux patches (if applicable)',
            'command' => sub {
                my $self      = shift;
                my $patch_dir = $self->{'patch_src_dir'};
                my $ap_dir    = $self->get_apache_src_dir();
                my @ret;

                if ( $self->has_cloudlinux_support() ) {
                    @ret = $self->apply_patch( "$patch_dir/0002-easy-cloudlinux-itk.patch", $ap_dir );
                }
                else {
                    $self->print_verbose_aware("Foregoing patches; CloudLinux unsupported or not enabled");
                    @ret = qw( 1 Ok );
                }

                return @ret;
            },
        },
    },
    'step' => {
        '0' => {
            'name'    => 'to config options',
            'command' => sub {
                my ($self) = @_;
                $self->replace_in_configure( { '--with-mpm' => 'itk' } );
                return ( 1, 'Ok' );
            },
        },
    },
};

1;
