<?php
// +----------------------------------------------------------------------+
// | Eventum - Issue Tracking System                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003, 2004, 2005 MySQL AB                              |
// |                                                                      |
// | This program is free software; you can redistribute it and/or modify |
// | it under the terms of the GNU General Public License as published by |
// | the Free Software Foundation; either version 2 of the License, or    |
// | (at your option) any later version.                                  |
// |                                                                      |
// | This program is distributed in the hope that it will be useful,      |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
// | GNU General Public License for more details.                         |
// |                                                                      |
// | You should have received a copy of the GNU General Public License    |
// | along with this program; if not, write to:                           |
// |                                                                      |
// | Free Software Foundation, Inc.                                       |
// | 59 Temple Place - Suite 330                                          |
// | Boston, MA 02111-1307, USA.                                          |
// +----------------------------------------------------------------------+
// | Authors: Joćo Prado Maia <jpm@mysql.com>                             |
// +----------------------------------------------------------------------+
//
// @(#) $Id$
//

$eventum_domain = 'firestuff.org';
$eventum_relative_url = '/eventum/';
$eventum_port = 80;
$state_dir = '/home/ian/.eventum-arch';
$client = 'tla';


//
// DO NOT CHANGE ANYTHING AFTER THIS LINE
//

if (!isset($_SERVER)) {
	$_SERVER = $HTTP_SERVER_VARS;
}

if (count($_SERVER['argv']) != 2) {
	print("Usage: ".$_SERVER['argv'][0]." <archive name>\n");
	exit(1);
}

$archive = $_SERVER['argv'][1];
$e_archive = escapeshellarg($archive);

@mkdir($state_dir);
@mkdir($state_dir.'/'.$archive);

$categories = explode("\n",trim(`$client categories $e_archive`));

foreach ($categories as $category) {
	if (empty($category))
		continue;
		
	$e_category = escapeshellarg($category);

	$branches = explode("\n",trim(`$client branches $e_archive/$e_category`));

	foreach ($branches as $branch) {
		if (empty($branch))
			continue;

		$e_branch = escapeshellarg($branch);

		$versions = explode("\n",trim(`$client versions $e_archive/$e_branch`));

		foreach ($versions as $version) {
			if (empty($version))
				continue;

			$e_version = escapeshellarg($version);

			$co_dir = "$state_dir/$archive/$version";
			$e_co_dir = escapeshellarg($co_dir);

			if (!is_dir($co_dir))
				system("$client get --silent $e_archive/$e_version--base-0 $e_co_dir");

			if (chdir($co_dir)) {
				$missings = explode("\n",trim(`$client missing`));

				system("$client update");

				foreach ($missings as $missing) {
					if (empty($missing))
						continue;
					$missing = preg_replace('/.*--/','',$missing);

					$log = trim(`$client cat-log $e_archive/$e_version--$missing`);

					if (preg_match('/^Summary: (.*(?:issue|bug) ?:? ?#?(\\d+).*)/im',$log,$matches)) {
						$summary = $matches[1];
						$issue = $matches[2];

						$creator = '';
						if (preg_match("/^Creator: (.*)/m",$log,$matches))
							$creator = $matches[1];

						$files = array();
						if (preg_match("/^New-files: (.*(\n\\s.*)*)/m",$log,$matches))
							$files = array_merge($files,preg_split('/[\\s\\n]+/',$matches[1]));

						if (preg_match("/^Modified-files: (.*(\n\\s.*)*)/m",$log,$matches))
							$files = array_merge($files,preg_split('/[\\s\\n]+/',$matches[1]));

						if (preg_match("/^Renamed-files: (.*(\n\\s.*)*)/m",$log,$matches))
							$files = array_merge($files,preg_split('/[\\s\\n]+/',$matches[1]));
						
						if (preg_match("/^Removed-files: (.*(\n\\s.*)*)/m",$log,$matches))
							$files = array_merge($files,preg_split('/[\\s\\n]+/',$matches[1]));
						
						foreach ($files as $id => $file) {
							if ($file == '' || strstr($file,'.arch-ids') !== false)
								unset($files[$id]);
						}

						report_change("$archive/$version",$creator,$summary,$issue,$files,$missing);
					}
				}
			}
		}
	}
}

function report_change($module,$username,$commit_msg,$issue,$files,$new_version) {
	global $eventum_domain, $eventum_relative_url, $eventum_port;

	print("Reporting $module--$new_version for issue $issue.\n");

	$url = $eventum_relative_url . 'scm_ping.php';
	$url .= '?module='.base64_encode($module);
	$url .= '&username='.base64_encode($username);
	$url .= '&commit_msg='.base64_encode($commit_msg);
	$url .= '&issue[]='.base64_encode($issue);

	$i = 0;
	foreach ($files as $file) {
		$url .= "&files[$i]=".base64_encode($file);
		$url .= "&old_versions[$i]=".base64_encode('previous');
		$url .= "&new_versions[$i]=".base64_encode($new_version);
		$i++;
	}

	$fp = fsockopen($eventum_domain, $eventum_port, $errno, $errstr, 30);
	if (!$fp) {
		print("Error: Can't hit Eventum SCM script: $errstr\n");
		exit();
	} else {
		fwrite($fp, "GET $url HTTP/1.1\r\nHost: $eventum_domain\r\nConnection: close\r\n\r\n");
		fclose($fp);
	}
}

?>
