# cpanel - Cpanel/Easy/Xcache.pm                  Copyright(c) 2015 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::Xcache;

use strict;

my $version = "3.2.0";

our $easyconfig = {
    'version'   => $version,
    'note'      => qq{Please see http://xcache.lighttpd.net for info about this option},
    'verify_on' => 'XCache is incompatible with EAccelerator and Zend Opcache',
    'name'      => "XCache $version for PHP",
    'src_cd2'   => 'xcache',
    'hastargz'  => 1,
    'url'       => 'http://xcache.lighttpd.net',
    'implies'   => {
        'Cpanel::Easy::EAccelerator'  => 0,
        'Cpanel::Easy::PHP5::Opcache' => 0
    },
    'modself' => sub {
        my $easy       = shift;
        my $self_hr    = shift;
        my $profile_hr = shift;

        # Only supported under PHP 5.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);
        }

        # All other versions of PHP aren't supported (e.g. Custom OptMods of PHP 4.x)
        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 commented 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*xcache\.so"?\s*$}is => q{}, qr{^\s*xcache\..*$}is => q{} }, {}, );
            }
        }
        return ( 1, 'Ok' );
    },
    'step' => {
        '1' => {
            'name'    => 'Install Xcache',
            'command' => sub {
                my $self = shift;
                if ( !-e './cpanel-install' ) {
                    return ( 0, qq{cpanel-install script is missing in source directory @{ [ qx/pwd/] } } );
                }
                my @rc = $self->run_system_cmd_returnable( [ './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 / xcache [.] so /xi, @{ $self->{'_'}{'php_ini'}{$key} } ) {
                    return 1;
                }
            }
        }
        return;
      }
}

1;
