michael@0: #! /bin/perl michael@0: michael@0: ######################################################################## michael@0: # michael@0: # /u/sonmi/bin/path_uniq michael@0: # michael@0: # this script makes components of a PATH like string unique cand prints michael@0: # it to stdout michael@0: # michael@0: # parameters michael@0: # ---------- michael@0: # PATH michael@0: # michael@0: # options michael@0: # ------- michael@0: # -d delimiter - default : michael@0: # -s shortens the path michael@0: # michael@0: # usefull enhancements: in the usage part, try to guess what was meant as michael@0: # a path and echo it to stdout to not break for PATHs with blanks michael@0: # michael@0: ######################################################################## michael@0: michael@0: sub usage { michael@0: print STDERR "usage $0 [-s] [-d ] PATH\n"; michael@0: print STDERR " this script makes components of the PATH unique, if you\n"; michael@0: print STDERR " pass in a searchpath A:B:C:A:B:E it will print A:B:C:E to\n"; michael@0: print STDERR " the stdout\n\n"; michael@0: print STDERR " -s will mercylessly cut components from the path, \n"; michael@0: print STDERR " use at your own risk\n\n"; michael@0: print STDERR " the parameters you gave were: \n"; michael@0: for ( $i = 0; $i <= $#ARGV; $i++ ) { michael@0: print STDERR " $ARGV[$i]\n"; michael@0: } michael@0: exit ; michael@0: } michael@0: michael@0: michael@0: $i = 0; michael@0: $j = 0; michael@0: $delimiter = ":"; michael@0: $searchpath = ""; michael@0: @pathcomponents; michael@0: $found=0; michael@0: $newpath=""; michael@0: $shorten=0; michael@0: michael@0: for ( $i=0; $i <= $#ARGV; $i++) { michael@0: if ( $ARGV[$i] eq '-d' ) { michael@0: $delimiter = $ARGV[++$i]; michael@0: } elsif ( $ARGV[$i] eq '-s' ) { michael@0: $shorten=1; michael@0: } else { michael@0: $searchpath = $ARGV[$i]; michael@0: } michael@0: } michael@0: if ( $searchpath eq "" ) { michael@0: usage; michael@0: } michael@0: #print STDERR "delimiter $delimiter\n"; michael@0: #print STDERR "shorten $shorten\n"; michael@0: #print STDERR "searchpath $searchpath\n"; michael@0: michael@0: @pathcomponents=split($delimiter, $searchpath); michael@0: michael@0: for ( $i = 0; $i <= $#pathcomponents; $i++ ) { michael@0: $found=0; michael@0: if ( $shorten == 1 ) { michael@0: if ( "\/tools\/ns-arch\/sparc_sun_solaris2\.4\/lib\/sparcworks\/SUNWspro/bin" eq $pathcomponents[$i] || michael@0: "\/h\/tortoise\/export\/share\/builds\/tools\/sparc_sun_solaris2\.5\.1\/perl5\.004\/bin" eq $pathcomponents[$i] || michael@0: "\/usr\/dist\/local\/exe" eq $pathcomponents[$i] || michael@0: "\/opt\/SUNWspro\/bin" eq $pathcomponents[$i] || michael@0: "\/opt\/SUNWwabi\/bin" eq $pathcomponents[$i] || michael@0: "\/u\/svbld\/bin" eq $pathcomponents[$i] || michael@0: "\/usr\/demos" eq $pathcomponents[$i] || michael@0: "\/usr\/audio\/bin" eq $pathcomponents[$i] || michael@0: "\/usr\/openwin\/demo" eq $pathcomponents[$i] || michael@0: "\/tools\/contrib\/bin" eq $pathcomponents[$i] || michael@0: "\/usr\/etc\/" eq $pathcomponents[$i] || michael@0: "\/usr\/demos\/bin" eq $pathcomponents[$i] ) { michael@0: michael@0: michael@0: #print "dumped: $pathcomponents[$i]\n"; michael@0: next; michael@0: } michael@0: #print "keep: $pathcomponents[$i]\n"; michael@0: } michael@0: for ( $j = 0; $j < $i; $j++ ) { michael@0: if ( $pathcomponents[$j] eq $pathcomponents[$i] ) { michael@0: #print "$i and $j match - $pathcomponents[$i] - $pathcomponents[$j]\n"; michael@0: $found=1; michael@0: last; michael@0: } michael@0: } michael@0: if ( $found == 0 ) { michael@0: #print "$pathcomponents[$i]:"; michael@0: if ($i == 0) { michael@0: $newpath = $pathcomponents[$i]; michael@0: } else { michael@0: $newpath=join($delimiter, $newpath,$pathcomponents[$i]); michael@0: } michael@0: } michael@0: } michael@0: print "$newpath\n"; michael@0: exit; michael@0: michael@0: