#!/usr/bin/perl
###############################################################################
#
# File:         www.newlines.pl
# RCS:          $Header: $
# Description:  find new lines in www logiles since date
# Author:       Christian Limpach <chris@nice.ch>
# Created:      Wed Apr  5 23:34:14 1995
# Modified:     Wed May  6 02:42:44 1998 (Christian Limpach) chris@nice.ch
# Language:     Perl
# Package:      N/A
# Status:       Experimental (Do Not Distribute)
#
# (C) Copyright 1995, Christian Limpach, all rights reserved.
# This is free software, and you are welcome to redistribute it
# under certain conditions; see ftp://nice.ethz.ch/users/chris/TERMS
# for details. This software comes with ABSOLUTELY NO WARRANTY.
#
###############################################################################

require 'timelocal.pl';

sub date2utc {
    my($mday,$mon,$hours,$min,$sec) =
	$_[0] =~ m,^.* .* .* [[](\d\d)/([jJfFmMaAsSoOnNdD][aAeEpPuUcCoO][nNbBrRyYlLgGpPtTvVcC])/\d\d\d\d:(\d\d):(\d\d):(\d\d),;
    my(%monthnames) = ('JAN',0,'FEB',1,'MAR',2,'APR',3,'MAY',4,'JUN',5,
		       'JUL',6,'AUG',7,'SEP',8,'OCT',9,'NOV',10,'DEC',11);
    my($time);
    if($mon) {
	$year = (localtime(time))[5];
	$mon =~ tr/a-z/A-Z/;
	$mon=$monthnames{$mon};
	$time=timelocal($sec,$min,$hours,$mday,$mon,$year);
	$time=timelocal($sec,$min,$hours,$mday,$mon,--$year) if ($time > time);
	$time;
    } else {
	-1;
    }
}

sub www_newlines {
    my($file, $date) = @_;
    my($last)=0;
    $date = date2utc($date) if ($date == 0);
    open(f, "<$file");
    while (<f>) {
	$last = date2utc($_);
	print if ($date < $last);
    }
    close f;
    $last?$last:$date;
}

if ($0 =~ /www.newlines.pl$/) {
    die "usage: $0 file since [time-stamp-file]\n"
	unless ($#ARGV==1 || $#ARGV==2);
    ($file, $since) = @ARGV;
    $since = hex($since) if $since =~ /^0x[0-9a-fA-F]*$/;
    if ($#ARGV==2) {
	open(ts, ">$ARGV[2]");
	printf ts "0x%x\n", www_newlines($file, $since);
	close ts;
    } else {
	printf stderr "0x%x\n", www_newlines($file, $since);
    }
}

1;
