import subprocess
import sys
from pathlib import Path
import os
import re

output_folder_name ="/bulk_convert_out/"

droppedFile = sys.argv[1]
droppedName = Path(droppedFile).name
originalfiles = sys.orig_argv[2:]

#long path shenanigans 
joined = (' '.join(originalfiles))
re.sub (' +', ' ', joined)
listed = re.split(r'(.+?mkv)', joined)
listed = list(filter(None, listed))
listed = [i.lstrip() for i in listed]

print(droppedName)

#get path of script location and remove the tail
head_tail = os.path.split(sys.argv[0])
output_path = head_tail[0]


#skip first part of sys.argv since it points to script location
arguments = sys.argv[1:]

#check if output folder exists, otherwise create it
isExist = os.path.exists(output_path+output_folder_name)
if not isExist:
    os.makedirs(output_path+output_folder_name)

print("printing listed", listed)
edited_list = ['-i "' + s +'"' for s in listed]
print("printing edited list", edited_list)
joined_list =' '.join(map(str, edited_list))
print(joined_list)
for file in edited_list:

    #
    print("printng file", file)
    #args = 'ffmpeg.exe -i "{file}" -map 0:s -c:s copy -f matroska "{output_path}{output_folder_name}{basename}"'.format(file=file, output_path=output_path, output_folder_name=output_folder_name,  basename=basename)
    #print(args)
basename = os.path.basename(listed[0]) 
print(basename)
args = 'ffmpeg.exe {joined_list} -c copy -f matroska "{output_path}{output_folder_name}{basename}"'.format(joined_list=joined_list, output_path=output_path, output_folder_name=output_folder_name,  basename=basename)
print(args)
subprocess.run(args)
k=input("press close to exit") 