#Python file: to run, type python launch_multi.py "name of default sub_file" "first seed" "# of replicas" "name of private queue" (optional)

# The top half on its own will generate a load of folders reader for the "array" submission to the cluster. The bottom half (which I deleted) does 
#the actual submission, if you have a well-defined dummy sub file present. If not, just use a placeholder word for the sub_file name  when launching this program. 

#The code is expecting to see a file called "input" in the local directory which could be successfully used to launch oxDNA from within that directory 
#(all references to setup files within input are local, not global - except for the seq_dep file). input should refer to existent and well-defined wfile, top, op, etc. as appropriate 

import sys
import os



#inputs
subfile= sys.argv[1]
subfile_trimmed=subfile.strip("sh")
subfile_trimmed=subfile_trimmed.strip(".")
first_seed=int(sys.argv[2])
replicas=int(sys.argv[3])
queue=""
if len(sys.argv)>4:
    queue=sys.argv[4]
current_dir = os.getcwd()


#make the directories for each simulation, checking if they already exist
#then update the generic input file into the specific input file with seed # etc
#then copy the setup files across to the right folder (they need to be in the working folder currently, and correctly referenced by the generic input  
#then update the generic input file the sub file for the array. Generic sub file must have correct executable address. 

##code below creates directories.
error=0
for i in range(replicas):
    seed= first_seed+i+1
    folder=current_dir+"/" +str(first_seed)+ "_" + str(i+1)
    if os.path.exists(folder):
        print "error: " + folder + "exists \n"
        error=1
    else:
        print(folder)
        os.makedirs(folder)
        input_file="input"+ "_" + str(i+1)
        f = open('input','r')
        f2=open(input_file,'w')
        temp=f.readline()
        while temp:
            test=temp.split()
            if len(test):
                if(test[0]=="seed"):
                    f2.write("seed = " + str(seed) + "\n")
                elif test[0]== "topology":
                    f2.write("topology = " + folder + "/"+  test[2] +"\n")
                    copy_command = "cp " + test[2] + " " + folder + " \n"
                    os.system(copy_command)
                elif test[0]== "conf_file":
                    f2.write("conf_file = " + folder + "/"+  test[2] +"\n")
                    copy_command = "cp " + test[2] + " " + folder + " \n"
                    os.system(copy_command)
                elif test[0]== "weights_file":
                    f2.write("weights_file = " + folder + "/"+  test[2]+ "\n")
                    copy_command = "cp " + test[2] + " " + folder + " \n"
                    os.system(copy_command)
                elif test[0]== "op_file":
                    f2.write("op_file = " + folder + "/"+  test[2] +"\n")
                    copy_command = "cp " + test[2] + " " + folder + " \n"
                    os.system(copy_command)
                else:
                    f2.write(temp)
            else:
                f2.write(temp)
            temp=f.readline()
        
        f.close()
        f2.close()
        copy_command = "cp " + input_file + " " + folder + " \n"
        os.system(copy_command)
 
        


    

