#!/usr/bin/perl

# files.pl - Generate file statistics from GN log
# Created 28/10/94 by Andrew Pam <avatar@aus.xanadu.com>
# Copyright (c) 1994 Serious Cybernetics
# http://www.sericyb.com.au/sc/
# Last modified 07/02/95 by Andrew Pam

# path to the server root
$root = "/web";

# estimated size of HTML HEAD section
$head = 128;

# estimated size of CGI POST output
$post = 256;

# estimated size of CGI redirection output
$redir = 32;

# estimated fraction of cache entries found by a search
$divisor = 10;

##### END OF CONFIGURATION SECTION #####

# Gather data
while (<>)
{
	# Keep count of the filenames sent
	if (($name, $type) = /\((.*)\): Sent (.*):/)
	{
		$type = "header" if $type =~ /header$/;
		$type = "not modified" if $type =~ /not modified$/;
		$count{$name, $type}++;
	}
}

# Get file sizes
foreach $file (keys %count)
{
	($name, $type) = split($;, $file);
	if ($type eq "not modified")
	{	$size{$file} = 16;	}
	elsif ($type eq "header")
	{	$size{$file} = $head;	}
	elsif ($type eq "CGI output")
	{	$size{$file} = $post;	}
	elsif ($type eq "CGI redirection")
	{	$size{$file} = $redir;	}
	elsif ($type eq "grep search")
	{	$size{$file} = (-s "$root$name/.cache") / $divisor;	}
	elsif ($type eq "range search")
	{	$size{$file} = (-s "$root$name..cache") / $divisor;	}
	elsif ($type eq "range")
	{	$size{$file} = (-s "$root$name") / $divisor;	}
	elsif ($type eq "cache")
	{
		if (-d "$root$name")
		{	$size{$file} = -s "$root$name/.cache";	}
		else
		{	$size{$file} = -s "$root$name..cache";	}
	}
	else
	{	$size{$file} = -s "$root$name";	}
}

# Define output format
format STDOUT_TOP =
Accesses   Bytes       Type     Description
-------- --------- ------------ ----------------------------------------
.
format STDOUT =
@####### @######## @>>>>>>>>>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$count{$file}, $count{$file} * $size{$file}, $type, $name
.

# Print results
foreach $file (sort {$count{$b} <=> $count{$a}} keys %count)
{
	($name, $type) = split($;, $file);
	write;
	$total += $count{$file};
	$size += $count{$file} * $size{$file};
}
print "======== =========\n";
printf "%8d %9d\n", $total, $size;

