#!/usr/bin/env python
# usage: txtmerge-all.py <input_dir> <output_file_name> 
# does the exact opposite of txtsplit.py

import os
import sys
import re
import io
import glob
import shutil

assert len(sys.argv) == 3, "usage:\ntxtmerge-all.py <input_dir> <output_file_name>"
idir = str(sys.argv[1])
oname = str(sys.argv[2])
startLocationPath = os.path.join(idir, 'start.qsrc')

with io.open(oname, 'w', encoding='utf-16', newline='\r\n') as ofile:
    with io.open(startLocationPath, 'rt', encoding='utf-8') as ifile:
        shutil.copyfileobj(ifile, ofile)
    for location in glob.glob(os.path.join(idir, '*.qsrc')):
        if location != startLocationPath:
            with io.open(location, 'rt', encoding='utf-8') as ifile:
                shutil.copyfileobj(ifile, ofile)
    from datetime import date
    from sys import version_info
    ofile.write(u"#addbuilddate" + '\n')
    today = date.today()
    if version_info.major == 2:
        du = unicode(today.strftime("$builddate = '%B %d, %Y'"), 'utf8') + '\n'
    elif version_info.major == 3:
        du = str(today.strftime("$builddate = '%B %d, %Y'")) + '\n'
    ofile.write(du)
    ofile.write(u"--- addbuilddate ---------------------------------" + '\n')
