#!/usr/local/bin/perl
$ID = q$Id: cvslog,v 1.1 1999/07/30 03:42:28 scrappy Exp $;
#
# cvslog -- Mail a CVS log message to a given address.
#
# Written by Russ Allbery <rra@stanford.edu>
# Copyright 1998 Board of Trustees, Leland Stanford Jr. University
#
# This program is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
# This program is designed to run from CVS's loginfo administrative file and
# takes a log message, massaging it and mailing it to the address given on
# the command line.  It's a modified version of the log script that comes
# with CVS, but tries to do less (it doesn't do cvs status).
#
# It should be run from loginfo with something like:
#
#       ^leland         $CVSROOT/CVSROOT/cvslog %s
#       ^leland/dbmodel $CVSROOT/CVSROOT/cvslog -a ls-dbmodel@lists %s
#
# Note that it mails everything to the address configured at the top of this
# file in addition to any other addresses given on the command line with -a.

# Mail all reports to this address.
$COMMIT = 'pgsql-committers@postgresql.org';

# Add this string to the beginning of the subject header of each message.
$PREFIX = '[COMMITTERS] ';

require 5.003;

use POSIX qw(strftime);

use strict;
use vars qw($COMMIT $ID $PREFIX);

# Grab the address to mail this report to.
my $address;
if ($ARGV[0] eq '-a') {
    shift;
    $address = shift or die "$0: -a given but no address specified\n";
}

# The next arguments are from %s; first the relative path in the repository
# and then the list of files modified.
my @files = split (' ', shift);
my $module = shift @files or die "$0: no module specified\n";
unless (@files) { die "$0: no files specified\n" }

# Figure out who is doing the update.
my $user = (getpwuid $<)[0] || $<;

# Build the headers.
my (@output, @header);
my $date = strftime ('%A, %B %e, %Y @ %T', localtime time);
if ($address) {
    push (@header, "To: $address\nCc: $COMMIT\n");
} else {
    push (@header, "To: $COMMIT\n");
}
push (@header, <<"EOH");

  Date: $date
Author: $user
EOH

# The first line of the log message will be "Update of ..."; just print it.
push (@output, scalar <STDIN>);

# Now comes the path to the local working directory.  Grab it and clean it
# up, and then ignore the next blank line.
$_ = <STDIN>;
my ($local) = /directory (\S+)/;
$local = "     from $local\n";
push (@output, $local);
<STDIN>;

# Add blank lines between the file lists.
my $tag;
while (<STDIN>) {
    last if /^Log Message/;
    $tag = $1, next if /^\s*Tag: (\S+)\s*$/;
    push (@output, "\n") if /Files:\s*/;
    push (@output, $_);
}

# Now construct the subject line, since we know the tag (if any).
if ($tag) {
    unshift (@header, "Subject: $PREFIX$tag $module (@files)\n");
    push (@header, "   Tag: $tag\n");
} else {
    unshift (@header, "Subject: $PREFIX$module (@files)\n");
}
unshift (@output, @header, "\n");

# Open our mail program.
my $mailcmd = "| /usr/sbin/sendmail -t -oi -oem -f $user\@postgresql.org";
open (MAIL, $mailcmd) || die "$0: cannot fork sendmail: $!\n";

# Now, print out the output so far and the log message.
print MAIL (@output, "\n", '-' x 29, "  Log Message  ", '-' x 29, "\n\n");
print MAIL while (<STDIN>);

# Make sure sending mail succeeded.
close MAIL;
unless ($? == 0) { die "$0: sendmail exit status " . ($? >> 8) . "\n" }
