package Cpanel::Easy::Utils::Lib;

# cpanel - Cpanel/Easy/Utils/Lib.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

use strict;
use warnings;
use Cpanel::SafeFind ();

# Search order is for EasyApache stuff first, system libraries, then customer-installed
# libraries last
our @LD_PATH = qw( /opt /usr/lib64 /lib64 /usr/local/lib64 /usr/lib /lib /usr/local/lib );

# Searches @LD_PATH to for a library matching a filename
sub get_library_path {
    my $self = shift;
    my $lib  = shift;
    my $path;

    # looping over each directory probably seems dumb, but we're doing this to help
    # short-circuit processing every file in every directory in @LD_PATH once we
    # found a match.
  LIB:
    foreach my $p (@LD_PATH) {
        last if $path;

        next LIB if ( !-d $p );
        if ( -e "$p/$lib" ) {    # try a quick search first, if nada, do the longer search
            $path = "$p/$lib";
        }
        else {
            local $_;
            no warnings 'once';    # prevent File::Find::name warning

            Cpanel::SafeFind::find(
                {
                    'wanted' => sub {
                        !defined $path && $_ =~ /\.so$/ && $_ eq $lib && ( $path = $File::Find::name );
                    },
                    'no_chdir' => 1
                },
                $p
            );
        }
    }

    return $path;
}

1;

