package Cpanel::Easy::PHP5::GD;

# cpanel - Cpanel/Easy/PHP5/GD.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'      => 'GD',
    'ensurepkg' => [qw{gd libXpm libXpm-dev libXpm-devel xorg-x11-devel xorg-x11-dev libpng-devel libpng-dev libjpeg-devel libjpeg-dev libX11-devel}],
    'depends'   => { 'optmods' => { 'Cpanel::Easy::PHP5::Zlib' => 1, }, },
    'implies' => { 'Cpanel::Easy::PHP5::Zlib' => 1, },
    'url'     => 'http://www.php.net/manual/en/ref.image.php',
    'dryrun'  => {
        '0' => {
            'name'    => 'Find jpeg path for PHP 5',
            'command' => sub {
                my ($self) = @_;
                my @jpeg = ();
                for my $pth (qw( /usr/local /usr )) {
                    next if !-d $pth;
                    if ( -e "$pth/lib/libjpeg.so" || -e "$pth/lib64/libjpeg.so" ) {
                        push @jpeg, '--with-jpeg-dir', $pth;
                        last;
                    }
                }
                if ( scalar @jpeg ne 2 ) {
                    return ( 0, 'Could not find [1] path', 'jpeg' );
                }
                else {
                    $self->{'__'}{'Jpeg'} = \@jpeg;

                    return ( 1, 'Ok' );
                }
            },
        },
        '1' => {
            'name'    => 'Find png path for PHP 5',
            'command' => sub {
                my ($self) = @_;
                my @png = ();
                for my $pth (qw( /usr/local /usr )) {
                    next if !-d $pth;
                    if ( -e "$pth/lib/libpng.so" || -e "$pth/lib64/libpng.so" ) {
                        push @png, '--with-png-dir', $pth;
                        last;
                    }
                }
                if ( scalar @png ne 2 ) {
                    return ( 0, 'Could not find [1] path', 'png' );
                }
                else {
                    $self->{'__'}{'Png'} = \@png;

                    return ( 1, 'Ok' );
                }
            },
        },
        '2' => {
            'name'    => 'Find xpm path for PHP 5',
            'command' => sub {
                my ($self) = @_;
                my @xpm = ();
                for my $pth (qw( /usr/local /usr /usr/X11R6 )) {
                    next if !-d $pth;
                    if ( -e "$pth/lib/libXpm.so" || -e "$pth/lib64/libXpm.so" ) {
                        push @xpm, '--with-xpm-dir', $pth;
                        last;
                    }
                }

                if ( scalar @xpm ne 2 ) {
                    return ( 0, 'Could not find [1] path', 'xpm' );
                }
                else {
                    $self->{'__'}{'Xpm'} = \@xpm;

                    return ( 1, 'Ok' );
                }
            },
        },
        '3' => {
            'name'    => 'Find GD path for PHP 5',
            'command' => sub {
                my ($self) = @_;

                my @gd = ();
                for my $pth (qw( /usr/local/lib64 /usr/lib64 /lib64  /usr/local/lib /usr/lib /lib )) {
                    next if !-d $pth;
                    if ( -e "$pth/libgd.so" ) {
                        push @gd, '--with-gd', '';
                        $self->{'__'}{'LibPath'} = $pth;
                        my $copy = $pth;
                        $copy =~ s/lib(64)?/include/;
                        $self->{'__'}{'IncludePath'} = $copy;
                        last;
                    }
                }

                if ( scalar @gd ne 2 ) {
                    return ( 0, 'Could not find [1] path', 'GD' );
                }
                else {
                    $self->{'__'}{'GD'} = \@gd;

                    return ( 1, 'Ok' );
                }
            },
        },
        '4' => {
            'name'    => 'Create GD test program',
            'command' => sub {
                my ($self) = @_;
                $self->{'__'}{'zLibSrc'} = <<'GD';
                    #include <stdio.h>
                    #include "gd.h"

                    main() {
                        printf("cPanel GD test");
                    }
GD
                return ( 1, 'Ok' );
            },
        },
        '5' => {
            'name'    => 'Compile GD test program',
            'command' => sub {
                my ($self)   = @_;
                my $prog_src = '/cpgd.c';
                my $prog     = '/cpgd';

                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 GD test' ) {
                    return ( 1, 'Ok' );
                }
                else {
                    return ( 0, 'Compile failed' );
                }
            },
        },
        '6' => {
            'name'    => 'Remove GD test files',
            'command' => sub {
                foreach my $file ( '/cpgd.c', '/cpgd' ) {
                    system( 'rm', $file );
                }
                return ( 1, 'Ok' );
            },
        },
    },
    'step' => {
        '0' => {
            'name'    => 'to config options',
            'command' => sub {
                my ($self) = @_;
                $self->add_to_configure(
                    {
                        '--with-gd' => '',
                        @{ $self->{'__'}{'Jpeg'} },
                        @{ $self->{'__'}{'Png'} },
                        @{ $self->{'__'}{'Xpm'} },
                    }
                );
                return ( 1, 'Ok' );
            },
        },
    },
};

1;
