package Cpanel::Easy::OptLib::lua;

# cpanel - Cpanel/Easy/OptLib/lua.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 $version = qw{5.3.4};

sub get_version {
    my $out = `/opt/lua/bin/lua -v 2>&1`;
    my $version;

    if ( $out =~ /Lua\s+(\d+\.\d+\.\d+)\s+/xms ) {
        $version = $1;
    }

    return $version;
}

sub is_pic_enabled {
    my $out = `/usr/bin/readelf --relocs /opt/lua/lib/liblua.a 2>&1`;
    return ( $out =~ /GOT|PLT|JU?MP_SLOT/xms );
}

sub path_installed {
    my ($easy_obj) = @_;
    my @configure;

    @configure = qw( make linux install INSTALL_TOP=/opt/lua );

    return {
        'name'         => 'lua',
        'install_path' => "/opt/lua/",
        'itis_up2date' => sub {

            # check two things:
            #   - supports position independent code (PIC)
            #   - correct liblua version
            my $vers = get_version();
            my $pic  = is_pic_enabled();
            return ( $pic && $vers eq $version );
        },
        'working_path' => 'lua',
        'command_list' => [
            [qw( patch -N -p7 -i ../0001-Enable-position-independent-code-PIC-in-library.patch )],
            [@configure],
        ],
    };
}

1;
