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

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

my %BROWSERID = qw(
	IE          ie
	Firefox     firefox
	Safari      safari
	Safari-iPad ios_saf
	Chrome      chrome
	Opera       opera

	Android     android
	iPhone      ios_saf
	iPod-Touch  ios_saf
);

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

if (@ARGV) {
	my %param = (
		bar             => 1,
		region_hidden   => 'ww', # Worldwide
		granularity     => 'monthly',
		fromMonthYear   => $ARGV[0],
		toMonthYear     => $ARGV[1] // $ARGV[0],
		csv             => 1,
	);
	my $site = $count{-site};
	$site .= 'chart.php?'.join('&', map "$_=$param{$_}", keys %param);
	$site .= '&statType_hidden=';
	$count{-source} = $site;

	require LWP::Simple;
	{
		my $url = $site.'browser_version';
		my $download = LWP::Simple::get($url) or die "No data from $url\n";
		open my $input, '<', \$download or die $!;
		parsecsv($input);
	}

	{
		my $url = $site.'mobile_vs_desktop';
		my $download = LWP::Simple::get($url) or warn "No data from $url\n";
		open my $input, '<', \$download or die $!;
		parsecsv($input);

		my %mult = map {
			$_ => ( delete $count{$_} )->{0} / 100,
		} qw(Desktop Mobile);

		# apply desktop multiplier with inverse of mobile (reverted below)
		for (values %count) {
			ref $_ eq 'HASH' or next;
			$_ *= $mult{Desktop} / $mult{Mobile} for values %$_;
		}
		$count{Opera} = delete $count{opera};  # preserve

		$url = $site.'mobile_browser';
		$download = LWP::Simple::get($url) or warn "No data from $url\n";
		open $input, '<', \$download or die $!;
		parsecsv($input);

		# apply mobile multiplier to everything (undoing it for desktop)
		for (values %count) {
			ref $_ eq 'HASH' or next;
			$_ *= $mult{Mobile} for values %$_;
		}
		$count{op_mini} = delete $count{opera};  # mobile distinction
		$count{opera}   = delete $count{Opera};  # restore desktop
	}
}
else {
	parsecsv(\*STDIN);
}

sub parsecsv {
	my ($input) = @_;
	my $csv = Text::CSV->new or die Text::CSV->error_diag;
	while (my $row = $csv->getline($input)) {
		my ($browser, $pct) = @$row;
		$browser and $pct or next;

		my $version = $browser =~ s/ (\d\S*)$// ? $1 : 0;
		given ($browser) {
			when (['Browser Version', 'Mobile Browser', 'Mobile vs. Desktop']) {
				$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

F<convert-stats-gs> <year-month>

