# cpanel - Cpanel/Easy/Zendopt.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::Zendopt;

use strict;

use Cpanel::FileUtils        ();
use Cpanel::Version::Compare ();

our $easyconfig = {
    'version'     => '$Rev: 1853 $',
    'note'        => q{Please read documentation that can found by clicking "More Info" to fully understand this option},
    'verify_on'   => 'Are you fully aware of the implications of this option and able to administer it?',
    'name'        => 'Zend Optimizer/Guard Loader for PHP',
    'src_cd2'     => 'zendopt',
    'hastargz'    => 1,
    'alttargz'    => '',
    'url'         => 'http://www.zend.com/en/products/zend-guard#OptimizerL',
    'license_url' => 'http://static.zend.com/topics/guard-license-agreement.txt',
    'modself'     => sub {
        my $easy       = shift;
        my $self_hr    = shift;
        my $profile_hr = shift;

        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 4.x
            if ( Cpanel::Version::Compare::compare( $php_version, '<', '5.2' ) ) {
                $self_hr->{'hastargz'}                   = 0;
                $self_hr->{'skip'}                       = 1;
                $self_hr->{'treat_as_off_while_skipped'} = 1;
            }

            # PHP 5.2
            elsif ( Cpanel::Version::Compare::compare( $php_version, '<', '5.3' ) ) {

                # < 5.3 uses zend optimizer v3.3.9
                $self_hr->{'name'} = 'Zend Optimizer 3.3.9 for PHP';
            }

            # PHP 5.3
            elsif ( Cpanel::Version::Compare::compare( $php_version, '<', '5.4' ) ) {

                # php 5.3 uses zend guard 5.5 (despite what `php -v' says)
                $self_hr->{'name'} = 'Zend Guard Loader 5.5.0 for PHP';
            }

            # PHP 5.4
            elsif ( Cpanel::Version::Compare::compare( $php_version, '<', '5.5' ) ) {
                $self_hr->{'name'} = 'Zend Guard Loader 6.0 for PHP';
            }

            # PHP 5.5
            elsif ( Cpanel::Version::Compare::compare( $php_version, '<', '5.6' ) ) {
                $self_hr->{'name'} = 'Zend Guard Loader 7.0 for PHP';
            }

            # PHP 5.6
            elsif ( Cpanel::Version::Compare::compare( $php_version, '<', '5.7' ) ) {
                $self_hr->{'name'} = 'Zend Guard Loader 7.0 for PHP';
            }

            # Default case
            else {
                $self_hr->{'hastargz'}                   = 0;
                $self_hr->{'skip'}                       = 1;
                $self_hr->{'treat_as_off_while_skipped'} = 1;
                return;
            }
        }

        # PHP is turned off
        else {
            $self_hr->{'hastargz'}                   = 0;
            $self_hr->{'skip'}                       = 1;
            $self_hr->{'treat_as_off_while_skipped'} = 1;
            return;
        }

        my $chipset = $easy->{'cpu_bits'};
        my $os      = $easy->{'generic_os'};
        my $release = 'generic';

        $self_hr->{'alttargz'} = $os . '-' . $release . '-' . $chipset;
    },
    'when_i_am_off' => sub {
        my $self = shift;
        return ( 1, 'ok' ) if $self->get_param('makecpphp');

        require Cpanel::FileUtils;

        # TODO: We should probably comment out the values here rather than simply removing them.
        #
        # Not sure if the Zend installer will reuse old values

        my $ini_file = '/usr/local/lib/php.ini';
        if ( -e $ini_file ) {
            Cpanel::FileUtils::regex_rep_file(
                $ini_file,
                {
                    qr{^\s*zend_extension_manager\.optimizer(_ts)?\s*=\s*"?\S+\/Optimizer(_TS)?-[\d\.]+"?\s*$}is           => q{},
                    qr{^\s*zend_optimizer\.\S+.*$}is                                                                       => q{},
                    qr{^\s*zend_extension(_ts)?\s*=\s*"?\S+Zend(ExtensionManager|Optimizer|GuardLoader)(_TS)?\.so"?\s*$}is => q{},
                },
                {},
            );
        }

        return ( 1, 'Ok' );
    },
    'step' => {
        '1' => {
            'name'    => 'Install Zend Optimizer',
            'command' => sub {
                my $self = shift;
                if ( !-e './cpanel-install' ) {
                    return ( 0, q{cpanel-install script is missing in source directory} );
                }
                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(zend_extension zend_extension_ts)) {
            if ( exists $self->{'_'}{'php_ini'}{$key} ) {
                if ( grep /Zend(?:ExtensionManager|Optimizer|GuardLoader)(?:_TS)?\.so/, @{ $self->{'_'}{'php_ini'}{$key} } ) {
                    return 1;
                }
            }
        }
        return;
    };
}

1;
