#!/usr/local/bin/perl -w
#
# $Id: qdu,v 1.3 1994/09/09 20:13:09 ejb Exp $
# $Source: /home/ejb/scripts/RCS/qdu,v $
# $Author: ejb $
#
# Use du to get the usage of a directory, but put output in a form
# suitable for viewing in selective display mode in emacs to quickly
# find large files or directories.
#

eval 'exec /usr/local/bin/perl -w -S $0 "${1+$@}"'
    if 0; # trick shell into running this script; must be first code in file
$whoami = ($0 =~ m,([^/]*)$,) ? $1 : $0;

if (scalar(@ARGV) == 0)
{
    $dir = ".";
}
elsif (scalar(@ARGV) > 1)
{
    &usage;
}
elsif ($ARGV[0] eq "--help")
{
    &usage;
}
else
{
    $dir = $ARGV[0];
}

open(DU, "du -a $dir|") || die "Can't run du -a $dir\n";

while (<DU>)
{
    chop;
    ($size, $path) = split;
    $lastcomp = $slashes = $path;
    $lastcomp =~ s,.*/([^/]*)$,$1,;
    $slashes =~ s,[^/],,g;
    $depth = length($slashes) + 1;
    printf("%s %s %s%8d\n",
	   (" " x (($depth - 1) * 2)),
	   $lastcomp, ("-" x (40 - length($lastcomp) - (($depth - 1) * 2))),
	   $size);
}

sub usage {
    print STDERR "Usage: $whoami [dir]\n";
    exit 1;
}
