#!/usr/bin/perl -w 
#
# $Id: shrimp.pl,v 1.3 1997/02/21 09:41:40 cord Exp cord $
#
#
# DESCRIPTION: shrimp.pl - a filter to transform Squid-1.0.x and Squid-1.1.x
#	Native-Log-Format into the Common Log Format. See
#	http://squid.nlanr.net/Squid/
#
# AUTHOR: Cord Beermann (cord@Wunder-Nett.org)
#
# 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.
#
# Don't show it to Larry Wall or to Tom Christiansen. ;-)
#
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#
# Instructions:
# * Filter the Native Log through this.
# * Enter you desired Timezone here:
$TZ="+0100";
#
# Bugs and shortcomings
# * It's not that fast ;-)
#
#

while (<>) {
	if (m#^(\d+)\.\d+\s+\d+\s+([^\s]+)\s+\w+/(\d+)\s+(\d+)\s+(\w+)\s+([^\s]+)\s+([\w\-\.]+)\s+\w+/[\w\.\-]+#) {
		# Squid 1.1.x Native Log
		$date = localtime($1);
		$host = $2;
		$code = $3;
		$size = $4;
		$method = $5;
		$url = $6;
		$ident = $7;
	} elsif (m#^(\d+)\.\d+\s+\d+\s+([^\s]+)\s+\w+/(\d+)/\w+\s+(\d+)\s+(\w+)\s+([^\s]+)#) {
		# Squid 1.0.x Native Log
		$date = localtime($1);
                $host = $2;
                $code = $3;
                $size = $4;
                $method = $5;
                $url = $6;
                $ident = "-";
	} else {
		chomp;
		warn("can't parse line '$_'");
		next;
	}
	$date =~ s|\w+ (\w+) (\d+) ([\d\:]+) (\d+)|$2/$1/$4:$3|;
	$date =~ s|\w+ (\w+)  (\d) ([\d\:]+) (\d+)|0$2/$1/$4:$3|;
	print "$host $ident - [$date $TZ] \"$method $url HTTP/1.0\" $code $size\n";
}
