#!/usr/athena/bin/perl

#	For stats of usage:
#	percfree filename clustername|totals

@clusters = ('BARKER5', 'BARKER6', 'M1', 'M2', 'M4-035', 'M4-167', 'M11-113', 'M11-116', 'M14', 'M16', 'M37-312', 'M37-318', 'M37-332', 'M66', 'E51', 'W20');

if($#ARGV<0){print"percfree filename clustername\n"; exit;}
$n = 1;
foreach $c (@clusters) {
    $cns{$c}=$n;
    $n++;
}

open (LFF, "$ARGV[0]");
$minute=0;
while (<LFF>) {
    foreach $cluster(@clusters) { 
	$cnum=$cns{$cluster};
	if(/\d\d:\d\d:\d\d/) {	# 
	    split;
	    @time = split(/:/, $_[3]);
	    $month = $_[1];
	    if($_[2] ne $day){
		$day = $_[2];
		print(STDERR "\n$month $day: ");	
	    }
	    $hour = $time[0];
	    $x = int($time[1]);
	    if($x >= 50) {
		$minute = "00";
		$hour++; }
	    if($x <= 50) { 
		$minute = 30; }
	}			# 
	
	if(/$cluster/) {
	    split;
	    if($_[$#_]!=0){
		$perc = 100*($_[$#_-1]/$_[$#_]);
		$tindex = 2*$hour; # 
		if ($minute == 30) { $tindex++; }
		$percents[$tindex, $cnum]=($dats[$tindex, $cnum]*$percents[$tindex, $cnum])+$perc;
		$dats[$tindex, $cnum]++; # 
		$percents[$tindex, $cnum] = $percents[$tindex, $cnum]/($dats[$tindex, $cnum]);
		$iperc = int($percents[$tindex, $cnum]); # 
#		print("At $hour:$minute average free-- $iperc    ($dats[$tindex])\n");
#		$y = int($tindex/2);
#		print("$y: $iperc\n");
		print(STDERR "."); # 
	    }			# 
	}
    }				# 
}
print(STDERR "\n");
foreach $name (@clusters) {
    open(FILE, ">/usr/tmp/clust.$name");
    print("Writing file:  /usr/tmp/clust.$name  ");
    $clust = $cns{$name};
    foreach $i (0..49) {
	$y = int($i/2);
	$iperc = int($percents[$i, $clust]);
	print(FILE "$y: $iperc\n");
    }
    close(FILE);
    print("...done.\n");
}				# 
				# 




