#!/usr/bin/perl -w
#
# Do bulk login accounting on the wtmp backups.
# Prints in descending order, similar to what ac(8) produces.
#
#

my %totals;

# Glob the filenames
@wtmps = </var/log/wtmp*>;

# bang through the list
for $wtmp (@wtmps) {
	open(AC,"ac -p -w $wtmp |") or warn "can't open pipe to ac\n";

	# grab the output and add it to the hash
	while(<AC>) {
		/\s+(.*?)\s+(.*)$/;
		$totals{$1} += $2;
	}
	
	close(AC);
}

# make write look purty
format STDOUT =
	@<<<<<<<<<<<<<<< @>>>>>>>
	$user,           $total
.

# print out the data, sorted in decending order by value
for $user (sort {$totals{$b} <=> $totals{$a}} (keys %totals)) {
	
	# make the number look purty
	$total = sprintf "%.2f", $totals{$user};
	write;

}

# done
exit;

