#! /usr/bin/env python
import sys,os
import subprocess
import commands

def removeDuplicates():
        # files are sorted!
        global rootfiles
        last=''
        lastfull=''
        cleaned=[]
        for file in rootfiles:
                ir=file.rfind('_')
                if ir<=3:
                        print 'Warning - file name not well formed: '+filename
                        cleaned.append(file)
                        continue
                comp=file[:ir-1]
                if comp.find('_') < 0:
                        print 'Warning - file name not well formed: '+filename
                        cleaned.append(file)
                        continue
                if comp==last:
                        killed=cleaned.pop()
                        print 'Duplicate warning: '+killed+' will be removed.'
                cleaned.append(file)
                last=comp
	delta=list(set(rootfiles)-set(cleaned))
        rootfiles=cleaned
	return delta

def checkIndir():
        """ check number of files in indir
            and return list of filenames
        """
        global indir
        prefix=''
        lscommand='ls'
        # check if dcache
        if indir.find('/store')==0:
                indir='/pnfs/desy.de/cms/tier2/'+indir
                lscommand='dcls'
        if indir.find('/pnfs')==0:
                lscommand='dcls'

        #full name
        if lscommand=='ls': indir = os.path.abspath(indir)
        # check for ending /
        if indir[-1]!='/': indir+='/'

        # list directory
        allfiles=subprocess.Popen([lscommand,indir], stdout=subprocess.PIPE).communicate()[0]
        if allfiles.find('No valid Proxy found')==0:
                print 'No valid Proxy found'
                exit(0)
        if allfiles.find('The following regular expressions')==0:
                print 'Cannot: '+lscommand+' '+indir
                exit(0)

        # add dcache gate
       # if lscommand=='dcls': indir=dcache_gate+indir

        # filter out *.root
        ret=[]
        for line in allfiles.split('\n'):
                if line.rfind('.root')>0 :ret.append(line)

        if len(ret)==0:
                print 'No files found in '+indir
                allfiles
                sys.exit(0)

        ret.sort()
        return ret


if __name__ == "__main__":

	if len(sys.argv) > 2 or  len(sys.argv) == 1:
		print "Usage: cleanDup directory"
		sys.exit(0)
	indir = sys.argv[1]	
	rootfiles = checkIndir()
	delta = removeDuplicates()
	for file in delta:
		retcode = subprocess.call(["dcdel",indir+file])
	if len(delta) == 0: print "No duplicates found"
