first check-in of feisty meow codebase. many things broken still due to recent
[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 require "importenv.pl";
23
24 local($chewed_line) = "";
25 local(@arg_list);
26 local($print_list) = "";
27
28 # if there were no parameters, we make the default listing for the current
29 # directory.
30 if ($#ARGV < 0) {
31   $print_list = "Current Directory";
32   @arg_list = ( "." );
33 } else {
34   local(@munged_list) = &patch_name_for_pc(&remove_trailing_slashes(@ARGV));
35   $print_list = "@munged_list";
36   @arg_list = &glob_list(@ARGV);
37 }
38
39 foreach $dir (@arg_list) {
40   $chewed_line = $chewed_line . " \"$dir\"";
41 }
42
43 if ("$chewed_line" eq "") {
44   print "No files matched that path specification.\n";
45   exit 0;
46 }
47
48 # show the header, now that we know there's something to print.
49 print "[" . $print_list . "]\n\n";
50
51 ##print "chewed_line is: $chewed_line\n";
52
53 local($temp_file)=`mktemp "$TMP/zz_frdsumdir.XXXXXX"`;
54 chop($temp_file);
55
56 system("ls -lF $chewed_line >$temp_file");
57
58 ##print "file is: $temp_file\n";
59
60 # drop the main payload, the list of directory info.
61 system("cat $temp_file");
62
63 local($lengths) = 0;
64
65 # open the file and process the lines to get file lengths.
66 open(DIRLIST, "<$temp_file");
67 foreach $file_line (<DIRLIST>) {
68   (local $munged = $file_line) =~ s/^[^ ]* *[^ ]* *[^ ]* *[^ ]* *([0-9]+).*$/\1/;
69 #  print "munge=$munged\n";
70   $lengths += $munged;
71 }
72 close(DIRLIST);
73 unlink($temp_file);  # clean up.
74
75 #print "lens are: $lengths\n";
76
77 local($total)=$lengths;
78 local($kbytes)=int($total / 102.4) / 10;
79 local($mbytes)=int($kbytes / 102.4) / 10;
80
81 print "\n";
82 print "These files occupy $total bytes ($kbytes KB / $mbytes MB).\n";
83 print "Overall Drive Usage (megs):\n";
84
85 system("df -m $chewed_line >$temp_file");
86
87 # now eat the file again, but this time to get drive space info.
88 open(DIRLIST, "<$temp_file");
89 local($space_info) = "";
90 local($did_title) = 0;  # true if we have printed the title by now.
91 foreach $file_line (<DIRLIST>) {
92   ($space_info = $file_line) =~ s/[^ ]* *([^ ]*) *([^ ]*) *([^ ]*) *([^ ]*) *.*$/\1     \2      \3      \4/;
93   if (!$did_title) {
94     # if the title hasn't been printed yet, we take some of the info out of
95     # the line and use it for the right sense of the last column.
96     print "Total        Used    Free    ";
97     $space_info =~ s/[^ ]*      *[^     ]*      *[^     ]*      *([^    ]*)/\1/;
98     print "$space_info";
99     $did_title = 1;
100   } else {
101   }
102 }
103 close(DIRLIST);
104 unlink($temp_file);  # clean up.
105
106 print "$space_info\n";
107
108 exit 0;
109