#!/usr/bin/perl -w
#
# $FreeBSD: CVSROOT/commitcheck,v 1.13 2000/04/12 02:59:17 obrien Exp $

# The "commitcheck" script is used to control pre-commit checks.
# It is evoked by the "commitinfo" configuration file.
# It is invoked with the repository and a list of files to check.
# A non-zero exit of the filter program will cause the commit to
# be aborted.

use strict;

# Detaint the world.
my $CVSROOT = $ENV{'CVSROOT'} || "/home/ncvs";
$ENV{PATH}  = "/bin:/sbin:/usr/bin:/usr/sbin";
delete      @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
$CVSROOT    =~ /(.*)/;
$CVSROOT    = $1;

# Get the configuration.
require         "$CVSROOT/CVSROOT/config.pm";
use vars        qw(@MASTERHOSTS $REQUIRED_GROUP);
import config;


# Check that the commit is happening on a designated host, to prevent
# commits to repository copies.
my $hostname = `/bin/hostname`;
chomp $hostname;
if (@MASTERHOSTS and not grep { /^$hostname$/ } @MASTERHOSTS) {
	print "You shouldn't commit to a repository copy.  Please commit to\n" .
	    (join " or ", @MASTERHOSTS) . " instead.\n";
	exit 1;
}


# XXX Check that user is operating under the correct effective group id.
my $gname = `/usr/bin/id -gn`;
chomp $gname;
if ($gname ne $REQUIRED_GROUP) {
	print "You are not running under group '$REQUIRED_GROUP', and are " .
	    "therefore\nnot allowed to commit to this repository.  Sorry.\n";
	exit 1;
}

#
# XXX Ensure the minimum version of cvs is installed.
#
# VERS=`/usr/bin/cvs -v | awk '$1 == "Concurrent" { print $5 }' | awk -F. '{printf "%d%02d%02d\n",$1,$2,$3}'`
# if [ "x${VERS}" = "x" -o ${VERS} -lt 10909 ]; then
#	echo "The wrong version of CVS is installed!" 1>&2
#	exit 1
# fi


#
# Does the access control list allow them commit access?
#
`$CVSROOT/CVSROOT/cvs_acls.pl @ARGV`;
if ($? >> 8) {
	print "Access control checks failed! (cvs_acls.pl)\n";
	exit 1;
}

# Last minute checks and preperations for log_accum.pl later.  This
# records the last directory in this commit so that log_accum knows when
# to finish coalescing commit messages and mail it.
#
`$CVSROOT/CVSROOT/commit_prep.pl @ARGV`;
if ($? >> 8) {
	print "Commit prechecks failed (commit_prep.pl)\n";
	exit 1;
}

##print "OK - fake exit\n";
##exit 1;
exit 0		# Lets do it!
#end
