taking out colons also, as painful as that sounds.
[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 (-d "$fname") {
46         print "recursively deleting directory: $fname\n";
47         &recursive_delete($fname);
48       }
49     }
50   }
51 }
52
53 ############################################################################
54
55 sub zap_the_dir {
56   local(@zap_list) = @_;
57   local($to_zap) = "";
58   foreach $to_zap (@zap_list) {
59     chomp $to_zap;
60
61 #print "to_zap is $to_zap\n";
62     if (! -d $to_zap) {
63       print "$to_zap is not a directory.\n";
64       next;
65     }
66
67     if ($to_zap =~ /^.*\.svn.*$/) {
68 #print "hey found a .svn dir!  skipping.\n";
69       next;
70     }
71
72     &remove_whackables($to_zap);
73     opendir WHACK, $to_zap;
74     @files_left = readdir(WHACK);
75     closedir WHACK;
76   
77     local($name) = "";
78     local($seen_anything) = "";
79     foreach $name (@files_left) {
80       # check if directory has nothing but the two directory entries in it.
81       if ( ($name ne ".") && ($name ne "..") ) {
82         if ($to_zap =~ /^.*\.svn.*$/) {
83           print "not empty: \"$to_zap/$name\"\n";
84         }
85         $seen_anything = "true";
86         break; 
87       }
88     }
89
90     if (length($seen_anything)) {
91       print "not empty: \"$to_zap\"\n";
92     } else {
93       # this should now work, if the directory is empty.
94       if (!rmdir $to_zap) {
95         print "still in use: \"$to_zap\"\n";
96         next;
97       }
98     }
99   }
100 }
101
102 ############################################################################
103
104 sub recursively_zap_dirs {
105   local(@zap_dirs) = @_;
106   local($zap_dir) = "";
107   foreach $zap_dir (@zap_dirs) {
108 #hmmm: can we use a perl utility to do the directory recursion?
109     local(@dirnames) = `find \"$zap_dir\" -depth -mindepth 1 -type d`;
110 #print "dirnames are:\n@dirnames\n";
111     &zap_the_dir(@dirnames);
112     &zap_the_dir($zap_dir);
113   }
114 }
115
116 ############################################################################
117