#!/usr/bin/perl
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla-Watcher script.
#
# The Initial Developer of the Original Code is Plasmon Data Ltd
# Portions created by Plasmon Data Ltd are Copyright (C) 2003 Plasmon 
# Data Ltd. All Rights Reserved.
#
# Contributor(s): Dave Swegen <dswegen@software.plasmon.com>,
#                 Steve McIntyre <smcintyre@software.plasmon.com>
#
# Description:
# Grotty script that takes a CVS log entry and mangles it into an email
# that is sent to bugzilla.
# The entry in CVSROOT/loginfo looks as follows:
# ALL (echo ""; echo "Date: "`date`; cat) | $CVSROOT/CVSROOT/bugzilla-watcher $USER %{SVv}
# (The 'S' is provided by a seperate CVS patch. Use 's' if you don't have the
# patch. The script won't cope with spaces in filenames though).
# 
# $Id: bugzilla-watcher,v 1.47 2003/07/24 14:26:40 dsw Exp $

use strict;

# Set this to the name of the user who will fiddle with the
# bz database
my $ADDRESS = 'bugzilla-interface@example.com';

my $username = shift @ARGV;
my $prefix = $ARGV[0]; # Module and directory name go here
my $rest = $ARGV[0];   # All the file and version info goes in this
# Regexp that grabs everything up until first whitespace followed by quote
$prefix =~ s/^(.*?)\s*".*$/$1/;
# Essentially inverse of the above
$rest =~ s/^.*?\s*(".*)$/$1/; 

my @entries = split(/ "/, $rest);
my $pad = "  ";

my $arg;
foreach $arg (@entries) {
    print "$arg\n";
}
if ($entries[0] eq "- New directory") {
    exit 0;
}

my (@lines) = readline(*STDIN);

chomp @lines;

my ($bug, $tag, $i, @rawlog);
my $done = 0;
while (! $done) {
    if ($lines[0] =~ /Log Message/) {
        $done = 1;
        shift @lines;
    }
    elsif ($lines[0] =~ s/^\s*Tag:\s*(.*)$/$1/) {
        $tag = shift @lines;
        chomp $tag;
    }
    else {
        shift @lines;
    }
}
if (! $tag) {
    $tag = "HEAD";
}
while (@lines) {
    my (%bugdict);
    if ($lines[0] =~ s/^BugID:?\s*#?([\d,\s]+)\s*$/$1/i) {
        # Get rid of useless spaces
        while ($lines[0] =~ s/\D{2,}/ / == 1) {};
        $bug = shift @lines;
        # Find all the bugs, split them by whitespace/commas, and put into
        # a hash for easier retrieval/uniqueness
        foreach $i (split /[\s+,+]/, $bug) {
            $bugdict{"$i"} = 1;
        }
        $bug = join(' ', keys %bugdict);
        print "Appending log to bug(s) $bug\n";
    }
    elsif ($lines[0] =~ /^BugID:?\s*.*$/) {
        print "BugID line found, but invalid entry. Ignoring.\n";
        push @rawlog, $pad . shift @lines;
    }
    else {
        push @rawlog, $pad . shift @lines;
    }
}
if (! $bug) {
    print "No bugID found. Not appending log message.";
    exit 0;
}
my $log = join("\n",@rawlog);
my $filelines = "";
my $filename = "";
my (@line, $file, @files);
foreach $arg (@entries) {
    @line = split(",", $arg);
    $file = "$prefix/$line[0]\t$line[1]    $line[2]";
    # Get rid of whitespace-in-filename preserving quotes
    $file =~ s/\"//g;
    push @files, $file;
}
while (@files) {
    $filelines = $filelines . $pad . (shift @files) . "\n";
}

my $subject = "[Bug $bug] CVS Checkin";


open MAIL, "| /usr/bin/mail -s \"$subject\" $ADDRESS " || die "Failed to open mail";
# Remove trailing empty lines
$/="";
# Remove various forms of whitespace
$log =~ s/^\s+//s;
$log =~ s/\s+$//s;
$filelines =~ s/^\s+//s;
$filelines =~ s/\s+$//s;
print (MAIL "CVS COMMIT\nLOG MESSAGE:\n$pad$log\nBRANCH: $tag\nFILES CHECKED IN:\n$pad$filelines");
close MAIL;
