# cpanel12 - PDOMySQL                        Copyright(c) 2006-2007 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::PHP5::PDOMySQL;

our $easyconfig = {
    'name'    => 'PDO MySQL',
    'url'     => 'http://us.php.net/manual/en/ref.pdo-mysql.php',
    'version' => '$Rev: 3323 $',
    'depends' => { 'optmods' => { 'Cpanel::Easy::PHP5::PDO' => 1, }, },
    'implies'       => { 'Cpanel::Easy::PHP5::PDO' => 1, },
    'when_i_am_off' => sub {
        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*extension\s*=\s*"?pdo_mysql\.so"?\s*$}is => q{} }, {}, );
            }
        }
    },
    'step' => {
        '0' => {
            'name'    => 'to config options',
            'command' => sub {
                my ($self) = @_;

                # can't be shared if versioning is in use
                $self->{'__'}{'shared'} = $self->get_ns_value_from_profile('Cpanel::Easy::PHP5::Versioning') ? '' : 'shared';
                $self->add_to_configure( { '--with-pdo-mysql' => $self->{'__'}{'shared'} } );
                return ( 1, 'ok' );
            },
        },
        '1' => {
            'name'    => 'after build php.ini setup',
            'command' => sub {
                my ($self) = @_;
                return ( 1, 'Ok' ) if $self->get_param('makecpphp');

                # no need to load extension if it's compiled static
                return ( 1, 'Ok' ) unless $self->{'__'}{'shared'} eq 'shared';

                my $command = sub {
                    my ($self) = @_;

                    # add extension=pdo_mysql.so

                    my $append = '';
                    $self->set_phpini_object_key_from('/usr/local/lib/php.ini');

                  EXT:
                    for my $new (qw(pdo_mysql.so)) {
                        next EXT if grep / \A \Q$new\E \z /xms, @{ $self->{'_'}{'php_ini'}{'extension'} };
                        $append .= "extension=$new\n";
                    }

                    if ($append) {
                        if ( open my $ini_fh, '>>', '/usr/local/lib/php.ini' ) {
                            print {$ini_fh} $append;
                            close $ini_fh;
                        }
                        else {
                            $self->print_alert( q{Could not append '[_1]' with '[_2]': [_3]}, '/usr/local/lib/php.ini', $append, $! );
                        }
                    }

                    return ( 1, 'ok' );
                };

                my ( $v, $spec ) = $self->get_php_version();
                return $self->add_to_modify_later_queue(
                    'Cpanel::Easy::PHP5::' . $spec,
                    {
                        'step' => {
                            '15.41' => {
                                'name'    => 'PDO MySQL to php.ini',
                                'command' => $command,
                            },
                        }
                    },
                );
            },
        },
    },
};

1;
