#!/usr/bin/perl
#
# This scripts reads (on STDIN) an HTTPD log file in common log file format,
# sorts it by date and time, and writes the results to stdout.
#
# Of course, HTTPD normally writes its files in sorted order anyway.
# I needed this because of a screwy HTTPD configuration that left my
# log entries split across two files.  This script let me cat(1) the
# files together and produce a sorted result that could be fed to
# getstats(l).
#
# Brent Baccala 05 Jul 1996

$month{"jan"} =  1;
$month{"feb"} =  2;
$month{"mar"} =  3;
$month{"apr"} =  4;
$month{"may"} =  5;
$month{"jun"} =  6;
$month{"jul"} =  7;
$month{"aug"} =  8;
$month{"sep"} =  9;
$month{"oct"} = 10;
$month{"nov"} = 11;
$month{"dec"} = 12;

$month{"Jan"} =  1;
$month{"Feb"} =  2;
$month{"Mar"} =  3;
$month{"Apr"} =  4;
$month{"May"} =  5;
$month{"Jun"} =  6;
$month{"Jul"} =  7;
$month{"Aug"} =  8;
$month{"Sep"} =  9;
$month{"Oct"} = 10;
$month{"Nov"} = 11;
$month{"Dec"} = 12;



print sort bydte <STDIN>;



sub bydte {
    ($junk,$datetimea) = split(/[\[\]]/, $a);
    ($junk,$datetimeb) = split(/[\[\]]/, $b);

    ($daya,$montha,$yeara,$houra,$mina,$seca) = split(/[\/: ]+/, $datetimea);
    ($dayb,$monthb,$yearb,$hourb,$minb,$secb) = split(/[\/: ]+/, $datetimeb);

    $result = ($yeara <=> $yearb);
    if ($result != 0) {return $result;}

    $result = ($month{$montha} <=> $month{$monthb});
    if ($result != 0) {return $result;}

    $result = ($daya <=> $dayb);
    if ($result != 0) {return $result;}

    $result = ($houra <=> $hourb);
    if ($result != 0) {return $result;}

    $result = ($mina <=> $minb);
    if ($result != 0) {return $result;}

    $result = ($seca <=> $secb);
    return $result;
}
