#!/usr/bin/perl
#
# $Id: mailto-committers.pl,v 1.4 2000/03/14 23:02:56 baba Exp $
# Copyright (C) 1998 Hajime BABA.  All rights reserved.
#

$debug = 0;

require 'jcode.pl';

$cvs = "/usr/bin/cvs";
$rcsdiff = "/usr/bin/rcsdiff";
$sendmail = "/usr/sbin/sendmail";
$tail = "/usr/bin/tail";


sub parse_args {
    my(@tmp, $tmp);

    $CVSROOT = ENV{'CVSROOT'};

    print $ARGV[0], "\n" if $debug;
    $ARGV[0] =~ s/ - New directory//go;
    ($repository, @tmp) = split(/ /, $ARGV[0]);
    foreach $tmp (@tmp) {
	my($file, $old, $new) = split(/,/, $tmp);
	push(@files, $file);
	$old{$file} = $old;
	$new{$file} = $new;
    }
    $files = scalar(@files);

    shift @ARGV;
    print $ARGV[0], "\n" if $debug;
    $user = $ARGV[0];				# NG $user = $ENV{'USER'}; 
    
    shift @ARGV;
    foreach $tmp (@ARGV) {
	$mailaddr .= "$tmp, ";
    }
    $mailaddr =~ s/, $//;			# get rid of trailing comma.
    print $mailaddr, "\n" if $debug;
}

sub set_header {
    my($xheader_a) = "";
    my($xheader_r) = "";
    my($xheader_m) = "";
    my(@h, $file, @tmp, $date);

    $header = "To: $mailaddr\n";
    open(HISTORY, "$tail -$files $CVSROOT/CVSROOT/history |") || die;
    @h = <HISTORY>;
    close(HISTORY);

    foreach $file (reverse @files) {
	$history{$file} = pop(@h);
	if (@tmp = split(/\|/, $history{$file})) {
	    $date = &todate($tmp[0]);
	}
	if ($old{$file} eq 'NONE') {
	    $xheader_a = "X-Added-File: $repository/$file,v $new{$file} $date $user\n";
	} elsif ($new{$file} eq 'NONE') {
	    $xheader_r = "X-Removed-File: $repository/$file,v $new{$file} $date $user\n";
	} else {
	    $xheader_m = "X-Modified-File: $repository/$file,v $new{$file} $date $user\n";
	}
	$header .= $xheader_a;
	$header .= $xheader_r;
	$header .= $xheader_m;
    }
    $subject .= "Subject: CVS: $repository committed by $user.\n";
}

sub todate {
    local($tmp) = @_;
    local($tt) = hex(substr($tmp, 1, 8));
    local($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($tt);
    $year += 1900;
    $date = sprintf("%4d/%02d/%02d %02d:%02d:%02d",
		    $year, $mon+1, $mday, $hour, $min, $sec);
}

sub mail_notification {
    my($file, $fullname, $old, $new, $tmp);

#    open(MAIL, "| $sendmail -odb -oem -t");
    open(MAIL, ">> /tmp/commitlog");
    print(MAIL "$subject");
    print(MAIL "$header");
    print(MAIL "\n");
    print(MAIL "$history{$file}\n\n") if $debug;

    while (<STDIN>) {
	s#$CVSROOT/##go;
	&jcode::convert(*_, 'jis');
	print(MAIL "$_");
    }
    print(MAIL "\n");

    foreach $file (@files) {
	$fullname = $CVSROOT .'/'. $repository .'/'. $file;
	$old = $old{$file};
	$new = $new{$file};

	next if ($new eq 'NONE' || $old eq 'NONE');
#	open(RCSDIFF, "$rcsdiff -u -kk -r$old -r$new $fullname,v 2>&1 |") || next;
#	open(RCSDIFF, "$cvs -nQ rdiff -u -kk -r$old -r$new $repository/$file 2>&1 |") || next;
	open(RCSDIFF, "$cvs -nQ diff -u -kk -r$old -r$new $file 2>&1 |") || next;
	while (<RCSDIFF>) {
	    &jcode::convert(*_, 'jis');
	    print(MAIL "$_");
	}
	print(MAIL "\n");
	close(RCSDIFF);
    }
    close(MAIL);
    sleep(2);
}

sub main {
    &parse_args();
    &set_header();
    &mail_notification();
}

# go!
&main();

## EOF
