package Cpanel::Easy::PHP5::Zlib;

# cpanel - Cpanel/Easy/PHP5/Zlib.pm               Copyright(c) 2011 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'      => 'Zlib',
    'ensurepkg' => [ 'zlib', 'zlib-devel' ],
    'note'      => 'Requires that zlib is installed and up to date',
    'url'       => 'http://www.php.net/manual/en/ref.zlib.php',
    'dryrun'    => {
        '0' => {
            'name'    => 'Find zlib path for PHP 5',
            'command' => sub {
                my ($self) = @_;

                my @opt;
                for my $pth (qw(/usr/lib64 /usr/lib /usr/local/lib64 /usr/local/lib /lib64 /lib)) {
                    next if !-d $pth;
                    if ( -e "$pth/libz.so" ) {
                        my $copy = $pth;
                        $copy =~ s/\/lib(64)?//;

                        # $copy = '/' if ! $copy; # if /lib64 or /lib
                        # last if !$copy; # if --with-zlib-dir without =whatever is not valid
                        @opt = ( '--with-zlib-dir', [$copy] );
                        $self->{'__'}{'LibPath'} = $pth;
                        $copy .= '/include';
                        $self->{'__'}{'IncludePath'} = $copy;
                        last;
                    }
                }

                $self->{'__'}{'ZlibDir'} = \@opt;
                return ( 1, 'Ok' );
            },
        },
        '1' => {
            'name'    => 'Create zlib test program',
            'command' => sub {
                my ($self) = @_;
                $self->{'__'}{'zLibSrc'} = <<'ZLIB';
                #include <stdio.h>
                #include "zlib.h"
                
                main() {
                    printf("cPanel zLib test");
                }
ZLIB
                return ( 1, 'Ok' );
            },
        },
        '2' => {
            'name'    => 'Compile zlib test program',
            'command' => sub {
                my ($self)   = @_;
                my $prog_src = '/cpzlib.c';
                my $prog     = '/cpzlib';
                open( my $src_h, '>', $prog_src );
                print {$src_h} $self->{'__'}{'zLibSrc'};
                close $src_h;

                system 'gcc', '-L' . $self->{'__'}{'LibPath'}, '-I' . $self->{'__'}{'IncludePath'}, '-o', $prog, $prog_src;
                my $test = `$prog`;

                if ( $test eq 'cPanel zLib test' ) {
                    return ( 1, 'Ok' );
                }
                else {
                    return ( 0, 'Compile failed' );
                }
            },
        },
        '3' => {
            'name'    => 'Remove zlib test files',
            'command' => sub {
                foreach my $file ( '/cpzlib.c', '/cpzlib' ) {
                    system( 'rm', $file );
                }
                return ( 1, 'Ok' );
            },
        },
    },
    'step' => {
        '0' => {
            'name'    => 'to config options',
            'command' => sub {
                my ($self) = @_;
                $self->add_to_configure( { '--with-zlib' => '' } );
                $self->add_to_configure( { @{ $self->{'__'}{'ZlibDir'} } } ) if @{ $self->{'__'}{'ZlibDir'} };
                return ( 1, 'Ok' );
            },
        },
    },
};

1;
