# -*- coding: utf-8 -*-

import codecs, os, cfg

def outcount():
    i = str(int(open('../0out/.counter').read())+1)
    open('../0out/.counter','w').write( i )
    return '../0out/%s.out' % i

for ea in cfg.ll():
    lst = [x for x in os.listdir('../%s' % ea) if 'new' in x]
    for fmsg in lst:
        new = codecs.open('../%s/%s' % (ea,fmsg),'r','utf-8').read().splitlines()
        header = new[:new.index('')]
        if len(header) == 2:
            buf = [ea] + new
        elif len(header) == 3:
            buf = [ea] + new[1:4] + ['@repto:%s' % new[0]] + new[4:]
        codecs.open(outcount(),'w','utf-8').write( '\n'.join(buf) )
        os.remove('../%s/%s' % (ea,fmsg))
