summaryrefslogtreecommitdiff
path: root/test/src/locate.pl
blob: ba74e030b60fdc0fb2ed1b760362aaffe8b4acec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env perl

use 5.010;
use strict;
use warnings;
use File::Find;
use Cwd;

my @dirs = grep { /^\// && -d } split(/:/, $ENV{PATH}), qw(
  /bin
  /usr/bin
  /usr/sbin
  /usr/lib
  /usr/libexec
  /usr/local/bin
  /usr/local/sbin
  /usr/local
  /opt
);

my %path = map { $_ => locate($_, @dirs) } @ARGV;

mkdir 'bin.sys'
  or die "bin.sys: $!"
  if not -d 'bin.sys';

foreach my $tool (keys %path) {
    next if not defined $path{$tool};
    print "$tool $path{$tool}\n";

    unlink "bin.sys/$tool";
    symlink $path{$tool}, "bin.sys/$tool"
      or warn "bin.sys/$tool -> $path{$tool}: $!\n";
}

sub locate {
    my ($tool, @dirs) = @_;

    # use die to break out of the find as soon
    # as we found it
    my $cwd = cwd;
    eval {
        find(
            sub {
                return $File::Find::prune = 1 unless -r -x -r;
                return unless $tool eq $_ and -x and -f _;
                die { found => $File::Find::name };
            },
            @dirs
        );
    };
    chdir $cwd;

    return (ref $@ eq ref {} and $@->{found}) ? $@->{found} : undef;
}