#!/bin/sh
eval 'if [ -x /usr/local/cpanel/3rdparty/bin/perl ]; then exec /usr/local/cpanel/3rdparty/bin/perl -x -- $0 ${1+"$@"}; else exec /usr/bin/perl -x $0 ${1+"$@"}; fi;'
  if 0;

#!/usr/bin/perl

# cpanel - targz/Cpanel/Easy/EAccelerator.pm.tar.gz.d/eaccelerator/cpanel-install
#                                                 Copyright(c) 2013 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

BEGIN {
    unshift @INC, '/usr/local/cpanel';
}

use Cpanel::SafeRun   ();
use Cpanel::FileUtils ();
use Cpanel::SafeFile  ();
use IO::Handle;

my $installed       = 0;
my $current_version = 'eaccelerator-0.9.6.1';
my $legacy_version  = 'eaccelerator-0.9.5.3';

my @prefixes = ( '/usr/local', '/usr/local/php4' );
if (@ARGV) {
    @prefixes = @ARGV;
}

foreach my $php_prefix (@prefixes) {
    $php_prefix =~ s/\/$//;
    my $php_ini = $php_prefix . '/lib/php.ini';
    if ( $php_prefix eq '/usr/local/cpanel/3rdparty' || $php_prefix eq '/var/cpanel/3rdparty' ) {
        $php_ini = '/usr/local/cpanel/3rdparty/etc/php.ini';
    }
    unless ( -e $php_ini ) {
        print "Skipping install to $php_prefix, missing php.ini\n";
        next;
    }
    unless ( -e $php_prefix . '/bin/php' ) {
        print "Skipping install to $php_prefix, missing php\n";
        next;
    }
    unless ( -e $php_prefix . '/bin/phpize' ) {
        print "Skipping install to $php_prefix, missing phpize\n";
        next;
    }
    unless ( -e $php_prefix . '/bin/php-config' ) {
        print "Skipping install to $php_prefix, missing php-config\n";
        next;
    }

    # determine version to use
    print "Determining PHP version\n";
    my @cmd;
    if ( -e '/usr/local/cpanel/scripts/php.ini' ) {
        @cmd = ( $php_prefix . '/bin/php', '-c', '/usr/local/cpanel/scripts/php.ini', '-v' );
    }
    else {
        @cmd = ( $php_prefix . '/bin/php', '-v' );
    }
    my $phpout = Cpanel::SafeRun::saferunallerrors(@cmd);
    my $php_version;
    my $src_version;
    if ( $phpout =~ m/^PHP\s+(\d+\.\d+)\.\d+\S*\s+\(([^\)]+)\)/m ) {
        $php_version = $1;
    }
    else {
        print "Could not determine PHP version from output:\n";
        print $phpout;
        next;
    }

    my @configure = ( './configure', '--with-php-config=' . $php_prefix . '/bin/php-config' );

    # SEE: http://freebsd.1045724.n5.nabble.com/ports-151680-eAccelerator-Makefile-glitch-td3694636.html
    if ( $^O =~ /freebsd/i ) {
        push @configure, '--with-eaccelerator-shared-memory';
    }

    #push @configure, '--without-eaccelerator-use-inode';

    chdir 'eaccelerator-src' || die "Could not chdir to eaccelerator";

    system( $php_prefix . '/bin/phpize' );
    system(@configure)   && next;
    system('make clean') && next;
    system('make')       && next;
    system( 'make', 'install' ) && next;
    system( 'make', 'clean' )   && next;
    my ( $ok, $res ) = activate_eaccelerator( $php_prefix, $php_ini );
    $installed = 1 if $ok;
    print $res . "\n";

    chdir '..' || die "Could not return to starting directory: $!";
}

sub activate_eaccelerator {
    my $prefix    = shift;
    my $ini_file  = shift;
    my $phpconfig = $prefix . '/bin/php-config';

    my $ext_dir = Cpanel::SafeRun::saferun( $phpconfig, '--extension-dir' );
    chomp $ext_dir;

    return ( 0, q{Could not locate EAccelerator extenstion at '[_1]}, $ext_dir . '/eaccelerator.so' ) unless ( -e $ext_dir . '/eaccelerator.so' );

    my %eaccelerator_settings = (
        'shm_size'         => '16',
        'cache_dir'        => '/tmp/eaccelerator',
        'enable'           => '1',
        'optimizer'        => '1',
        'check_mtime'      => '1',
        'debug'            => '0',
        'filter'           => '',
        'shm_max'          => '0',
        'shm_ttl'          => '0',
        'shm_prune_period' => '0',
        'shm_only'         => '0',
        'compress'         => '1',
        'compress_level'   => '9'
    );

    my $ini_fh = IO::Handle->new();
    my $lock = Cpanel::SafeFile::safeopen( $ini_fh, '+<', $ini_file );
    return ( 0, "Failed to open $ini_file for writing: $!" ) unless $lock;
    my @ini_contents = ();
    my $extension_dir_line;    # This will actually point to the last line before the extension_dir line
    my $line_counter = -1;
    while ( my $line = readline($ini_fh) ) {
        next if ( $line =~ /^\s*(zend_)?extension(_ts)?\s*=\s*"?\S*eaccelerator\.so"?\s*$/ );
        if ( $line =~ /^\s*eaccelerator\.([^\s=]+)\s*=\s*['"]?([^\s'"]*?)['"]?\s*$/ ) {
            $eaccelerator_settings{$1} = $2;
            next;
        }
        if ( $line =~ /^\s*;?\s*extension_dir\s*=.*$/ ) {
            $extension_dir_line = $line_counter;
            next;
        }
        $line_counter++;
        push @ini_contents, $line;
    }

    seek( $ini_fh, 0, 0 );

    # Deal with no extension_dir line
    if ( !defined $extension_dir_line || $extension_dir_line < 0 ) {
        $extension_dir_line = $#ini_contents;
    }

    # Deal with last line missing newline
    if ( $#ini_contents >= 0 && $ini_contents[$#ini_contents] !~ /\n$/s ) {
        $ini_contents[$#ini_contents] .= "\n";
    }

    print $ini_fh @ini_contents[ 0 .. $extension_dir_line ];
    print $ini_fh qq{extension_dir = "$ext_dir"\nextension="eaccelerator.so"\n};
    foreach my $setting_name ( sort keys %eaccelerator_settings ) {
        my $setting_value = $eaccelerator_settings{$setting_name};
        print $ini_fh qq{eaccelerator.${setting_name}="${setting_value}"\n};
    }
    if ( ++$extension_dir_line <= $#ini_contents ) {
        print $ini_fh @ini_contents[ $extension_dir_line .. $#ini_contents ];
    }

    truncate( $ini_fh, tell($ini_fh) );
    Cpanel::SafeFile::safeclose( $ini_fh, $lock );

    return ( 1, 'EAccelerator extension activated' );
}
