#!/usr/bin/perl -w
#
# $FreeBSD$

use strict;
use lib $ENV{CVSROOT};
use CVSROOT::cfg;
my $CVSROOT = $ENV{'CVSROOT'} || die "Can't determine \$CVSROOT!";
my $debug = $cfg::DEBUG;

my $BASE_FN       = "$cfg::TMPDIR/$cfg::FILE_PREFIX";
my $FILES_FILE = "$BASE_FN.files";

print "$$: pgrp: ", getpgrp(), "\n" if ($debug);
print "$$: committer: ", $cfg::COMMITTER, "\n" if ($debug);

foreach (@ARGV) {
	print "$$: args: $_\n" if ($debug);
}

my $logfile = $ARGV[0];
my $log = "";
if (-r $logfile) {
	open(H, "<$logfile") or die;
	foreach (<H>) {
		print "$$: log: $_" if ($debug);
		$log .= $_;
	}
	close(H);
}

if (-r $FILES_FILE and open(FILES, $FILES_FILE)) {
	my %tag;
	while (<FILES>) {
		chomp;
		my ($tag, $file) = split(/\t/, $_, 2);
		print "$$: files_file: tag=$tag, file=$file\n" if ($debug);
		push @{$tag{$tag}}, $file;
	}
	close(FILES);

	if (check_approvers($log, %tag)) {
		unlink $FILES_FILE;
		exit 1;
	}
}
exit 0;

# ============================================================
sub read_approvers {
	my @Approvers;
	my $approvers = "$CVSROOT/CVSROOT/approvers";
	if (-r $approvers) {
		print "$$: Read $approvers\n" if ($debug);
		open(APP, "<$approvers") or return;
		while (<APP>) {
			chomp;
			next if (/^#/);
			my ($mod, $tag, $rev) = split(/\t+/, $_, 3);
			if ($rev) {
				push @Approvers, { mod => $mod,
						   tag => $tag,
						   app => $rev };
			}
		}
		close(APP);
	}
	@Approvers;
}

sub check_approvers {
	my ($log, %files) = @_;
	my %tag;
	my @Approvers = &read_approvers();
	foreach my $t (keys %files) {
		foreach (@{$files{$t}}) {
			$tag{$t}->{$_}++;
		}
	}
	foreach my $r (@Approvers) {
		printf "$$: checking: %s, %s, %s\n",
			$r->{mod}, $r->{tag}, $r->{app} if ($debug);
		foreach my $tag (sort keys %tag) {
			foreach my $file (sort keys %{$tag{$tag}}) {
				return 1 if (check_one($log, $r, $tag, $file));
			}
		}
	}
	0;
}

sub check_one {
	my ($log, $r, $tag, $file) = @_;
	if ($file !~ m|$r->{mod}|) {
		printf "$$: file not matched (%s, %s)\n", $file, $r->{mod}
			if ($debug);
		return 0;
	}
	printf "$$: file matched (%s, %s)\n", $file, $r->{mod} if ($debug);
	if ($tag !~ m|$r->{tag}|) {
		printf "$$: tag not matched (%s, %s)\n",
			$tag, $r->{tag} if ($debug);
		return 0;
	}
	printf "$$: tag matched (%s, %s)\n", $tag, $r->{tag} if ($debug);
	if ($log =~ m|Approved by:[\t ]*$r->{app}|s) {
		printf "$$: log matched (%s, %s)\n", $log, $r->{app}
			if ($debug);
		return 0;
	}
	printf "$$: log not matched (%s, %s)\n", $log, $r->{app} if ($debug);
	printf "**** You need \"Approved by: %s\" line in your log entry.\n",
		$r->{app};
	return 1;
}