#!/usr/bin/php
<?php
/**
* Minecraft Death Counter
*
* Date: $Date: 2014-01-21 12:23:11 +0900 (火, 21  1月 2014) $
*/

mb_http_input('pass');
mb_http_output('pass');
mb_internal_encoding('utf-8');

$DEATH_LISTS=array(
	'#(drowned)#'
	,'#(took a long walk off a short pier)#'
	,'#(hit the ground too hard)#'
	,'#was [\w\s]+ by (.+)#'
	,'#(fell out of the world)#'
	,'#(tried to swim in lava)#'
	,'#(went up in flames)#'
	,'#(burned to death)#'
	,'#(blew up)#'
	,'#(suffocated in a wall)#'
	,'#(pricked to death)#'
	,'#(hugged a cactus)#'
	,'#(died)#'
);

main($argc, $argv);

function usage_exit(){
	$filename = basename(__FILE__);
	$str =<<<"EOD"
Usage: $filename [server.log]

EOD;
	fwrite(STDERR, $str);
	exit(1);
}

function main($argc, $argv){
	global $DEATH_LISTS;
	$exit_status=0;
	$death = array();

	if ($argc <=1){
		usage_exit();
	}

	$filename = $argv[1];
	$fp = fopen($filename, 'r') or die("can't open file. $filename\n");
	while($line = fgets($fp)){
		foreach($DEATH_LISTS as $key => $regexp){
			if(preg_match($regexp, $line, $matches)){
				$str = trim($matches[1]);
				@$death[$str]++;
				break;
			}
		}
	}
	fclose($fp);

	arsort($death);
	foreach($death as $key => $val){
		printf("%04d %s\n", $val, $key);
	}

	
	exit($exit_status);
}
