#!/usr/bin/perl -w
#############################################################################
# This code is free software                                                #
#############################################################################
#                                                                           #
# 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 Library 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.#
#                                                                           #
# written by Reid Canavan, rcanavan@digital-gateway.ca                      #
#                                                                           #
#############################################################################
# To run: perl squid-log-converter.pl < access.log > output.txt             #
#############################################################################
# Result: Text File, space delimited with " Text Qualifier for URL requests #
#############################################################################

@tmp_file = <STDIN>;  #reads the file into an array

#read current time...
($null,$minute,$hour,$day,$month,$year,$null,$null,$null)=localtime(time);
$year = $year + 1900;
$month = $month + 1;

foreach $line (@tmp_file)
{
chop($line);
$line =~ s/ /\"\+\"/g;
@tmp_line = split /\"\+\"/, $line; # separate the spaces into new lines

$datum_bearbeitet = 0;  # Set date converter to 0 (Not Completed)
$tmp_chk = 0; # Set Log Type converter to 0 (Not Completed)
$tmp_url = 0; # Set URL converter to 0 (Not Completed)
foreach $tmp_var (@tmp_line)
{
#Date:
	if (($tmp_var =~ m/\d+\.\d+/) and ( $datum_bearbeitet eq 0))
	{
		#convert UTC-Date into human-readable format:
		($null, $minute, $hour, $day, $month, $year, $null, $null, $null )= localtime($tmp_var);
		$year= $year + 1900;
		$month = $month + 1;
		if ($minute < 10) { $minute = '0'.$minute;} #add a 0 if minute <10
		print " $year\-$month\-$day $hour:$minute";
		$datum_bearbeitet = '1';
	}
	elsif (($tmp_var eq '') or ($tmp_var eq ' '))
	{
                $tmp_url = $tmp_url - 1;
		#Do Nothing
	}
        elsif (($tmp_var =~ m/TCP/) and ($tmp_chk eq 0))
        {
        	$tmp_length = length($tmp_var);
        	$tmp_type = substr($tmp_var,4,($tmp_length - 8));
        	$tmp_code = substr($tmp_var,($tmp_length - 3));
        	print " $tmp_type";
        	print " $tmp_code";
        	$tmp_chk = '1';
        }
        elsif (($tmp_var =~ m/NONE/) and ($tmp_chk eq 0))
        {
        	$tmp_length = length($tmp_var);
        	$tmp_type = substr($tmp_var,5,($tmp_length - 8));
        	$tmp_code = substr($tmp_var,($tmp_length - 3));
        	print " $tmp_type";
        	print " $tmp_code";
        	$tmp_chk = '1';
        }
        elsif ($tmp_url eq '6')
        {
               print " \"$tmp_var\"";
        }
        else
	{
        print " $tmp_var";
        }
        $tmp_url++;
}
print "\n";
}
