#! /bin/sh
# disksum --- summarize disk usage for all users on the system
# Author: Noah Friedman <friedman@prep.ai.mit.edu>
# Created: 1995-07-26
# Public domain

# $Id: disksum,v 1.2 1995/10/02 19:46:05 friedman Exp $

# Commentary:

# Written for the ACTlab at UT Austin.

# Code:

PATH=/usr/ucb:$PATH

tmpfile=/tmp/dspasswd$$
output=/tmp/dsreport$$

trap 'exitstat=$?
      rm -f "$tmpfile" "$output"
      trap '' 0 1 2 3 15
      exit $exitstat
     ' 0 1 2 3 15

if grep '^+:' /etc/passwd > /dev/null 2>&1; then
  ypcat passwd > $tmpfile
  exec 0< $tmpfile
else
  exec 0< /etc/passwd
fi

OIFS="$IFS"

echo "
Summary of home directory disk usage, in kilobytes.

Please be aware of the fact that this program cannot determine the size of
subdirectories to which you do not have access, and therefore these numbers
may be innacurate (but you will know that at *least* that much space is in
use, if not more).  For a closer estimate, run this program as root.

"

echo -n "Searching..."

while read pwent; do
  IFS=:
  set fnord $pwent
  shift
  IFS="$OIFS"

  user=$1
  home=$6

  # Set a single arg, then shift it away.
  # We do this instead of "shift $#" because some versions of sh do not
  # allow a numeric argument to shift.
  set fnord
  shift

  # If the home directory doesn't exist (or is inaccessible), skip it.
  if test -d "$home"; then
    :
  else
    continue
  fi

  # For the ACTLAB: Don't summarize usage of accounts unless they have home
  # directories in /export/home.
  case "$home" in
    /export/home/* ) : ;;
    * ) continue ;;
  esac

  echo -n "."

  usage=`{ cd $home && du -sk; } 2> /dev/null`
  set fnord $usage
  case $# in
    1 ) set fnord 'unknown' ;;
  esac

  echo "$2 $user $home" >> "$output"
  set fnord
  shift
done

echo "done

"

sort -nr "$output" \
| awk 'BEGIN {
         fmt="%-15s%-15s%s\n";
         printf(fmt, "USER", "KBYTES", "HOME");
         printf(fmt, "----", "------", "----");
       }

       {
         printf(fmt, $2, $1, $3);
       }'

echo ""

# disksum ends here
