#! /usr/bin/perl -w

use File::Find ();
use File::stat;


# for the convenience of &wanted calls, including -eval statements:
use vars qw/*name *dir *prune/;
*name   = *File::Find::name;
*dir    = *File::Find::dir;
*prune  = *File::Find::prune;

$main::totalusage=0;
sub wanted;

my (%uid, %user);
while (my ($name, $pw, $uid) = getpwent) {
    $uid{$name} = $uid{$uid} = $uid;
}

$searchuid=$uid{$ARGV[0]};

$|=1;
if (! defined($ARGV[0])){
   print "Usage: quotals <username>";
   exit 1;
}

# Traverse desired filesystems
local $^W = 0; 
File::Find::find({wanted => \&wanted, no_chdir => 1}, '/');
print "\n";
print "-----------------------------------------------\n";
printf("%-70s %6.0fM\n", "Total Usage:",($main::totalusage/(1024*1024)));

system("quota -vs $ARGV[0]\n");
print "If the quota system reports something very different to quotals, then quotas are corrupt.\n";
print "try running the following: quotaoff / ; quotacheck -m / ; quotaon /\n";
exit 0;

sub wanted {
    my ($dev,$ino,$mode,$nlink,$uid,$gid);
    return if ($name =~ /^\/proc/); #Skip everything in /proc
    return if (-l $name);           #Skip soft links
    return unless (-f $name);
    my $sb = stat($name);
    return unless (defined($sb));
     
    return unless  ($sb->uid eq $searchuid);
    printf("%-70s %6.0fK\n",$name,$sb->size/1024);
    $main::totalusage = $main::totalusage + $sb->size;
}


