#!/usr/bin/perl -w

# $Id: conflict.pl,v 1.1 2000/06/05 12:09:55 martinto Exp $

use strict;

# In the argument list to this script is
# 1) the full path to the current source repository
# 2) the list of files being acted upon.

# This script will get run when someone is attempting to commit files.

my $debug = 0;
my $dir = shift @ARGV;

if ($debug) {
  open (FILE, ">/tmp/conflict.txt")
    or die "Cannot open /tmp/conflict.txt: $!\n";
}

# for each file in the file list, verify that there isn't another file in the
# directory that differs only by case.  Check both $dir and $dir/Attic.
my %conflict;
my $numbad = 0;                 # Number of conflicting files.
foreach my $subdir ("", "/Attic") {
  my $thisdir = $dir . $subdir;
  print FILE "Directory is $thisdir\n" if ($debug);

  if (opendir(DIR, "$thisdir")) {
    my @dirlist = grep !/^\.\.?$/, readdir DIR;
    closedir DIR;

    # List of files whose names conflict, each is a hash with the new name as
    # a key and the repository name as value.
    foreach my $file (@ARGV){
      print FILE "file is $file\n" if ($debug);
      foreach my $repfile (@dirlist){
        $repfile =~ s/,v$//;
        print FILE "\tRepository file is $repfile\n" if ($debug);
        next if ($file eq $repfile);

        if (lc($file) eq lc($repfile)){
          print FILE "$file conflicts with $repfile\n" if ($debug);
          $conflict{$file} = $repfile;
          $numbad++;
        }
      }
    }
  }
}

close FILE if ($debug);

if ($numbad) {
  my ($msg_files, $s, $nots, $isare);
  if ($numbad == 1) {
    $msg_files = "a file";
    $s = "";
    $nots = "s";
    $isare = "is";
  } else {
    $msg_files = "some files";
    $s = "s";
    $nots = "";
    $isare = "are";
  }
  print <<THEEND;

You have added $msg_files which already exist$nots in the repository, but
whose name$s differ$nots only in case.  CVS is unable to detect this when you
add the file as the repository file system is case sensitive.

In order to get out of this mess you need to:
  + Move your new file out of the way by renaming it.
  + Edit the file CVS\\Entries and remove the line
    starting /thefile.name/0/dummy timestamp/...
  + Move your file back giving it the name currently used in the repository,
    carefully observing the case.
  + Add the file using the new name and commit it.

Repository directory is: $dir

The following file$s $isare affected:

THEEND
  while (my($file, $repfile) = (each %conflict)) {
    print "    $file exists as $repfile\n";
  }
  exit 1;                       # Should stop the commit.
}

exit 0;
