chiark / gitweb /
nailing-cargo: Search for TOML::Tiny near our (actual) script
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 8 May 2020 12:13:26 +0000 (13:13 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 8 May 2020 12:13:26 +0000 (13:13 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
nailing-cargo

index 60e509163abea2dc4e3cc8b114db535fac2d0fd9..1a315686af305a9b63542dc001044c9c733e8a7f 100755 (executable)
 #    [subdirs]
 #    subdir
 
+our $self;
+
 use strict;
-use TOML::Tiny::Faithful;
 use POSIX;
+
+BEGIN {
+  $self = $0;  $self =~ s{^.*/(?=.)}{};
+  my $deref = $0;
+  while ($deref =~ m{^/}) {
+    my $link = readlink $deref;
+    if (!defined $link) {
+      $! == EINVAL
+       or die "$self: checking our script location $deref: $!\n";
+      $deref =~ s{/[^/]+$}{}
+       or die "$self: unexpected script path: $deref\n";
+      unshift @INC, $deref."/TOML-Tiny/lib";
+      last;
+    }
+    last if $link !~ m{^/};
+    $deref = $link;
+  }
+}
+
 use Fcntl qw(LOCK_EX);
 use File::Compare;
-
-my $self = $0;  $self =~ s{^.*/(?=.)}{};
+use TOML::Tiny::Faithful;
 
 our $worksphere = getcwd() // die "$self: getcwd failed: $!\n";
 $worksphere =~ s{/[^/]+$}{} or die "$self: cwd \`$worksphere' unsupported!\n";