import time
import math
import datetime
import cmd
import sys
import csv
import os
import termios, fcntl, struct
import textwrap
import shlex, subprocess

def combinefiles():
	os.chdir(os.path.dirname(sys.argv[0]))
	os.chdir("recordings/")
	print("combining files")
	dirList=os.listdir("../recordings")
	dirList.reverse()
	sortedList = []
	for fname in dirList:
		if len(fname) > 9:
			fname = fname[:0] + fname[1:]
			sortedList.append(fname)
			
	sortedList.sort()
	for filename in sortedList:
		print(filename)
		
	for i in range(0,len(sortedList),2):
		print(i, sortedList[i], sortedList[i+1])
		file1 = "L"+sortedList[i]
		file2 = "R"+sortedList[i+1]
		outputfile = "merged/" + sortedList[i]
		args = ['../sox', '-m', file1, file2, outputfile]
		print(args)
		p = subprocess.Popen(args) # Success!
	


if __name__ == '__main__':

    print(("\n"*20)+"    \033[91mhello JAS!\033[0m")
    
    try:
        combinefiles()
    except KeyboardInterrupt:
       try:
           sys.exit()
       except:
            print("\n\n    goodbye :)\n")

