#!/usr/um/bin/perl

# Clark version 2.0b1 March 7, 1996
# Copyright (c) 1995, 1996 David S. Carter. All rights reserved.
# Permission is granted to freely use and modify for personal use, but not to distribute.
# Information on Clark, licensing agreement, and the latest version, 
# can be obtained at http://www.sils.umich.edu/~superman/AP/

# IMPORTANT NOTE: you must use the Unix sort command on the input file
#   before running.

# Example command for executing:
# sils% zcat Nov.95.acc.gz | sort | clark2.pl > Nov.95.log

# $gap is gap size in minutes - modify as needed

$gap = 30;
$sgap = $gap * 60;

# setting initial values

$ctr = 0;
$gif = 0;
$chost = "none";

# get things rolling

while (<>) {

	($host, $r, $u, $tstr, $gmt, $request) = split (/ /, $_, 6);
	($dt, $time) = split (/:/, $tstr, 2);
	$date = substr ($dt, 1);
	($day, $mo, $yr) = split (/\//, $date);
	($eday, $emo, $eyr) = split (/\//, $edate);
	
	if ($host ne $chost || &tcheck(&tcalc($time), &tcalc($etime), $day, $eday) ) {
		&out ($ctr, $chost, $rfc, $user, $sdate, $edate, $stime, $etime, $gif, @events);
		undef @events;
		$ctr++;
		$chost = $host;
		$rfc = $r;
		$user = $u;
		$sdate = $date;
		$edate = $date;
		$stime = $time;
		$etime = $time;
		$gif = 0;
		if ($request =~ /\.gif|\.GIF/) {
			$gif = 1;
			}
		push (@events, $request);
		}
		
	else {
		if ($request =~ /\.gif|\.GIF/) {
			$gif = 1;
			}
		$etime = $time;
		$edate = $date;
		push (@events, $request);
		}
		
	}
	
	
&out ($ctr, $chost, $rfc, $user, $sdate, $edate, $stime, $etime, $gif, @events);




sub tcalc {
	local($hr, $min, $sec) = split (/:/, $_[0]);
	return ($sec + ($min * 60) + ($hr * 3600));
	}



sub tcheck {

	local($time, $etime, $day, $eday) = @_;
	
	$calc = $time - $etime + (($day - $eday) * 86400);
	
	if ($calc > $sgap) {
		return(1);
		}
	else {
		return(0);
		}
		
	}
	
	

sub out {

	local($ctr, $chost, $rfc, $user, $sdate, $edate, $stime, $etime, $gif, @events) = @_;

	if ($ctr) {
	
		if ($sdate eq $edate) {
			$pl = 0;
			}
		else {
			$pl = 86400;
			}
	
		$len = &tcalc($etime) - &tcalc($stime) + $pl;
		$nev = $#events + 1;
	
		print "*$ctr\n$chost\n$rfc\n$user\n$nev\n$sdate\n$edate\n$stime\n$etime\n$len\n$gif\n";
		foreach $line (@events) {
			print "$line";
			}
		print "\n";
	
		}
	}	
	
