#!/usr/bin/perl
#------------------------------------------------------------------------------
# Split access logs by year. Hard-coded filenames!
# Quite fast: processes 320 Mb/min on Egret.
#------------------------------------------------------------------------------

open(INPUT, "<egret_access_logs_2002_to_2007.log") or die "Can't open input\n";
open(OUTPUT2002, ">egret_access_log_2002.log") or die "Can't open 2002 output\n";
open(OUTPUT2003, ">egret_access_log_2003.log") or die "Can't open 2003 output\n";
open(OUTPUT2004, ">egret_access_log_2004.log") or die "Can't open 2004 output\n";
open(OUTPUT2005, ">egret_access_log_2005.log") or die "Can't open 2005 output\n";
open(OUTPUT2006, ">egret_access_log_2006.log") or die "Can't open 2006 output\n";
open(OUTPUT2007, ">egret_access_log_2007.log") or die "Can't open 2007 output\n";

while (<INPUT>) {
  my($line) = $_;
  chomp($line);
  if ($line =~ m/^\d+\.\d+.\d+\.\d+ .+ \[(\d+)\/(\w+)\/(\d+):(\d+):(\d+):(\d+) (\S+?)\]/ ) {
    my($day) = $1;
    my($month) = $2;
    my($year) = $3;
    my($hour) = $4;
    my($minute) = $5;
    my($second) = $6;
    my($timezone) = $7;
    
    if ($year eq "2002") { print OUTPUT2002 $line . "\n"; }
    elsif ($year eq "2003") { print OUTPUT2003 $line . "\n"; }
    elsif ($year eq "2004") { print OUTPUT2004 $line . "\n"; }
    elsif ($year eq "2005") { print OUTPUT2005 $line . "\n"; }
    elsif ($year eq "2006") { print OUTPUT2006 $line . "\n"; }
    elsif ($year eq "2007") { print OUTPUT2007 $line . "\n"; }
    else { print "UNKNOWN YEAR: $line\n"; }
  }
  else {
    print "UNRECOGNIZED LINE: $line\n";
  }
}
print "Done.\n";
