#!/usr/bin/perl

my ($op, $mod, $rin, $file);

$op = $ARGV[0];

@ARGV = split(' ', $ARGV[0]); # all in the first argument
$mod = shift; # module name

if ($op =~ /- Imported sources$/) {
	$op = 'import';
	$file = "$ENV{USER} imported files into module '$mod'."
} else {

	$op = 'commit';
	
	my ($oldvers, $newvers, @files);
	while (($file, $oldvers, $newvers) = split(',', shift)) {
		$oldvers = '<added>'   if $oldvers eq 'NONE';
	 	$newvers = '<deleted>' if $newvers eq 'NONE';
		push @files, "\t$file [$oldvers -> $newvers]";
	}

	$file = "In module '$mod', $ENV{USER} committed:\n\n" . join("\n", @files);
}

open (MSG, "|sendmail address\@myhost.com"); ## CHANGE ME!
#open (MSG, ">blah.txt");

my ($indir, $modfiles, $logmsg, $cvsroot);

$cvsroot = $ENV{CVSROOT};
$cvsroot =~ s|/$||;

HEADER: while (<>) {
	last HEADER if /^$/;
	$mod = $1,    next HEADER if ?^Update of $cvsroot/(.*)?o;
  $indir = $1,  next HEADER if ?^In directory (.*)?;
}

my (@modfiles, $filelist, $repl);

if ($op eq 'commit') {

	MODFILE: while (<>) {
		last MODFILE if /^Log/;
		if (/^(\w+)/) {
			if    ($1 eq 'Modified') { $repl = undef; }
			elsif ($1 eq 'Removed')  { $repl = '"($1)"'; }
			elsif ($1 eq 'Added')    { $repl = '"+$1"'; }
			next MODFILE;
		}
		s/^\t//;
		chomp;
		s/ $//;
		s/^(.*)$/$repl/ee if $repl;
		push @modfiles, $_;
	}

	$filelist = ': ' . join(', ', @modfiles);

} else { # got a log message instead
	$_ = <>;
}

print MSG qq|From: ### CHANGE ME ### <cvs\@myhost.com>
To: CVSS commits <commits\@myhost.com>
Subject: CVS $op by $ENV{USER} in $mod$filelist
Reply-To: $ENV{USER}\@myhost.com

$file

Log message:

\t|;

while (<>) {
	print MSG $_;
}

close MSG;
