#! /usr/local/bin/perl

push(@INC, '/usr/local/lib');
do 'mailqutil.pl';

$now = time;
$secondsperhour = 3600;

format q_top =
Queue locked age<1    <2    <4    <8   <16   <32   <64  >=64 total (files)
--------------------------------------------------------------------------
.
format q_line =
@<<<<< @>>>> @>>>> @>>>> @>>>> @>>>> @>>>> @>>>> @>>>> @>>>> @>>>> @>>>>>>
$queue,$locked,$b[0],$b[1],$b[2],$b[3],$b[4],$b[5],$b[6],$b[7],$total,$files
.

$^ = "q_top";
$~ = "q_line";
$dirs = 0;
foreach $qdir (</usr/spool/{mqueue,mq[0-9],mq-*}>) {
	chdir $qdir || die "can't chdir to $qdir: $!\n";
	opendir(qdir, ".") || die "can't opendir $qdir: $!\n";
	$dirs++;
	@_ = split(/\//, $qdir);
	$queue = $_[$#_];
	$locked = 0;
	@b = ();
	$total = 0;
	$files = 0;
	while ($_ = readdir(qdir)) {
		next if ($_ eq "." || $_ eq "..");
		$files++;
		if (/^lf/) {
			$locked++;
		} elsif (/^qf/) {
			$total++;
			s/q/d/;
			$age = ($now - &mtime($_)) / $secondsperhour;
			$b[&bucket($age)]++;
		}
	}
	close(qdir);
	write() if ($files > 0);
	$t_locked += $locked;
	for ($i = 0;  $i <= $#b;  $i++) {
		$t_b[$i] += $b[$i];
	}
	$t_total += $total;
}

if ($dirs > 1) {
	$queue = ""; $locked = ""; @b = (); $total = ""; $files = ""; write();
	$queue = "TOTAL";
	$locked = $t_locked;
	@b = @t_b;
	$total = $t_total;
	write();
}

exit 1;
