# cpanel - Cpanel/Easy/EAccelerator.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

package Cpanel::Easy::EAccelerator;

use strict;

use Cpanel::Version::Compare ();

our $version = '0.9.6.1';

our $easyconfig = {
    'version'   => $version,
    'note'      => "Caches compiled PHP pages ONLY when running in a persistent (DSO/FCGID) manner",
    'verify_on' => 'EAccelerator is incompatible with xCache',
    'name'      => qq{EAccelerator $version for PHP},
    'src_cd2'   => 'eaccelerator',
    'hastargz'  => 1,
    'url'       => 'http://eaccelerator.net/',
    'modself'   => sub {
        my $easy       = shift;
        my $self_hr    = shift;
        my $profile_hr = shift;

        # append 'verify_on' message only if Apache 2.2 has been selected, since this is when ITK is an option
        my $and_itk = ( '2_2' eq $easy->{'working_profile'}{'Apache'}{'version'} ) ? ' and ITK' : '';
        $self_hr->{'verify_on'} .= $and_itk;

        # EAccelerator is not compatible with Cpanel::Easy::Apache::MPMItk
        if ( $profile_hr->{'Cpanel::Easy::Apache::MPMItk'} ) {
            $self_hr->{'hastargz'}                   = 0;
            $self_hr->{'skip'}                       = 1;
            $self_hr->{'treat_as_off_while_skipped'} = 1;
        }

        # Only supported under PHP 5.x
        # All other versions of PHP aren't supported (e.g. Custom OptMods of PHP 4.x)
        if ( $profile_hr->{'Cpanel::Easy::PHP5'} ) {

            # This controls the ordering of the build, forcing this module to be built *after* PHP
            $self_hr->{'depends'}{'optmods'} = $easy->get_php_optmods_depends($profile_hr);

            my $php_version = $easy->get_php_version();

            # PHP 5.5 not supported as of 7/8/13
            if ( Cpanel::Version::Compare::compare( $php_version, '>=', '5.5' ) ) {
                $self_hr->{'hastargz'}                   = 0;
                $self_hr->{'skip'}                       = 1;
                $self_hr->{'treat_as_off_while_skipped'} = 1;
            }
        }
        else {
            $self_hr->{'hastargz'}                   = 0;
            $self_hr->{'skip'}                       = 1;
            $self_hr->{'treat_as_off_while_skipped'} = 1;
        }
    },
    'when_i_am_off' => sub {

        require Cpanel::FileUtils;

        # TODO: We should probably comment out the values here rather than simply removing them.
        # That would imply we're going to use the commended out values down below where we turn the extension on though.

        my $self = shift;
        if ( !$self->get_param('makecpphp') ) {
            if ( -e '/usr/local/lib/php.ini' ) {
                Cpanel::FileUtils::regex_rep_file( '/usr/local/lib/php.ini', { qr{^\s*(zend_)?extension(_ts)?\s*=\s*"?\S*eaccelerator\.so"?\s*$}is => q{}, qr{^\s*eaccelerator\..*$}is => q{} }, {}, );
            }
        }
        return ( 1, 'Ok' );
    },
    'step' => {
        '1' => {
            'name'    => 'Install EAccelerator',
            'command' => sub {
                my $self = shift;
                if ( !-e '../eaccelerator/cpanel-install' ) {
                    return ( 0, q{cpanel-install script is missing in source directory} );
                }
                my @rc = $self->run_system_cmd_returnable( [ '../eaccelerator/cpanel-install', '/usr/local' ] );
                return @rc;
            },
        },
    },
};

sub get_extension_status_coderef {
    return sub {
        my ($self) = @_;

      KEYS:
        for my $key (qw(extension extension_ts zend_extension zend_extension_ts)) {
            if ( exists $self->{'_'}{'php_ini'}{$key} ) {
                if ( grep / eaccelerator [.] so /xi, @{ $self->{'_'}{'php_ini'}{$key} } ) {
                    return 1;
                }
            }
        }
        return;
      }
}

1;
