package Cpanel::Easy::ModPerl;

# cpanel - Cpanel/Easy/ModPerl.pm                 Copyright(c) 2014 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

# Aside from having apxs provide the LoadModule line for mod_perl, no configuration
# of mod_perl is currently carried out.  If there is ever reason to configure mod_perl
# properly in the future, the necessary directives in httpd.conf follow:

# Apache 2 + mod_perl 2.0.8

# Remove the .pl extension from the cgi-script AddHandler line
# AddHandler perl-script .pl
# PerlHandler ModPerl::Registry

# More details at https://dev.cpanel.net/default.php?947

my $perl = '/usr/bin/perl';

my $version = q{2.0.8};

our $easyconfig = {
    'note'         => qq{Not needed to run CGI scripts},
    'name'         => qq{Mod Perl $version},
    'url'          => 'http://perl.apache.org/',
    'src_cd2'      => 'mod_perl-2',
    'hastargz'     => 1,
    'perl_modules' => {
        'BSD::Resource'         => 1,    # JHI/BSD-Resource-1.2904
        'TAP::Harness'          => 1,    # OVID/Test-Harness-3.26
        'Inline'                => 1,    # SISYPHUS/Inline-0.51
        'Devel::Cover'          => 1,    # PJCJ/Devel-Cover-0.99
        'IPC::Run3'             => 1,    # RJBS/IPC-Run3-0.045
        'LWP'                   => 1,    # GAAS/libwww-perl-6.04
        'HTTP::Request::Common' => 1,    # GAAS/HTTP-Message-6.06
        'Net::SSL'              => 1,    # NANIS/Crypt-SSLeay-0.64
        'Tie::IxHash'           => 1,    # CHORNY/Tie-IxHash-1.22
        'Data::Flow'            => 1,    # ILYAZ/Data-Flow-1.02
        'HTML::HeadParser'      => 1,    # GAAS/HTML-Parser-3.69
        'HTTP::Date'            => 1,    # GAAS/HTTP-Date-6.02
        'Date::Format'          => 1,    # GBARR/TimeDate-1.20
        'Time::ParseDate'       => 1,    # MUIR/Time-modules-2011.0517
        'Mail::Send'            => 1,    # MARKOV/MailTools-2.12
        'Net::Ident'            => 1,    # TODDR/Net-Ident-1.23
        'B::LexInfo'            => 1,    # DOUGM/B-LexInfo-0.02
        'B::Fathom'             => 1,    # KSTAR/B-Fathom-0.07
        'Devel::Symdump'        => 1,    # ANDK/Devel-Symdump-2.08
        'URI'                   => 1,    # GAAS/URI-1.60
        'Compress::Zlib'        => 1,    # PMQS/IO-Compress-2.060
        'Module::Build'         => 1     # LEONT/Module-Build-0.4003
    },
    'verify_on' => "For security reasons, you must only install ModPerl on a single-user system.\n\nDo not install ModPerl without an experienced ModPerl administrator.\n\nModPerl is not compatible with ModRuid2.",
    'modself'   => sub {
        my ( $easy, $self_hr, $profile_hr ) = @_;

        # mod_perl is not currently supported with cPanel/WHM because
        # it requires a threaded Perl, which we don't deploy.
        if ( $profile_hr->{'Apache'}{'version'} eq '2_4' ) {
            $self_hr->{'hastargz'}                   = 0;
            $self_hr->{'skip'}                       = 1;
            $self_hr->{'treat_as_off_while_skipped'} = 1;
            return ( 0, 'ModPerl unsupported with Apache 2.4' );
        }

        #Case 59437: Mod_perl, MPM Event, and single threaded Perl cause EA build to fail.
        my $perlv = `$perl -v`;
        if ( $perlv !~ /thread-multi/i ) {
            $self_hr->{'implies'} = { 'Cpanel::Easy::Apache::MPMEvent' => 0, };
            $self_hr->{'verify_on'} .= "\n\nMod Perl and MPM Event cannot be used together because your currently installed version of Perl does not have multi-threading support.",;
        }

        # ExtUtils::Embed was moved into the perl core long ago, but
        # on Cent6 it is a separate package, and doesn't appear to be
        # installed by default.  It is included within the perl-5.8.8
        # package on Cent5.  Since Cent directly uses RHEL packages,
        # they should be the same, and since Cloudlinux bases itself
        # on RHEL/Cent, they should be also.
        if ( $easy->{'getos_releaseversion'} ge '6.0' ) {
            my $result = `$perl -MExtUtils::Embed -e 'print "hello";' 2>&1`;
            if ( $result =~ m/Can't locate/ ) {
                push @{ $self_hr->{'ensurepkg'} }, 'perl-ExtUtils-Embed';
            }
        }

        return ( 1, 'Ok' );
    },
    'dryrun' => {
        '2' => {
            'name'    => 'Configuring mod_perl',
            'command' => sub {
                my ($self) = @_;

                my $cmd_ref = [ $perl, qw(Makefile.PL MP_USE_DSO=1 MP_APXS=/usr/local/apache/bin/apxs) ];

                return $self->run_system_cmd_returnable($cmd_ref);
            },
        },
        '3' => {
            'name'    => 'Making mod_perl',
            'command' => sub {
                my ($self) = @_;
                return $self->run_system_cmd_returnable( ['make'] );
            },
        },
    },
    'step' => {
        '-1' => {
            'name'    => 'Apply event patch',
            'command' => sub {
                my ($self) = @_;
                $self->print_to_log_and_screen("Applying event patch to mod_perl");
                return $self->apply_patch( '../cppatch/event.patch', );
            },
        },
        '0' => {
            'name'    => 'Install mod_perl',
            'command' => sub {
                my ($self) = @_;
                return $self->run_system_cmd_returnable( [qw(make install)] );
            },
        },
        '1' => {
            'name'    => 'APXSing mod_perl into apache',
            'command' => sub {
                my ($self) = @_;
                my $module = 'mod_perl.so';
                if ( -e '/usr/local/apache/modules/' . $module ) {
                    return $self->ensure_loadmodule_in_httpdconf( 'perl', $module );
                }
                return ( 0, 'Failed to build mod_perl' );
            },
        },
    },
};

1;
