nice new tool to show version of feisty meow
[feisty_meow.git] / scripts / files / zap_the_dir.pl
1 #!/usr/bin/perl
2
3 ###############################################################################
4 #                                                                             #
5 #  Name   : zap_the_dir                                                       #
6 #  Author : Chris Koeritz                                                     #
7 #  Rights : Copyright (C) 1996-$now by Author                                 #
8 #                                                                             #
9 #  Purpose:                                                                   #
10 #                                                                             #
11 #    Processes arguments that are expected to be directories.  The directory  #
12 #  has all the known junk files strained out of it and then it is removed     #
13 #  if it is empty.                                                            #
14 #                                                                             #
15 ###############################################################################
16 #  This program is free software; you can redistribute it and/or modify it    #
17 #  under the terms of the GNU General Public License as published by the Free #
18 #  Software Foundation; either version 2 of the License or (at your option)   #
19 #  any later version.  See: "http://www.gruntose.com/Info/GNU/GPL.html" for a #
20 #  version of the License.  Please send any updates to "fred@gruntose.com".   #
21 ###############################################################################
22
23 require "filename_helper.pl";
24
25 ############################################################################
26
27 sub remove_whackables {
28   local($from_dir) = @_;
29
30   # open the directory and grab all the files out.
31   opendir CHECK, $from_dir;
32   local(@files) = readdir(CHECK);
33   closedir CHECK;
34
35   # iterate over the potentially whackable files.
36   local($fname) = "";
37   foreach $fname (@files) {
38 #print "filename is $fname\n";
39     # check if this one matches one of our patterns.
40     if (! &important_filename($fname)) {
41       # it's a junk file; whack it.
42       $fname = $from_dir . '/' . $fname;
43 #print "whacking: $fname.\n";
44       unlink $fname;
45       if (-f "$fname") {
46         print "cleaning file: $fname\n";
47         &recursive_delete($fname);
48       } else {
49         print "skipping item rather than deleting: $fname\n";
50       }
51     }
52   }
53 }
54
55 ############################################################################
56
57 sub zap_the_dir {
58   local(@zap_list) = @_;
59   local($to_zap) = "";
60   foreach $to_zap (@zap_list) {
61     chomp $to_zap;
62
63 #print "to_zap is $to_zap\n";
64     if (! -d $to_zap) {
65       print "$to_zap is not a directory.\n";
66       next;
67     }
68
69     if ($to_zap =~ /^.*\.svn.*$/) {
70 #print "hey found a .svn dir!  skipping.\n";
71       next;
72     }
73
74     &remove_whackables($to_zap);
75     opendir WHACK, $to_zap;
76     @files_left = readdir(WHACK);
77     closedir WHACK;
78   
79     local($name) = "";
80     local($seen_anything) = "";
81     foreach $name (@files_left) {
82       # check if directory has nothing but the two directory entries in it.
83       if ( ($name ne ".") && ($name ne "..") ) {
84         if ($to_zap =~ /^.*\.svn.*$/) {
85           print "not empty: \"$to_zap/$name\"\n";
86         }
87         $seen_anything = "true";
88         break; 
89       }
90     }
91
92     if (length($seen_anything)) {
93       print "not empty: \"$to_zap\"\n";
94     } else {
95       # this should now work, if the directory is empty.
96       if (!rmdir $to_zap) {
97         print "still in use: \"$to_zap\"\n";
98         next;
99       }
100     }
101   }
102 }
103
104 ############################################################################
105
106 sub recursively_zap_dirs {
107   local(@zap_dirs) = @_;
108   local($zap_dir) = "";
109   foreach $zap_dir (@zap_dirs) {
110 #hmmm: can we use a perl utility to do the directory recursion?
111     local(@dirnames) = `find \"$zap_dir\" -depth -mindepth 1 -type d`;
112 #print "dirnames are:\n@dirnames\n";
113     &zap_the_dir(@dirnames);
114     &zap_the_dir($zap_dir);
115   }
116 }
117
118 ############################################################################
119