summaryrefslogtreecommitdiff
path: root/test/src/locate.pl
blob: 6f752c13794c15d78f90c6492efdd0ab1bf7f3ab (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
#!/usr/bin/env perl
use strict;
use warnings;
use File::Find;
use Cwd;

my @dirs = grep { /^\// && -d } split(/:/, $ENV{PATH}), qw(
  /bin
  /usr/bin
  /usr/sbin
  /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 unless $tool eq $_ and -x $_ and -f _;
                die { found => $File::Find::name };
            },
            @dirs
        );
    };
    chdir $cwd;

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