#!/usr/bin/php
<?php

$columns = array('Nbr of files' => 'Number of files transferred', 'Size' => 'Total file size', 'Transferred' => 'Total transferred file size', 'Generation' => 'File list generation time', 'Transfer' => 'File list transfer time');

function getValue($value)
{
	static $timeUnits = array('minutes' => 60, 'hours' => 60, 'days' => 24);
	static $fileUnits = array('KB' => 1024, 'MB' => 1024, 'GB' => 1024);

	if (preg_match('/^([^\s]+)\s+(bytes|seconds)$/', $value, $matches))
	{
		switch ($matches[2])
		{
			case 'bytes':
				$units = $fileUnits;
				$matches[2] = 'B ';
				break;

			case 'seconds':
				$units = $timeUnits;
				break;
		}

		foreach ($units as $unit => $value)
		{
			if ($matches[1] >= $value)
			{
				$matches[1] /= $value;
				$matches[2] = $unit;
			}
		}

		$value = round($matches[1], 2) . ' ' . $matches[2];
	}

	return $value;
}

$report = array();

$line = fgets(STDIN, 4096);

while (!feof(STDIN)) {
	if (preg_match('/rsync\s+(?:[^\s]+\s+)--stats/', $line))
	{
		$rsync = rtrim(trim($line), '\\');

		while (!preg_match('/^\s*$/', $line))
		{
			$line = fgets(STDIN, 4096);
			$rsync .= rtrim(trim($line), '\\');
		}

		$rsync = explode(' ', $rsync);
		$source = $rsync[sizeof($rsync) -2];
		unset($rsync);

		while ($line = fgets(STDIN, 4096))
		{
			if (preg_match('/([^:]+):\s+(.*)$/', $line, $matches))
			{
				if (in_array($matches[1], $columns) === true)
				{
					$report[$source][$matches[1]] = getValue($matches[2]);
				}
			}
			else
			{
				break;
			}
		}
	}
	else if (preg_match('/^Database dump\s+(.+)$/', $line, $matches))
	{
		$source = $matches[1];

		while ($line = fgets(STDIN, 4096))
		{
			if (preg_match('/([^:]+):\s+(.*)$/', $line, $matches))
			{
				if (in_array($matches[1], $columns) === true)
				{
					$report[$source][$matches[1]] = getValue($matches[2]);
				}
			}
			else
			{
				break;
			}
		}
	}
	else
	{
		$line = fgets(STDIN, 4096);
	}
}

$titles = array();

foreach ($report as $source => $values)
{
	$length = strlen($source);

	if (isset($titles['Source']) === false)
	{
		$titles['Source'] = strlen('Source');
	}
	else if ($titles['Source'] < $length)
	{
		$titles['Source'] = $length;
	}

	foreach ($columns as $shortLabel => $label)
	{
		if (isset($values[$label]) === true)
		{
			$length = strlen($values[$label]);

			if (isset($titles[$shortLabel]) === false)
			{
				$titles[$shortLabel] = strlen($shortLabel);
			}
			else if ($titles[$shortLabel] < $length)
			{
				$titles[$shortLabel] = $length;
			}
		}
	}
}

foreach ($titles as $label => $length)
{
	$line[] = str_repeat('-', $length);
}

echo join('-|-', $line) . "\n";

$line = array();

foreach ($titles as $label => $length)
{
	$line[] = str_pad($label, $length, ' ', STR_PAD_BOTH);
}

echo join(' | ', $line) . "\n";

$line = array();

foreach ($titles as $label => $length)
{
	$line[] = str_repeat('-', $length);
}

echo join('-|-', $line) . "\n";

foreach ($report as $source => $values)
{
	$line = array(str_pad($source, $titles['Source'], ' ', STR_PAD_RIGHT));

	foreach ($columns as $shortLabel => $label)
	{
		if (isset($values[$label]) === true)
		{
			$line[] = str_pad($values[$label], $titles[$shortLabel], ' ', STR_PAD_LEFT);
		}
		else
		{
			$line[] = str_pad('N/A', $titles[$shortLabel], ' ', STR_PAD_LEFT);
		}
	}

	echo join(' | ', $line) . "\n";
}

$line = array();

foreach ($titles as $label => $length)
{
	$line[] = str_repeat('-', $length);
}

echo join('-|-', $line) . "\n";

?>
