#!/usr/bin/perl
#
#   Parses Lighttpd logfiles and inserts them into a MySQL database
#

use strict;
use warnings;
use Data::Dumper;

my @names = qw(ip id user datime req status bytes referer agent);

my @cols = qw(  ip        id        user    date    time    zone
                method    bytes    status    url    proto    type
                datime    req      referer   agent);
my $colstr = join( ',', @cols );
my @places;
for (@cols){push @places, '?'}
my $places = join ',', @places;

open (LOG, "< $ARGV[0]") or die "Cannot open file $ARGV[0]\n";

while (my $line = <LOG>){

    my %dat;
    my @fields = $line =~ m/("[^\"]*"|\[.*\]|[^\s]+)/g;

    $fields[3] =~ s/[\[\]]//g;
    ($dat{date}, $dat{time}) = split /:/, $fields[3], 2;
    ($dat{time}, $dat{zone}) = split / /, $dat{time}, 2;

    $fields[4] =~ s/"//g; #"
    ($dat{method}, $dat{url}, $dat{proto}) = split / /, $fields[4];

    if ($dat{url} =~ /\/$/){
        $dat{type} = 'dir';
    }else{
        ($dat{type}) = $dat{url} =~ /(\.\w+)$/g;
    }
    $dat{type} = 'file' unless $dat{type};

    $fields[7] =~ s/"//g; #"
    $fields[8] =~ s/"//g; #"

    for (0..$#names){
        $dat{ $names[$_] } = $fields[$_];
    }
    my @insert;
    for (@cols){
        push @insert, $dat{$_};
    }
    print Dumper(@insert);
}

close (LOG);
