#!/usr/bin/env python
"""
Recursively checks that no filenames from the current working directory 
down are the same, even if they are in different subdirectories. Exits 
with status 0 if no matching filenames are found, or 1 if a match is 
found.

2020-03-25 PMS Copied originally from https://gist.github.com/seanh/229455 
Modified to lowercase everything before checking, to detect clashes caused
by capitalisation, and to capture full path for all clashing files
"""
import sys,os
print "Checking for filename clashes...",

def sortSecond(val): 
    return val[1] 

allfilenames = []
detect = []
clashes = []
report = []
exclude = set([".git",".hg"])
for root, dirs, files in os.walk('.', topdown=True):
    dirs[:] = [d for d in dirs if d not in exclude] # modifies dirs in-place.
    for filename in files:
        if filename.endswith('~'):
            # Ignore automatically created backup files.
            continue
        if os.path.islink(os.path.join(root,filename)):
            # Don't count symlinks as filename clashes.
            continue
        allfilenames.append((root,filename))
        if filename.lower() in detect:
            clashes.append(filename.lower())
        else:
            detect.append(filename.lower())

print len(allfilenames), 'files found...',
if clashes == []:
    print 'OK'
    sys.exit(0)
else:
    print len(clashes)," clash(es) found."
    for r,f in allfilenames:
        if f in clashes:
            report.append((r,f))
    # sorts the array in descending according to 
    # second element: the clashing filename
    report.sort(key = sortSecond) 
    for r,f in report:
        print os.path.join(r,f)
    print len(clashes)," clash(es) found."
    sys.exit(1)