package Cpanel::Easy::PHP5::Pgsql;

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

our $easyconfig = {
    'name'   => 'PGsql',
    'note'   => 'Requires PostgreSQL 7.3 or higher to already be installed. You can do that with /usr/local/cpanel/scripts/installpostgres',
    'url'    => 'http://www.php.net/manual/en/ref.pgsql.php',
    'dryrun' => {
        '0' => {
            'name'    => 'Check postgresql',
            'command' => sub {
                my ($self) = @_;

                my $psql =
                    -x '/usr/bin/psql'       ? '/usr/bin/psql'
                  : -x '/usr/local/bin/psql' ? '/usr/local/bin/psql'
                  :                            '';

                my $got_psql = 0;
                if ($psql) {
                    $self->run_system_cmd( [ $psql, '--version' ], 0, 1 );

                    #  psql seems to like to exit -1 when open3() does it, but not via normal shell or qx...
                    if ( $self->{'last_run_system_cmd'} =~ m{ \A \s* RC }xms ) {
                        $self->{'last_run_system_cmd'} = qx($psql --version);
                    }

                    if ( $self->{'last_run_system_cmd'} =~ /(\d+)\.(\d+)\.(\d+)/ ) {
                        my $pmajor = $1;
                        my $pminor = $2;
                        if ( $pmajor > 7 || ( $pmajor == 7 && $pminor >= 3 ) ) {
                            $got_psql = 1;
                        }
                    }
                }

                if ( !$got_psql ) {
                    $self->{'__'}{'SKIP'} = 1;
                    $self->print_alert('postgres is not installed, skipping postgres flags');
                }

                return ( 1, 'ok' );
            },
        },
        '1' => {
            'name'    => 'Check postgresql for PHP 5',
            'command' => sub {
                my ($self) = @_;

                if ( -e '/usr/local/bin/pg_config' ) {
                    $self->{'__'}{'PgPath'} = '/usr/local';
                    return ( 1, 'Ok' );
                }
                elsif ( -e '/usr/bin/pg_config' ) {
                    $self->{'__'}{'PgPath'} = '/usr';
                    return ( 1, 'Ok' );
                }
                else {
                    return ( 0, 'Unable to find [_1] path', 'postgresql' );
                }
            },
        },
    },
    'step' => {
        '0' => {
            'name'    => 'to config options',
            'command' => sub {
                my ($self) = @_;
                $self->add_to_configure( { '--with-pgsql' => $self->{'__'}{'PgPath'} } );
                return ( 1, 'Ok' );
            },
        },
    },
};

1;
