first check-in of feisty meow codebase. many things broken still due to recent
[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 require "importenv.pl";
25
26 ############################################################################
27
28 sub remove_whackables {
29   local($from_dir) = @_;
30
31   # open the directory and grab all the files out.
32   opendir CHECK, $from_dir;
33   local(@files) = readdir(CHECK);
34   closedir CHECK;
35
36   # iterate over the potentially whackable files.
37   local($fname) = "";
38   foreach $fname (@files) {
39 #print "filename is $fname\n";
40     # check if this one matches one of our patterns.
41     if (! &important_filename($fname)) {
42       # it's a junk file; whack it.
43       $fname = $from_dir . '/' . $fname;
44 #print "whacking: $fname.\n";
45       unlink $fname;
46       if (-d "$fname") {
47         print "recursively deleting directory: $fname\n";
48         &recursive_delete($fname);
49       }
50     }
51   }
52 }
53
54 ############################################################################
55
56 sub zap_the_dir {
57   local(@zap_list) = @_;
58   local($to_zap) = "";
59   foreach $to_zap (@zap_list) {
60     chomp $to_zap;
61
62 #print "to_zap is $to_zap\n";
63     if (! -d $to_zap) {
64       print "$to_zap is not a directory.\n";
65       next;
66     }
67
68     if ($to_zap =~ /^.*\.svn.*$/) {
69 #print "hey found a .svn dir!  skipping.\n";
70       next;
71     }
72
73     &remove_whackables($to_zap);
74     opendir WHACK, $to_zap;
75     @files_left = readdir(WHACK);
76     closedir WHACK;
77   
78     local($name) = "";
79     local($seen_anything) = "";
80     foreach $name (@files_left) {
81       # check if directory has nothing but the two directory entries in it.
82       if ( ($name ne ".") && ($name ne "..") ) {
83         if ($to_zap =~ /^.*\.svn.*$/) {
84           print "not empty: \"$to_zap/$name\"\n";
85         }
86         $seen_anything = "true";
87         break; 
88       }
89     }
90
91     if (length($seen_anything)) {
92       print "not empty: \"$to_zap\"\n";
93     } else {
94       # this should now work, if the directory is empty.
95       if (!rmdir $to_zap) {
96         print "still in use: \"$to_zap\"\n";
97         next;
98       }
99     }
100   }
101 }
102
103 ############################################################################
104
105 sub recursively_zap_dirs {
106   local(@zap_dirs) = @_;
107   local($zap_dir) = "";
108   foreach $zap_dir (@zap_dirs) {
109 #hmmm: can we use a perl utility to do the directory recursion?
110     local(@dirnames) = `find \"$zap_dir\" -depth -mindepth 1 -type d`;
111 #print "dirnames are:\n@dirnames\n";
112     &zap_the_dir(@dirnames);
113     &zap_the_dir($zap_dir);
114   }
115 }
116
117 ############################################################################
118