| 1 |
#!/bin/env perl -lw |
| 2 |
# |
| 3 |
# $Id: pkgmanifest 830 2006-09-19 18:23:08Z comand $ |
| 4 |
# |
| 5 |
# Copyright 2006 Cory Omand <comand@blastwave.org> |
| 6 |
# All rights reserved. Use is subject to license terms. |
| 7 |
# |
| 8 |
# Redistribution and/or use, with or without modification, is |
| 9 |
# permitted. This software is without warranty of any kind. The |
| 10 |
# author(s) shall not be liable in the event that use of the |
| 11 |
# software causes damage. |
| 12 |
# |
| 13 |
# pkgmanifest - strip one or more path levels from a list of files. |
| 14 |
# |
| 15 |
|
| 16 |
use strict; |
| 17 |
use File::Basename; |
| 18 |
use File::Spec::Functions qw/splitdir catdir/; |
| 19 |
use Getopt::Long qw/:config no_ignore_case gnu_getopt/; |
| 20 |
|
| 21 |
my $self = basename $0; |
| 22 |
my $VERSION = "1.0"; |
| 23 |
|
| 24 |
# Get command line options |
| 25 |
my ($strip_path, $prefix); |
| 26 |
my $include_prefix = 0; |
| 27 |
my $depth = 0; |
| 28 |
GetOptions( |
| 29 |
'strip|s=s' => \$strip_path, |
| 30 |
'prefix|p=s' => \$prefix, |
| 31 |
'depth|d=s' => \$depth, |
| 32 |
'incprefix!' => \$include_prefix, |
| 33 |
'help|h' => \&usage, |
| 34 |
'version|V' => sub { |
| 35 |
print STDERR "$self v$VERSION\n"; |
| 36 |
exit 2; |
| 37 |
}, |
| 38 |
) or usage(); |
| 39 |
|
| 40 |
sub usage { |
| 41 |
print STDERR "Usage: $self [--prefix <path>] [--depth <int>] [--no|incprefix] [--strip <path>] < path_list\n"; |
| 42 |
exit 1; |
| 43 |
} |
| 44 |
|
| 45 |
my $pf_elems = scalar splitdir($prefix) if $prefix; |
| 46 |
$pf_elems = 0 unless $pf_elems; |
| 47 |
$pf_elems += $depth; |
| 48 |
|
| 49 |
my %paths; |
| 50 |
foreach (<>) { |
| 51 |
chomp; |
| 52 |
s/^$strip_path// if $strip_path; |
| 53 |
|
| 54 |
unless (-d) { |
| 55 |
my @path = splitdir(dirname($_)); |
| 56 |
for (my $i = 0; $i <= $#path; $i++) { |
| 57 |
my $npath = catdir(@path[0..$i]); |
| 58 |
$paths{$npath}++ if $i >= $pf_elems; |
| 59 |
} |
| 60 |
} |
| 61 |
$paths{$_}++; |
| 62 |
} |
| 63 |
|
| 64 |
foreach (sort keys %paths) { |
| 65 |
next if /^$prefix$/ and not $include_prefix; |
| 66 |
print; |
| 67 |
} |
| 68 |
|