#!/usr/bin/perl -w
#
#  Copyright 2002,2003 Ketil Froyn <ketil@froyn.name>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# NOTES
#
# This tiny program can be useful if you are using multilog[1] to log and
# rotate your apache[2] logs. I have used multilog with apache like this:
#
# LogFormat "%h %l %u \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
# CustomLog "| /usr/local/bin/setuidgid httpdlog multilog t s4194304 n50 /etc/httpd/logs/multilog/access" combined
# 
# To get webalizer[3] to understand my new logs, I wrote this script. Use 
# it something like this:
#
# cat /etc/httpd/logs/multilog/access/{*.s,current} | tai64nlocal | multilog2apache
#
# This will massage the logs so they have the same format as the standard
# combined logformat in apache, and write it to STDOUT. Webalizer can take
# logs on STDIN.
#
# I used to use apache to write the log, and I want to include this in
# the analysis as well. To analyse the old weblog written by apache as well
# as the new ones written by multilog, I run a command something like this:
#
# (cat /etc/httpd/logs/access_log; cat /etc/httpd/logs/multilog/access/{*.s,current} 2>/dev/null | tai64nlocal | multilog2apache) | webalizer -Q -n localhost -o /var/www/logs/
#
# Enjoy :)
#
# [1] http://cr.yp.to/daemontools/multilog.html
# [2] http://httpd.apache.org/
# [3] http://www.mrunix.net/webalizer/
#
# $Id: multilog2apache,v 1.9 2004/02/19 11:02:21 ketil Exp $
#
use strict;

my $z=`date +%z`; chomp $z;
my %c=(
	"-01-" => "/Jan/",
	"-02-" => "/Feb/",
	"-03-" => "/Mar/",
	"-04-" => "/Apr/",
	"-05-" => "/May/",
	"-06-" => "/Jun/",
	"-07-" => "/Jul/",
	"-08-" => "/Aug/",
	"-09-" => "/Sep/",
	"-10-" => "/Oct/",
	"-11-" => "/Nov/",
	"-12-" => "/Dec/");

while (<>) {
    if (/^\d\d\d\d-\d\d-\d\d\s/) {
	chomp;
	s/^(\d\d\d\d)(-\d\d-)(\d\d) (\d\d:\d\d:\d\d)\.\d+/[$3$c{$2}$1:$4 $z]/;
	my @a=split(/ /,$_,6);
	print "$a[2] $a[3] $a[4] $a[0] $a[1] $a[5]\n";
    } else {
	print;
    }
}
