skipping some common flags i add accidentally, since summing dir has its own format...
[feisty_meow.git] / scripts / files / summing_dir.pl
1 #!/usr/bin/perl
2
3 ###############################################################################
4 #                                                                             #
5 #  Name   : summing_dir                                                       #
6 #  Author : Chris Koeritz                                                     #
7 #  Rights : Copyright (C) 2000-$now by Author                                 #
8 #                                                                             #
9 #  Purpose:                                                                   #
10 #                                                                             #
11 #    Provides a somewhat stylized directory lister.                           #
12 #                                                                             #
13 ###############################################################################
14 #  This program is free software; you can redistribute it and/or modify it    #
15 #  under the terms of the GNU General Public License as published by the Free #
16 #  Software Foundation; either version 2 of the License or (at your option)   #
17 #  any later version.  See: "http://www.gruntose.com/Info/GNU/GPL.html" for a #
18 #  version of the License.  Please send any updates to "fred@gruntose.com".   #
19 ###############################################################################
20
21 require "filename_helper.pl";
22
23 use Env qw($TMP $color_add $TERM);
24
25 local($chewed_line) = "";
26 local(@arg_list);
27 local($print_list) = "";
28
29 # if there were no parameters, we make the default listing for the current
30 # directory.
31 if ($#ARGV < 0) {
32   $print_list = "Current Directory";
33   @arg_list = ( "." );
34 } else {
35   local(@munged_list) = &patch_name_for_pc(&remove_trailing_slashes(@ARGV));
36   $print_list = "@munged_list";
37   @arg_list = &glob_list(@ARGV);
38 }
39
40 foreach $dir (@arg_list) {
41   if ($dir == "-al") { next; }  # skip ls directives.
42   if ($dir == "-l") { next; }  # skip ls directives.
43   $chewed_line = $chewed_line . " \"$dir\"";
44 }
45
46 if ("$chewed_line" eq "") {
47 #  print "No files matched that path specification.\n";
48 #  exit 0;
49   $chewed_line = "."
50 }
51
52 # show the header, now that we know there's something to print.
53 print "[" . $print_list . "]\n\n";
54
55 ##print "chewed_line is: $chewed_line\n";
56
57 local($temp_file)=`mktemp "$TMP/zz_frdsumdir.XXXXXX"`;
58 chop($temp_file);
59
60 # drop the main payload, the list of directory info, but also save that
61 # info to a file for analysis.
62 system("ls -HhlF $color_add $chewed_line");
63 system("ls -HhlF $color_add $chewed_line > $temp_file");
64   # the color_add variable, if defined, will have flags for setting the
65   # directory listing color scheme.
66
67 ##print "file is: $temp_file\n";
68
69 local($lengths) = 0;
70
71 # open the file and process the lines to get file lengths.
72 open(DIRLIST, "<$temp_file");
73 # we only want to match ls -al style output lines, and only want to keep the size.
74 $pattern="^[^ ]+ +[^ ]+ +[^ ]+ +[^ ]+ +([0-9.]+[KMG]?).*\$";
75 foreach $file_line (<DIRLIST>) {
76   if ($file_line =~ /$pattern/) {
77     (local $munged = $file_line) =~ s/$pattern/\1/;
78     #print "munge=$munged\n";
79     if ($munged =~ /K$/) {
80       chop $munged;
81       $munged *= 1024.0;
82       #print "K munged is now $munged\n";
83     }
84     if ($munged =~ /M$/) {
85       chop $munged;
86       $munged *= 1024.0 * 1024.0;
87       #print "M munged is now $munged\n";
88     }
89     if ($munged =~ /G$/) {
90       chop $munged;
91       $munged *= 1024.0 * 1024.0 * 1024.0;
92       #print "G munged is now $munged\n";
93     }
94     $lengths += $munged;
95   }
96 }
97 close(DIRLIST);
98 unlink($temp_file);  # clean up.
99
100 #print "lens are: $lengths\n";
101
102 local($total)=int($lengths);
103 local($kbytes)=int($total / 102.4) / 10;
104 local($mbytes)=int($kbytes / 102.4) / 10;
105
106 print "\n";
107 print "These files occupy $total bytes ($kbytes KB / $mbytes MB).\n";
108 print "Overall Drive Usage (megs):\n";
109
110 system("df -m $chewed_line >$temp_file");
111
112 # now eat the file again, but this time to get drive space info.
113 open(DIRLIST, "<$temp_file");
114 local($space_info) = "";
115 local($did_title) = 0;  # true if we have printed the title by now.
116 foreach $file_line (<DIRLIST>) {
117   ($space_info = $file_line) =~ s/[^ ]* *([^ ]*) *([^ ]*) *([^ ]*) *([^ ]*) *.*$/\1     \2      \3      \4/;
118   if (!$did_title) {
119     # if the title hasn't been printed yet, we take some of the info out of
120     # the line and use it for the right sense of the last column.
121     print "Total        Used    Free    ";
122     $space_info =~ s/[^ ]*      *[^     ]*      *[^     ]*      *([^    ]*)/\1/;
123     print "$space_info";
124     $did_title = 1;
125   } else {
126   }
127 }
128 close(DIRLIST);
129 unlink($temp_file);  # clean up.
130
131 print "$space_info\n";
132
133 exit 0;
134