#!/bin/sh
# summarize-usage --- summarize disk usage based on `find -ls' output
# Author: Noah Friedman <friedman@prep.ai.mit.edu>
# Created: 1994-03-23
# Last modified: 1994-03-23
# Public domain

# Commentary:

# To change the default blocksize, change your `blocksize' environment
# variable.  In the bourne shell this can be temporarily done with the
# syntax ``blocksize=512 summarize-usage ...''

# Code:

blocksize=${blocksize-1024}

echo "blocksize: 1 block = $blocksize bytes"
echo 'User       #Files   Bytes(real)    Blocks     Bytes(blocks)'
echo '-----------------------------------------------------------'

awk 'BEGIN {
       find_inode  = 1;   find_bytes        = 7;
       find_blocks = 2;   find_month        = 8;
       find_mode   = 3;   find_day_of_month = 9;
       find_nlinks = 4;   find_time_or_year = 10;
       find_user   = 5;   find_filename     = 11;
       find_group  = 6;
     }
     {
        nfiles[$find_user] = nfiles[$find_user] + 1
        blocks[$find_user] = blocks[$find_user] + $2 
        bytes[$find_user]  = bytes[$find_user]  + $7

        total_nfiles = total_nfiles + 1
        total_blocks = total_blocks + $2
        total_bytes = total_bytes   + $7
      } 
      END { 
        fmtstr = "%-10s %-8d %-14ld %-10ld %-20ld\n"
        for (user in blocks)
          {
            printf(fmtstr, user, nfiles[user], bytes[user], blocks[user], \
                   blocks[user] * blocksize);
          }
        printf(fmtstr, "TOTAL", total_nfiles, total_bytes, total_blocks, \
               total_blocks * blocksize);

      }' blocksize=${blocksize} ${1+"$@"} \
| sort -nrk3

# summarize-usage ends here
