[project @ 1996-07-19 18:36:04 by partain]
[ghc-hetmet.git] / glafp-utils / scripts / zap-if-same.prl
1 #   "zap" files in a directory tree if they're the same as somewhere else
2 #
3 #   zap normally means "rm", but "-s" means to put a symlink in place instead.
4 #
5 #   usage:
6 #       # delete all files in this dir that are same as in master copy...
7 #       % zap-if-same /src/ghc-master-copy
8 #       # use lndir to put in mere links...
9 #       % lndir /src/ghc-master-copy
10 #
11 #   a similar effect can be had with just...
12 #       % zap-if-same -s /src/ghc-master-copy
13
14 $Usage = "usage: zap-if-same [-s] master-dir\n";
15
16 $Action = 'rm';
17
18 if ($#ARGV >= 0 && $ARGV[0] eq '-s') {
19     $Action = 'link';
20     shift;
21 }
22
23 if ($#ARGV != 0) {
24     die $Usage;
25 } else {
26     $Master_dir = $ARGV[0];
27     die "no such dir: $Master_dir\n$Usage" if ! -d $Master_dir;
28 }
29
30 open(F,"find . -type f -print |") || die "Cannot open find ($!)";
31 while (<F>) {
32     chop;
33
34     if ( -f "$Master_dir/$_" && &same_contents($_) ) { # ToDo: & not same file?
35         print STDERR "$_ ...\n";
36         unlink $_;
37         if ($Action eq 'link') {
38             symlink("$Master_dir/$_", $_);
39         }
40     }
41 }
42 close(F);
43
44 sub same_contents {
45     local($f) = @_;
46
47     local($return_val) = 0;
48     $return_val = system("cmp -s $Master_dir/$f $f") >> 8;
49     ($return_val == 0) ? 1 : 0;
50 }