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) > 12:
			sortedList.append(fname)
			
	sortedList.sort()
	#sortedList.reverse()
	args = ['../sox']
	for filename in sortedList:
		print(filename)
		args.append(filename)
		print(args)
	 
	outputfile = "../recording.wav"
	args.append(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")

