<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | Eventum - Issue Tracking System                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003, 2004 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>                             |
// |          Adam Ratcliffe <adam.ratcliffe@geosmart.co.nz>              |
// |          Frederik M. Kraus <f.kraus@pangora.com>                     |
// +----------------------------------------------------------------------+
//
// @(#) $Id$
//

$eventum_domain = 'www.geosmartlbs.co.nz';
$eventum_relative_url = '/eventum/';
$eventum_port = 80;
$svnlook = '/usr/bin/svnlook';
$separatorChar = "/";

//
// DO NOT CHANGE ANYTHING AFTER THIS LINE
//

if(!file_exists($svnlook)) {
    echo 'svnlook does not exist, edit $svnlook\n';
    exit(1);
}

if(count($argv) < 3) {
    echo 'missing arguments, got ' . count($argv) . ' expected 3\n';
    exit(1);
}

$repos = $argv[1];
$rev = $argv[2];
$oldRev = $rev - 1;

$svn_module = base64_encode(substr(strrchr($repos, $separatorChar), 1));

$results = array();
exec($svnlook . ' info ' . $repos . ' -r ' . $rev, $results);

$username = base64_encode(array_shift($results));
$date = array_shift($results);
array_shift($results); // ignore file length

$commit_msg = '';
foreach ($results as $line) {
    $commit_msg .= $line."\n";
}
// now we have to strip html-tags from the commit message
$commit_msg = strip_tags($commit_msg);

$files = array();
exec($svnlook . ' changed ' . $repos . ' -r ' . $rev, $files);
foreach ($files as $file_info) {
    $pieces = explode('   ', $file_info);
    $filename = $pieces[1];
    $modified_files[] = array(
    'filename'     => "/" . $filename,
    'old_revision' => $oldRev,
    'new_revision' => $rev
    );
}

// parse the commit message and get all issue numbers we can find
$pattern = "/[issue|bug] ?:? ?#?(\d+)/i";
preg_match_all($pattern, $commit_msg, $matches);

if (count($matches) == 2 && count($matches[1]) > 0) {
    $commit_msg = base64_encode($commit_msg);
    foreach ($matches[0] as $p_issue_id) {
        echo "Matched ISSUE #".$p_issue_id."\n";
        $issue_id = base64_encode($p_issue_id);
        // build the GET url to use
        $ping_url = $eventum_relative_url .
                    "scm_ping.php?module=$svn_module&username=$username&commit_msg=$commit_msg";
        $ping_url .= "&issue[]=$issue_id";
        for ($i = 0; $i < count($modified_files); $i++) {
            $ping_url .= "&files[$i]=" . base64_encode($modified_files[$i]['filename']);
            $ping_url .= "&old_versions[$i]=" .
                         base64_encode($modified_files[$i]['old_revision']);
            $ping_url .= "&new_versions[$i]=" .
                         base64_encode($modified_files[$i]['new_revision']);
        }

        $fp = fsockopen($eventum_domain, $eventum_port, $errno, $errstr, 30);

        if (!$fp) {
            echo "Error: Could not ping the Eventum SCM handler script.\n";
            exit();
        } else {
            $msg = "GET $ping_url HTTP/1.1\r\n";
            $msg .= "Host: $eventum_domain\r\n";
            $msg .= "Connection: Close\r\n\r\n";
            fwrite($fp, $msg);
            fclose($fp);
        }
    }
} else {
    echo "No Match found\n";
}

?>
