package Cpanel::Easy::PHP5::Intl;

# cpanel - Cpanel/Easy/PHP5/Intl.pm               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

use Cpanel::SafeRun          ();
use Cpanel::Sys::OS          ();
use Cpanel::Version::Compare ();

our $easyconfig = {
    'name'            => 'Intl',
    'url'             => 'http://www.php.net/manual/en/ref.intl.php',
    'ensurepkg'       => [qw{libicu-devel libicu-dev libicu icu-devel icu-dev icu}],
    'min_icu_version' => '3.4',
    'skip'            => 1,
    'dryrun'          => {
        '0' => {
            'name'    => 'Find ICU path for PHP 5',
            'command' => sub {
                my ($self) = @_;

                # Find the latest version of all installed versions of ICU
                my $best_version = 0;
                for my $pth (qw( /usr/local/easy /usr/local /usr )) {
                    next if !-d $pth;
                    if ( -e $pth . '/bin/icu-config' ) {
                        my $version = Cpanel::SafeRun::saferunallerrors( $pth . '/bin/icu-config', '--version' );
                        if ( $version =~ /^(\d+(?:\.\d+)+)/m ) {
                            $version = $1;
                            if ( Cpanel::Version::Compare::compare( $version, '>', $best_version ) ) {
                                $self->{'__'}{'icu_dir'}     = $pth;
                                $self->{'__'}{'icu_version'} = $version;
                                $best_version                = $version;
                            }
                        }
                    }
                }
                if ($best_version) {
                    return ( 1, 'Ok' );
                }

                return ( 0, 'Could not find [1] path', 'ICU' );
            },
        },
        '1' => {
            'name'    => 'Check ICU version for PHP 5',
            'command' => sub {
                my ($self) = @_;

                if ( !$self->{'__'}{'icu_version'} ) {
                    $self->print_alert_color( 'red', 'Could not determine which version of ICU is available.' );
                    $self->print_alert_color( 'red', 'If you encounter build problems, disable to PHP5 Intl module.' );
                    return ( 1, 'Ok' );
                }
                elsif ( Cpanel::Version::Compare::compare( $self->{'__'}{'icu_version'}, '<', $Cpanel::Easy::PHP5::Intl::easyconfig->{'min_icu_version'} ) ) {
                    return (
                        0, 'Detected version [_1], need [_2] or higher',
                        $self->{'__'}{'icu_version'},
                        $Cpanel::Easy::PHP5::Intl::easyconfig->{'min_icu_version'}
                    );
                }
                else {
                    return ( 1, 'Ok' );
                }
            },
        },
    },
    'step' => {
        '0' => {
            'name'    => 'to config options',
            'command' => sub {
                my ($self) = @_;
                $self->add_to_configure( { '--enable-intl' => '', '--with-icu-dir' => $self->{'__'}{'icu_dir'} } );
                return ( 1, 'Ok' );
            },
        },
    },
};

1;
