package Cpanel::Easy::PHP5::Zip;

# cpanel - Cpanel/Easy/PHP5/Zip.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'   => 'Zip',
    'url'    => 'http://www.php.net/manual/en/ref.zip.php',
    'dryrun' => {
        '0' => {
            'name'    => 'Find zlib path for PHP 5 Zip',
            '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;
                        $self->{'__'}{'IncludePath'} = $copy . '/include';
                        last;
                    }
                }

                if ( scalar @opt != 2 ) {
                    return ( 0, 'Could not find [1] path', 'zlib' );
                }
                else {
                    $self->{'__'}{'ZlibDir'} = \@opt;
                    return ( 1, 'Ok' );
                }
            },
        },
        '1' => {
            'name'    => 'Create zlib test program for PHP 5 Zip',
            '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 for PHP 5 Zip',
            '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 for PHP 5 Zip',
            '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( { '--enable-zip' => '' } );
                $self->add_to_configure( { @{ $self->{'__'}{'ZlibDir'} } } ) if @{ $self->{'__'}{'ZlibDir'} };

                return ( 1, 'Ok' );
            },
        },
    },
};

1;
