aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2021-06-29 18:39:30 +0200
committerJohn Ankarström <john@ankarstrom.se>2021-06-29 18:39:30 +0200
commit8a00961524ba5bf2a65eaab1ccc496cf02471280 (patch)
tree28c7165921fbc8cc7f33509eeaabff88ecd6bf75
parent657104d7ff9ced7908a2dcf398ccee6b06fac3a6 (diff)
downloadxutil-8a00961524ba5bf2a65eaab1ccc496cf02471280.tar.gz
Add 're!' utility
-rw-r--r--Makefile3
-rwxr-xr-xre!48
2 files changed, 50 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 20f5c13..3c3e014 100644
--- a/Makefile
+++ b/Makefile
@@ -15,8 +15,9 @@ EXEC != ls -l | perl -ne ' \
} \
} \
'
+IEXEC != echo $(EXEC) | sed -E 's,^| ,&/usr/local/bin/,g'
install:
install -m 644 *.1 /usr/local/man/man1/
install $(EXEC) /usr/local/bin
- ./re! `echo $(EXEC) | sed -E 's,^| ,&/usr/local/bin/,g'`
+ re! $(IEXEC)
diff --git a/re! b/re!
new file mode 100755
index 0000000..1314ae9
--- /dev/null
+++ b/re!
@@ -0,0 +1,48 @@
+#!/usr/bin/env perl
+
+# re! -- rewrite shebangs
+
+use strict;
+
+my $test = 0;
+sub usage { die "usage: $0 [-n] file ...\n" }
+
+if (@ARGV and $ARGV[0] eq '-n') {
+ $test = 1;
+ shift @ARGV;
+}
+usage if not @ARGV;
+
+for my $file (@ARGV) {
+ open my $o, '<', $file or die "could not open $file: $!\n";
+
+ # parse shebang
+ my $shebang = <$o>;
+ $shebang =~ /^#!/ or die "no shebang: $file\n";
+ $shebang =~ /^#!\s*(\S+)\s*(.*)/;
+ my ($old, $args) = ($1, $2);
+
+ # validate path
+ next if -x $old;
+
+ # get new path
+ (my $basename = $old) =~ s,.*/,,;
+ chomp(my $new = `which $basename`);
+ $new or die "could not find $basename\n";
+ next if $old eq $new;
+
+ # print results if test
+ if ($test) {
+ print "$file: $old -> $new\n";
+ next;
+ }
+
+ # write new shebang
+ open my $n, '>', "$file.tmp" or die "could not open $file.tmp: $!\n";
+ print $n "#!$new $args\n";
+ print $n $_ while <$o>;
+
+ close for ($o, $n);
+ system('mv', "$file.tmp", $file) == 0
+ or die "could not overwrite $file\n";
+}