forked from eisop-plume-lib/plume-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path-remove
executable file
·67 lines (60 loc) · 1.77 KB
/
path-remove
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
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env perl
# Shorten a path environment variable by removing duplicates and
# non-existent directories.
# With optional argument "-r REGEXP", removes any matching path element.
# Works for either space- or colon- delimiated paths.
# Usage:
# # csh
# setenv PATH `echo $PATH | .../plume-scripts/path-remove`
# # sh
# export PATH=`echo $PATH | .../plume-scripts/path-remove`
# ## Does this even work??
# set path = (`echo $path | .../plume-scripts/path-remove`)
my $debug = 0;
# $debug = 1;
my $extra_regexp; # undefined if not specified on command line.
while (scalar(@ARGV) > 0) {
if ($ARGV[0] eq "-r") {
if (scalar(@ARGV) < 2) {
die "No regexp specified after -r";
}
shift @ARGV;
$extra_regexp = shift @ARGV;
} else {
die "path-remove: Unrecognized argument $ARGV[1]";
}
}
$splitchar = ":";
@result = ();
while (<>) {
my @pieces;
chomp;
if ($_ =~ /:/) {
@pieces = split(":");
$splitchar = ":";
} elsif ($_ =~ / /) {
@pieces = split(" ");
$splitchar = " ";
} else {
# no separators; assume colon, but don't set splitchar.
@pieces = split(":");
}
if ($debug) {
print STDERR "splitchar: $splitchar\n";
print STDERR "" . scalar(@pieces) . " initial components\n";
# print STDERR "Input: $_\n";
}
foreach $temp (@pieces) {
if (! -d $temp) { next; }
if (defined($already_seen{$temp})) { next; }
if ($temp =~ /vortex\/(M3|Smalltalk)\/bin\/shell$/) { next; }
if (defined($extra_regexp) && ($temp =~ /$extra_regexp/)) { next; }
# This directory should appear in the output
push(@result, $temp);
$already_seen{$temp} = 1;
}
}
if ($debug) {
print STDERR "" . scalar(@result) . " final components\n";
}
print join($splitchar, @result);