import sys
import os
import glob
import gc

fills = sys.argv[1:]

if len(fills) < 1:
    print 'give me the fill numbers as input!'
    sys.exit(-1)

disk = os.path.expandvars('$DISK') + '/data/ST/Aging'
scripts = os.path.expandvars('$CCEHOME') + '/macros/CCEScan'
pwd = os.getcwd()
haddcmd = os.path.expandvars('$ROOTSYS') + '/bin/hadd'
temp_dir = "/disk/data1/hep/elena/data/ST/Aging"

sectors = []
with open('%s/TTsectors.dat' % os.path.dirname(pwd), 'rb') as sectsfile:
    for line in sectsfile:
        sectors.append(str(line).strip())
 
with open('%s/ITsectors.dat' % os.path.dirname(pwd), 'rb') as sectsfile:
    for line in sectsfile:
        sectors.append(str(line).strip())

files_to_hadd = {}

def modtime(f):
    return os.path.getmtime(f)

flatten = lambda l: [item for sublist in l for item in sublist]

# Hadd
for sector in sectors:
    files_to_hadd[sector] = {}
    files_to_hadd[sector]['list'] = []
    files_to_hadd[sector]['target'] = disk + '/landauFit_%s.root' % sector
    for fill in fills:
        files = glob.glob('%s/temp*_%s_%s_*.root' % (disk, sector, fill))
        files_to_hadd[sector]['list'].append(files)
        #if modtime(f) > dec15 and 'temp_' in f and sector in f and '.root' in f:
        #    files_to_hadd[sector]['list'].append(f)
    files_to_hadd[sector]['list'] = flatten(files_to_hadd[sector]['list'])

gc.collect()
sector_files = []

for sector in sectors:
    haddstring = "%s -f %s %s\n" % (haddcmd, files_to_hadd[sector]['target'], " ".join(files_to_hadd[sector]['list']))
    with open('hadd-%s.sh' % sector, 'wb') as exefile:
        exefile.write(haddstring)
    os.system('bash hadd-%s.sh' % sector)
    gc.collect()
    os.system('rm hadd-%s.sh' % sector)
    sector_files.append(files_to_hadd[sector]['target'])

target = temp_dir + '/CCEScan_temp.root'
with open('haddAll.sh', 'wb') as exefile:
    exefile.write('%s -f %s %s\n' % (haddcmd, target, ' '.join(sector_files)))
    exefile.write('%s -f %s %s %s\n' % (haddcmd, temp_dir + '/CCEScan_temp_all.root', target, temp_dir + '/CCEScan.root'))
    exefile.write('cp %s %s\n' % (temp_dir + '/CCEScan.root', temp_dir + 'CCEScan_lastbk.root'))

gc.collect()
os.system('bash haddAll.sh')
os.system('rm haddAll.sh')
gc.collect()

# cleanup
clean = raw_input('do you want to clean up [yes] or to first go out and check?')
if not 'yes' in clean:
    print 'Exiting, then...'
    sys.exit(-1)

with open('cleanup.sh', 'wb') as cleanfile:
    for sector in sectors:
        for f in files_to_hadd[sector]['list']:
            cleanfile.write('rm %s\n' % f)
    for f in sector_files:
        cleanfile.write('rm %s\n' % f)
    cleanfile.write('rm %s\n' % (temp_dir + '/CCEScan.root'))
    cleanfile.write('mv %s %s\n' % (temp_dir + '/CCEScan_temp_all.root', temp_dir + '/CCEScan.root'))

os.system('bash cleanup.sh')
os.system('rm cleanup.sh')


# ll = {}
# for fill in fills:
#     ll[fill] = glob.glob('%s/temp*_%s_*.root' % (disk,fill))
# targets = []
# for fill in fills:
#     targets += ll[fill]
# 
# os.system('rm haddfile.sh')
# 
# with open('haddfile.sh', 'wb') as f:
#     f.write('hadd -f %s/CCEScan_temp.root %s\n' % (disk, ' '.join(targets)))
#     f.write('hadd -f %s/CCEScan_all.root %s/CCEScan.root /disk/data1/hep/elena/data/ST/Aging/CCEScan_temp.root\n' % (disk, disk))
#     f.write('mv %s/CCEScan.root %s/CCEScan_lastbk.root\n' % (disk, disk))
#     f.write('mv %s/CCEScan_all.root %s/CCEScan.root\n' % (disk, disk))
