summaryrefslogtreecommitdiff
path: root/glafp-utils/scripts/zap-if-same.prl
blob: 1bdb69ca992611d70a18b147c80c03abfc551164 (plain)
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
#   "zap" files in a directory tree if they're the same as somewhere else
#
#   zap normally means "rm", but "-s" means to put a symlink in place instead.
#
#   usage:
#	# delete all files in this dir that are same as in master copy...
#	% zap-if-same /src/ghc-master-copy
#	# use lndir to put in mere links...
#	% lndir /src/ghc-master-copy
#
#   a similar effect can be had with just...
#	% zap-if-same -s /src/ghc-master-copy

$Usage = "usage: zap-if-same [-s] master-dir\n";

$Action = 'rm';

if ($#ARGV >= 0 && $ARGV[0] eq '-s') {
    $Action = 'link';
    shift;
}

if ($#ARGV != 0) {
    die $Usage;
} else {
    $Master_dir = $ARGV[0];
    die "no such dir: $Master_dir\n$Usage" if ! -d $Master_dir;
}

open(F,"find . -type f -print |") || die "Cannot open find ($!)";
while (<F>) {
    chop;

    if ( -f "$Master_dir/$_" && &same_contents($_) ) { # ToDo: & not same file?
	print STDERR "$_ ...\n";
	unlink $_;
	if ($Action eq 'link') {
	    symlink("$Master_dir/$_", $_);
	}
    }
}
close(F);

sub same_contents {
    local($f) = @_;

    local($return_val) = 0;
    $return_val = system("cmp -s $Master_dir/$f $f") >> 8;
    ($return_val == 0) ? 1 : 0;
}