#!/usr/bin/perl -w

use strict;
use Time::Local;
use Benchmark;

my (@allrecords);
my ($startday);
my ($startweek);
my ($record);
my (%total);
my ($grandtotal);
my ($sum);

while (<>) {
    $record = [split];
    if (!$startday) {
	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
	   localtime($record->[3]);
	$startday = timelocal(0,0,0,$mday,$mon,$year);
	$startweek = $startday - (24*3600 * $wday);
    }
    my $useragent = $record->[10];

    # Find Useragent, Version #, OS
    $useragent =~ s/%([0-9a-fA-F]{2})/pack("H2", $1)/ge;
    $useragent =~ s{^Mozilla/\d\.\d .*\(compatible; (MSIE) (\d\.[0-9bB]+); .*((Win|Mac)[^;\)]+).*}{$1/$2; $3} or
    $useragent =~ s{^Mozilla/\d\.\d .*\(compatible; (Opera).*((?:Win|Mac)[^;\)]+)\) (.*).*}{$1/$3; $2} or
    $useragent =~ s{^(Mozilla/\d\.\S+) .*\(X11; .; (\S+).*}{$1; $2} or
    $useragent =~ s{^(Mozilla/\d\.\S+) .*\(([^;]+);.*}{$1; $2};

    # eliminate minor Version number from some browsers
    $useragent =~ s{^(Mozilla/\d)\.\S+; (.*)}{$1.x; $2} or
    $useragent =~ s{^(MSIE/\d)\.\S+; (.*)}{$1.x; $2} or
    $useragent =~ s{^(Teleport Pro/\d)\.\S+}{$1.x;};
    $useragent =~ s{^(Lynx/\d)\..+}{$1.x;};
    $total{$useragent} ++;
    $grandtotal ++;
}

for my $i (sort {$total{$b} <=> $total{$a}} keys (%total)) {
    print "  $i| ${total{$i}}| ";
    $sum += ${total{$i}};
    print "$sum| ", int($sum * 100 / $grandtotal), "\n";
    
}
