#!/usr/bin/perl
#------------------------------------------------------------------------------
# Split access logs by year. Hard-coded filenames!
# Quite fast: 320 Mb/min on Egret.
#------------------------------------------------------------------------------

open(INPUT, "<egret_access_log_2007.log") or die "Can't open input\n";
open(OUTPUT01, ">egret_access_log_2007_01.log") or die "Can't open 2007_01 output\n";
open(OUTPUT02, ">egret_access_log_2007_02.log") or die "Can't open 2007_02 output\n";
open(OUTPUT03, ">egret_access_log_2007_03.log") or die "Can't open 2007_03 output\n";
open(OUTPUT04, ">egret_access_log_2007_04.log") or die "Can't open 2007_04 output\n";
open(OUTPUT05, ">egret_access_log_2007_05.log") or die "Can't open 2007_05 output\n";
open(OUTPUT06, ">egret_access_log_2007_06.log") or die "Can't open 2007_06 output\n";
open(OUTPUT07, ">egret_access_log_2007_07.log") or die "Can't open 2007_07 output\n";
open(OUTPUT08, ">egret_access_log_2007_08.log") or die "Can't open 2007_08 output\n";
open(OUTPUT09, ">egret_access_log_2007_09.log") or die "Can't open 2007_09 output\n";
open(OUTPUT10, ">egret_access_log_2007_10.log") or die "Can't open 2007_10 output\n";
open(OUTPUT11, ">egret_access_log_2007_11.log") or die "Can't open 2007_11 output\n";
open(OUTPUT12, ">egret_access_log_2007_12.log") or die "Can't open 2007_12 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 ($month eq "Jan") { print OUTPUT01 $line . "\n"; }
    elsif ($month eq "Feb") { print OUTPUT02 $line . "\n"; }
    elsif ($month eq "Mar") { print OUTPUT03 $line . "\n"; }
    elsif ($month eq "Apr") { print OUTPUT04 $line . "\n"; }
    elsif ($month eq "May") { print OUTPUT05 $line . "\n"; }
    elsif ($month eq "Jun") { print OUTPUT06 $line . "\n"; }
    elsif ($month eq "Jul") { print OUTPUT07 $line . "\n"; }
    elsif ($month eq "Aug") { print OUTPUT08 $line . "\n"; }
    elsif ($month eq "Sep") { print OUTPUT09 $line . "\n"; }
    elsif ($month eq "Oct") { print OUTPUT10 $line . "\n"; }
    elsif ($month eq "Nov") { print OUTPUT11 $line . "\n"; }
    elsif ($month eq "Dec") { print OUTPUT12 $line . "\n"; }
    else { print "UNKNOWN MONTH: $line\n"; }
  }
  else {
    print "UNRECOGNIZED LINE: $line\n";
  }
}
print "Done.\n";
