| 1 |
#!/bin/env perl -lw |
| 2 |
# |
| 3 |
# $Id: killdupes 2484 2008-12-09 10:20:31Z dmichelsen $ |
| 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 |
# killdupes - Remove duplicate packages from a directory, with or |
| 14 |
# without user interaction. Note that this only works |
| 15 |
# for packages named according to package standards. |
| 16 |
# |
| 17 |
|
| 18 |
use strict; |
| 19 |
use File::Basename; |
| 20 |
use Getopt::Std; |
| 21 |
use Data::Dumper; |
| 22 |
use vars qw/$opt_n/; |
| 23 |
|
| 24 |
getopts ":n"; |
| 25 |
|
| 26 |
my %packages; |
| 27 |
foreach my $file (@ARGV) { |
| 28 |
my $bn = basename $file; |
| 29 |
my ($pkg,$ver) = split /-/, $bn, 3; |
| 30 |
push @{$packages{$pkg}}, $file; |
| 31 |
} |
| 32 |
|
| 33 |
foreach my $pkg (sort keys %packages) { |
| 34 |
if (scalar @{$packages{$pkg}} > 1) { |
| 35 |
|
| 36 |
my @unlink; |
| 37 |
if ($opt_n) { |
| 38 |
# Non-interactive |
| 39 |
my %finfo; |
| 40 |
foreach my $file (@{$packages{$pkg}}) { |
| 41 |
$finfo{$file} = (stat($file))[9]; |
| 42 |
} |
| 43 |
@unlink = sort {$finfo{$a} <=> $finfo{$b}} keys %finfo; |
| 44 |
pop @unlink; |
| 45 |
} |
| 46 |
else { |
| 47 |
# Write dupes out to a tempfile |
| 48 |
my $tfile = "/tmp/kd.$pkg.$$"; |
| 49 |
open TFILE, ">>$tfile" or die "Cannot open file: $!"; |
| 50 |
print TFILE foreach @{$packages{$pkg}}; |
| 51 |
close TFILE; |
| 52 |
|
| 53 |
# Use ckitem for menu selection |
| 54 |
my ($rst) = `ckitem -p "Keep which file" -f $tfile`; |
| 55 |
unlink $tfile; |
| 56 |
|
| 57 |
chomp $rst; last if $rst eq 'q'; |
| 58 |
@unlink = grep { !/$rst/ } @{$packages{$pkg}}; |
| 59 |
} |
| 60 |
foreach (@unlink) { print "unlink: $_" ; unlink or die } |
| 61 |
|
| 62 |
} |
| 63 |
} |