#!/usr/bin/perl

sub calculatePer {
    $fileName = "$_[2].html";
    $url = "http://www.basketball-reference.com/players/$_[1]/$fileName";
    system("wget -q $url");
    open (RESULT, $fileName);

    $stopStr = "<h2 data-mobile-header=\"\" style=\"\">Per Game</h2>\n";
    do {
        $buffer = readline(RESULT);
    } until ($buffer eq $stopStr);
    $stopStr = "   <td align=\"left\" ><a href=\"/players/$_[1]/$_[2]/gamelog/2015/\">2014-15</a>";
    do {
        $buffer = readline(RESULT);
    } until ($buffer =~ $stopStr || $buffer =~ "</html>");
    if ($buffer =~ "</html") {
	printf "%s NO DATA\n", $_[0];
	return;
    }
    for ($index = 0; $index < 6; $index++) {
        readline(RESULT);
    }
    $mpgLine = readline(RESULT);
    $mpg = substr($mpgLine, 22, 4);
    $bracket = rindex($mpg, "<", 3);
    if ($bracket != -1) {
        substr($mpg, $bracket, 1, ' ');
    }

    if ($_[3] eq "y") {
        $stopStr = "<h2 data-mobile-header=\"\" style=\"\">Per 36 Minutes</h2>\n";
	do {
	    $buffer = readline(RESULT);
	} until ($buffer eq $stopStr);
	$stopStr = "   <td align=\"left\" ><a href=\"/players/$_[1]/$_[2]/gamelog/2015/\">2014-15</a>";
	do {
	    $buffer = readline(RESULT);
	} until ($buffer =~ $stopStr);
	for ($index = 0; $index < 22; $index++) {
	    readline(RESULT);
	}
	$perLine = readline(RESULT);
	$val = substr($perLine, 22, 4);
	$bracket = rindex($val, "<", 3);
	if ($bracket != -1) {
	    substr($val, $bracket, 1, ' ');
	}
	
	readline(RESULT);
	readline(RESULT);
	$perLine = readline(RESULT);
	$val2 = substr($perLine, 22, 4);
	$bracket = rindex($val2, "<", 3);
	if ($bracket != -1) {
	    substr($val2, $bracket, 1, ' ');
	}
	
	$ast = $val*4/3;
	$to = $val2*4/3;
    }

    if ($_[3] eq "b") {
        $stopStr = "<h2 data-mobile-header=\"\" style=\"\">Per 36 Minutes</h2>\n";
	do {
	    $buffer = readline(RESULT);
	} until ($buffer eq $stopStr);
	$stopStr = "   <td align=\"left\" ><a href=\"/players/$_[1]/$_[2]/gamelog/2015/\">2014-15</a>";
	do {
	    $buffer = readline(RESULT);
	} until ($buffer =~ $stopStr);
	for ($index = 0; $index < 24; $index++) {
	    readline(RESULT);
	}
	$perLine = readline(RESULT);
	$val = substr($perLine, 22, 4);
	$bracket = rindex($val, "<", 3);
	if ($bracket != -1) {
	    substr($val, $bracket, 1, ' ');
	}
	
	$blk = $val/36*200;
    }

    $stopStr = "<h2 data-mobile-header=\"\" style=\"\">Advanced</h2>\n";
    do {
        $buffer = readline(RESULT);
    } until ($buffer eq $stopStr);
    $stopStr = "   <td align=\"left\" ><a href=\"/players/$_[1]/$_[2]/gamelog/2015/\">2014-15</a>";
    do {
        $buffer = readline(RESULT);
    } until ($buffer =~ $stopStr);
    for ($index = 0; $index < 6; $index++) {
        readline(RESULT);
    }
    $perLine = readline(RESULT);
    for ($index = 0; $index < 15; $index++) {
        readline(RESULT);
    }
    $wsLine = readline(RESULT);
    close (RESULT);
    $val = substr($perLine, 22, 4);
    $ws = substr($wsLine, 22, 5);
    $bracket = rindex($val, "<", 3);
    if ($bracket != -1) {
        substr($val, $bracket, 1, ' ');
    }
    $bracket = rindex($ws, "<", 4);
    if ($bracket != -1) {
        substr($ws, $bracket, 1, ' ');
    }
    if ($_[3] eq "y") {
	printf "%s %4.1f %.3f %.1f %.1f %.1f\n", $_[0], $val, $ws, $mpg, $ast, $to;
    }
    elsif ($_[3] eq "b") {
	printf "%s %4.1f %.3f %.1f %.1f\n", $_[0], $val, $ws, $mpg, $blk;
    }
    else {
	printf "%s %4.1f %.3f %.1f\n", $_[0], $val, $ws, $mpg;
    }

    system("rm", "$_[2].html");
}

print "     Starters\n";
&calculatePer("Kawhi Leonard        ", "l", "leonaka01", "n");
&calculatePer("Anderson Varejao     ", "v", "varejan01", "b");
&calculatePer("Jordan Hill          ", "h", "hilljo01", "b");
&calculatePer("John Wall            ", "w", "walljo01", "y");
&calculatePer("Victor Oladipo       ", "o", "oladivi01", "n");
print "     Bench\n";
&calculatePer("Gerald Green         ", "g", "greenge01", "n");
&calculatePer("DeJuan Blair         ", "b", "blairde01", "b");
&calculatePer("Amar'e Stoudemire    ", "s", "stoudam01", "b");
&calculatePer("Ramon Sessions       ", "s", "sessira01", "y");
&calculatePer("Rodney Stuckey       ", "s", "stuckro01", "y");
&calculatePer("Anthony Tolliver     ", "t", "tollian01", "b");
&calculatePer("Jonas Jerebko        ", "j", "jerebjo01", "b");
&calculatePer("Beno Udrih           ", "u", "udrihbe01", "y");
