#!/usr/bin/perl
# $Id: pcvs,v 1.2 2001/01/16 23:29:59 muhri Exp $
#Stolen from Mandrake and Raster !!!! Thanks guys. Shame on us perl hackers.. 
#We are so lazy.. hehe

if(($ARGV[0] =~ /com/) || ($ARGV[0] =~ /ci/)) {
	print "We're doing a commit, time to edit the logfile.\n";
	$ARGV[0] .= " -F CommitLog";
	$username=$ENV{USER};
	if (defined $ENV{CVSROOT} && $ENV{CVSROOT} =~ /\:(.+)\:(.+)\@(.*)/){
		$username=$2;
	}
	open COMMITLOG, ">CommitLog";
	print COMMITLOG "Date: ";
	print COMMITLOG `date`;
	print COMMITLOG "\n";
	print COMMITLOG "($username)\n";
  print COMMITLOG "\n";
	print COMMITLOG "\n\n";
	close COMMITLOG;
	if($ENV{EDITOR}) {
		system("$ENV{EDITOR} CommitLog");
	} else {
		system("vi CommitLog");
	}

	open COMMITLOG, "CommitLog";
	@lines = <COMMITLOG>;
	close COMMITLOG;
	if($#lines < 3) {
		unlink "CommitLog";
		print "Obviously this was supposed to be an aborted commit.\n";
		exit(0);
	}
	@ARGV2 = @ARGV;
	$ARGV2[0] = "update";

	print "Force updating changelog\n";
	unlink "ChangeLog";
	system("cvs -z3 update ChangeLog");

	print "Updating the files you are committing.\n";
	system("cvs -z3 @ARGV2 2>&1 |tee errors");

	open ERRORS, "errors";
	while(<ERRORS>) {
		if(/conflicts during merge/) {
			print "There's a conflict.  Resolve and try again.\n";
			unlink "errors" if(-f "errors");
			exit(0);
		}
	}
	close ERRORS;
	
	unlink "errors" if(-f "errors");



	print "Updating the changelog with your entry\n";
	open CHANGELOG, "<ChangeLog";
	@lines = <CHANGELOG>;
	close CHANGELOG;
	`cat CommitLog > ChangeLog`;
	open CHANGELOG, ">>ChangeLog";
	print CHANGELOG "\n-------------------------------------------------------------------------------\n\n";
	foreach (@lines) {
		print CHANGELOG $_;
	}
	close CHANGELOG;

	if($#ARGV >= 1) {
		$found = 0;
		foreach(@ARGV) {
			if(/ChangeLog$/) {
				$found = 1;
			}
		}

		push @ARGV, "ChangeLog" if(!$found);

		$found = 0;
		foreach(@ARGV) {
			if(/timestamp\.h$/) {
				$found = 1;
			}
		}


	}
}

system("cvs -z3 @ARGV");
unlink "CommitLog" if(-f "CommitLog");



`rm -f *~`;
