#! /usr/bin/perl
# CVS logging script
# hacked by tridge@samba.org, May 2001
#
# to use, add a line like this to loginfo
#
#    module $CVSROOT/CVSROOT/log -m foo@blah %{sVv}
#

my $users = "";
my $cvsroot = $ENV{'CVSROOT'};
my $logfile = "";
my $cvsweb = "http://www.samba.org/cgi-bin/cvsweb";
my @files;

# parse command line arguments
#
while (@ARGV) {
        my $arg = shift @ARGV;
	
	if ($arg eq '-m') {
                $users = "$users " . shift @ARGV;
	} elsif ($arg eq '-f') {
		$logfile = shift @ARGV;
	} else {
		@files = split(/ /, $arg);
	}
}

# the first argument is the module location relative to $CVSROOT
my $modulepath = shift @files;
my ( $module ) = split(/\//, $modulepath);

if (! $logfile) {
  $logfile = "$cvsroot/CVSROOT/$module.updates";
}


my $mailcmd = "| Mail -s 'CVS update: $modulepath'";

# get a login name for the guy doing the commit....
$login = getlogin || (getpwuid($<))[0] || "nobody";

# open log file for appending
open(OUT, ">>" . $logfile) || die "Could not open(" . $logfile . "): $!\n";

# send mail, if there's anyone to send to!
if ($users) {
	$mailcmd = "$mailcmd $users";
	open(MAIL, $mailcmd) || die "Could not Exec($mailcmd): $!\n";
}

my $tstring = localtime();

# print out the log Header
print OUT "\n";
print OUT "****************************************\n";
print OUT "Date:\t$tstring\n";
print OUT "Author:\t$login\n\n";

if (MAIL) {
	print MAIL "\n";
	print MAIL "Date:\t$tstring\n";
	print MAIL "Author:\t$login\n\n";
}

# print the stuff from logmsg that comes in on stdin to the logfile
while (<>) {
	print OUT $_;
	if (MAIL) { print MAIL $_; }
}

# and revisions of all files
print OUT "\nRevisions:\n";
if (MAIL) { print MAIL "\nRevisions:\n"; }

while (@files) {
	my $s = shift @files;
	my ($file, $rev1, $rev2) = split(/,/, $s);
	if ($file eq "-") {
	  # directory creation
	  last;
	}
	my $rev = "$file\t\t$rev1 => $rev2\n\t$cvsweb/$modulepath/$file?";
	if ($rev1 eq "NONE") {
	  $rev .= "rev=$rev2\n";
	} elsif ($rev2 eq "NONE") {
	  $rev .= "rev=$rev1\n";
	} else {
	  $rev .= "r1=$rev1&r2=$rev2\n";
	}
	print OUT $rev;
	if (MAIL) { print MAIL $rev; }
}

close(OUT);
close(MAIL);

exit 0;
