#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;

use Data::Dump 'pp';
use Text::CSV;

my %BROWSERID = qw(
	IE          trident
	Firefox     gecko
	Safari      webkit_saf
	Safari-iPad ios_saf
	Chrome      webkit_chr
	Opera       presto
);

my %count = (
	-source => 'StatCounter',
	-site   => 'http://gs.statcounter.com/',
);

my $csv = Text::CSV->new or die Text::CSV->error_diag;
while (my $row = $csv->getline(\*STDIN)) {
	my ($browser, $pct) = @$row;
	$browser and $pct or next;

	my $version = $browser =~ s/ (\d\S*)$// ? $1 : 0;
	given ($browser) {
		when ('Browser Version') {
			$pct =~ s/^Market Share % // and $count{-date} = $pct;
		}
		$browser =~ tr/ /-/;
		$browser = $_ for $BROWSERID{$browser} // ();
		$version =~ s/\.0$//;
		$count{$browser}->{$version} += $pct;
	}
}
$csv->eof or $csv->error_diag;

print pp(\%count);

__END__

=head1 USAGE

cat <csv data> | F<convert-stats-gs> > browser-usage-gs.inc.pl

