#!/usr/local/bin/perl

# na_qformat - format quota information in a nice way
# Copyright (C) 1998  Daniel Quinlan
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

$prog = $0;
$prog =~ s@.*/@@;

require "getopts.pl";
&Getopts("hu");

# Quota Fields, for major versions 4 and 5
sub QF4_Type ()		{0;}
sub QF4_ID ()		{1;}
sub QF4_KBytesUsed ()	{2;}
sub QF4_KBytesLimit ()	{3;}
sub QF4_FilesUsed ()	{4;}
sub QF4_FilesLimit ()	{5;}
sub QF4_QuotaSpec ()	{6;}
#
sub QF5_Type ()		{0;}
sub QF5_ID ()		{1;}
sub QF5_Volume ()	{2;}
sub QF5_Tree ()		{3;}
sub QF5_KBytesUsed ()	{4;}
sub QF5_KBytesLimit ()	{5;}
sub QF5_FilesUsed ()	{6;}
sub QF5_FilesLimit ()	{7;}
sub QF5_QuotaSpec ()	{8;}
#
sub QF_Type ()		{($QF == 4) ? &QF4_Type        : &QF5_Type; }
sub QF_ID ()		{($QF == 4) ? &QF4_ID          : &QF5_ID; }
sub QF_KBytesUsed ()	{($QF == 4) ? &QF4_KBytesUsed  : &QF5_KBytesUsed; }
sub QF_KBytesLimit ()	{($QF == 4) ? &QF4_KBytesLimit : &QF5_KBytesLimit; }
sub QF_FilesUsed ()	{($QF == 4) ? &QF4_FilesUsed   : &QF5_FilesUsed; }
sub QF_FilesLimit ()	{($QF == 4) ? &QF4_FilesLimit  : &QF5_FilesLimit; }
sub QF_QuotaSpec ()	{($QF == 4) ? &QF4_QuotaSpec   : &QF5_QuotaSpec; }

sub sort_quotas {
    if ($a->[&QF_Type] ne $b->[&QF_Type]) {
	return ($a->[&QF_Type] cmp $b->[&QF_Type]);
    }
    if ($QF == 5 && ($a->[&QF5_Tree] ne $b->[&QF5_Tree])) {
	return ($a->[&QF5_Tree] cmp $b->[&QF5_Tree]);
    }
    if ($opt_u) {
	return ($a->[&QF_ID] cmp $b->[&QF_ID]);
    }
    return ($b->[&QF_KBytesUsed] <=> $a->[&QF_KBytesUsed]);
}

if ($opt_h) {
    print <<EOF;
usage: $prog [options]
 -h                print this help
 -u                sort by user (default is to sort by disk usage)
EOF
    exit 0;
}

$header = 1;

$QF = 4;
$header_done = 0;
$header_lines = '';
while (<STDIN>) {
    if (! $header_done) {
	s/K-Bytes/M-Bytes/;
	if (/Volume/) {
	    $QF = 5;
	}
	$header_lines .= $_;
	if (/^-/) {
	    $header_done = 1;
	}
	next;
    }
    @tmp = split;
    # work-around NetApp quota format brain damage
    if (!defined($tmp[&QF_QuotaSpec])) {
	$tmp[&QF_QuotaSpec] = $tmp[&QF_FilesLimit];
	$tmp[&QF_FilesLimit] = '-';
    }
    push @lines, [ @tmp ];
}
close(STDIN);

@lines = sort sort_quotas @lines;

print $header_lines;

for ($i = 0; $i <= $#lines; $i++) {
    if ($lines[$i][&QF_KBytesUsed] < 1024) {
	$lines[$i][&QF_KBytesUsed] = int($lines[$i][&QF_KBytesUsed] / 102.4 + 0.5) / 10;
    }
    else {
	$lines[$i][&QF_KBytesUsed] = int($lines[$i][&QF_KBytesUsed] / 1024 + 0.5);
    }
    $lines[$i][&QF_KBytesLimit] = int($lines[$i][&QF_KBytesLimit] / 1024 + 0.5);

    if ($QF == 4) {
	printf "%-16s %8s  %8s %8s  %7s %7s  %-22s\n", @{ $lines[$i] }[0..6];
    }
    if ($QF == 5) {
	printf "%-5s %-8s  %-4s %12.12s  %7s %7s %8s %8s %-22s\n", @{ $lines[$i] }[0..8];
    }
}
