#!/usr/local/bin/perl -Tw
#
#  $Id: loglook,v 1.1 2000/09/26 19:10:59 dgregor Exp $
#
# Copyright (c) 1998 Daniel J. Gregor, Jr., All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
# 	This product includes software developed by Daniel J. Gregor, Jr.
# 4. The name of Daniel J. Gregor, Jr. may not be used to endorse or promote
#    products derived from this software without specific prior written
#    permission.
# 
# THIS SOFTWARE IS PROVIDED BY DANIEL J. GREGOR, JR. ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL DANIEL J. GREGOR, JR. BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

#$DEBUG = 1;

$backuplinesize = 1024;


die "Usage: loglook <datematch> <log file>\n" unless (@ARGV == 2);

$datematch = shift(@ARGV);
$logfile = shift(@ARGV);

open(LOGFILE, $logfile) || die "cannot open $logfile: $!\n";

$filesize = (stat(LOGFILE))[7];


$depth = 1;
$position = 0; # start at the beginning of the file

sub btreesearch {
	local($begin) = shift(@_);
	local($end) = shift(@_);
	local($datematch) = shift(@_);
	local($inversematch) = shift(@_);
	local($compare) = -1;
	local($depth) = 1;
	local($position) = $begin;

	seek(LOGFILE, $position - 1, 0) || die "could not seek: $!\n";
	while(defined($line = <LOGFILE>)) {
		print STDERR "1: " . substr($line, 0, length($datematch)) . "\n"
			if $DEBUG;
		print STDERR "2: $datematch\n" if $DEBUG;
	
		$compare = (substr($line, 0, length($datematch)) cmp
			$datematch);
	
		if ($compare == 0) {
			return int($position);
		}
	
		print STDERR "compare: $compare\n" if $DEBUG;
	
		if ($inversematch) {
			if ($compare == -1) {
				$compare = 1;
			} else {
				$compare = -1;
			}
		}

		if ($compare == -1) {
			$position += ($end - $begin) / (1<<$depth);
		} else {
			$position -= ($end - $begin) / (1<<$depth);
		}
		$depth++;
	
		print STDERR "position: $position\n" if $DEBUG;
		seek(LOGFILE, $position - 1, 0) || die "could not seek: $!\n";
	
		# chances are very good that we are not at the beginning of a
		# line, so read a line and discard it.  the next read line
		# will be at the beginning of a line.
		<LOGFILE>;
	}

	return;
}

$lastret = btreesearch(1, $filesize, $datematch, 0);

print STDERR "\n\nret: $ret\n\n\n" if $DEBUG;

while (defined($ret = btreesearch(1, $lastret + 1, $datematch, 0)) &&
				$ret != $lastret) {
	print STDERR "\n\nret: $ret\n\n\n" if $DEBUG;
	$lastret = $ret;
}

if ($lastret - ($backuplinesize * 3) > 0) {
	seek(LOGFILE,  -($backuplinesize * 3), 1) || 
		die "could not seek: $!\n";
} else {
	seek(LOGFILE, 0, 0) || die "could not seek: $!\n";
}


while (defined($_ = <LOGFILE>) && !m/^$datematch/) {
	print STDERR "Ignored: $_" if $DEBUG;
}
print $_;

while (defined($_ = <LOGFILE>) && m/^$datematch/) {
	print $_;
}
