#!/usr/local/bin/perl

$mailto0 = 'morikawa@gfd-dennou.org';
$server = 'toyoda.gfd-dennou.org:8087';
$cgi = 'cgi-bin/cvsweb.cgi';
$project = 'gtool4';
$charcode = 'iso-2022-jp';

# umask 0;
# open(LOG, ">/tmp/cvsdiff.tmp");

$mailto = $ARGV[0] || $mailto0;
$loginfo = $ARGV[1];
$user = $ENV{'USER'} || $ENV{'LOGNAME'} || $<;

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time);
$date = sprintf("%04d-%02d-%02dT%02d:%02d",
  $year + 1900, $mon + 1, $mday, $hour, $min);

open(MAIL, "|nkf|/usr/lib/sendmail $mailto"); # convert to JIS
select(MAIL);
print <<EOF;
From: $user\@gfd-dennou.org
To: $mailto
Subject: ${project}-cvs-commit $date
Mime-Version: 1.0
Content-Type: text/plain; charset=$charcode
Content-Transfer-Encoding: 8bit

EOF

@loginfo = split(/\s/, $loginfo);
$path = shift @loginfo;
$number = scalar(@loginfo);

if ($loginfo[0] eq '-') {
	@loginfo = ('.,NONE,NONE');
}

$s = 's' if $number != 1;

print <<EOF;
At $date, user $user commited following $number file$s.

EOF
foreach $hunc (@loginfo) {
	($fnam, $old, $new) = split(/,/, $hunc);
	$diff = ".diff?r1=text&tr1=$old&r2=text&tr2=$new&f=h";
	$diff = undef if ($old eq 'NONE' || $new eq 'NONE');
	print <<EOF;
$path/$fnam: $old to $new
  http://$server/$cgi/$path/$fnam$diff
EOF
}

print <<EOF;

EOF

while (<STDIN>) {
    s/\x0D\x0A|\x0D|\x0A/\n/g;  # convert each OS linefeed code to "\n"
    print;
}

close(MAIL);
