#!/usr/bin/python
from argparse import *
import os
import glob
import platform
from datetime import date

today = date.today()

print("Today's date:", today)

parser = ArgumentParser()

parser.add_argument('--max_taylor_order', type=int, help='highest [4]'  ,default='4')
parser.add_argument('--max_weight_power', type=int, help='weight power [5]'   ,default='5')
parser.add_argument('--input', type=str, help='input file template'   ,default="_input_templates/merged.inputs")
parser.add_argument('--batch', type=str, help='batch file template'   ,default="_batch_templates/serial.batch")
parser.add_argument('--prefix', type=str, help='name of test["test"]',default="merged_3d")

args = parser.parse_args()
print(args)

homestr = os.getcwd();
print ("homedir = " + homestr)
neartopstr = homestr + "/_" +args.prefix
strtoday =str(today.month) + "_" + str(today.day) + "_" + str(today.year)


topstr = neartopstr +"_" + strtoday
print ("topstr = " + topstr)

if not os.path.exists(topstr):
    printstr = "making directory " + topstr
    print (printstr)
    os.mkdir(topstr)

print ("changing directory to " + topstr)
os.chdir(topstr);

submitstr = "submit."  + args.prefix + ".jobs"
r = open(submitstr,'w')
r.write('#/usr/bin/csh\n')

max_taylor = args.max_taylor_order
max_weight = args.max_weight_power
taylor_P = 1

submitstr = "submit."  + args.prefix + ".jobs"
subfile = open(submitstr,'w')
subfile.write('#/usr/bin/csh\n')
inputfilebase = os.path.basename(args.input)
batchfilebase = os.path.basename(args.batch)
executablename  = "../../../main.exe"

inputtemplatename = homestr + "/" + args.input
batchtemplatename = homestr + "/" + args.batch
while taylor_P <= max_taylor:
    weight_P = 1
    while weight_P <= max_weight:

        f_inputtemplate = open(inputtemplatename,'r')
        f_batchtemplate = open(batchtemplatename,'r')
        
        configstr = "_T" + str(taylor_P) + "__W" + str(weight_P)
        print (configstr);

        print ("changing directory to " + homestr)
        os.chdir(homestr);

        print ("changing directory to " + topstr)
        os.chdir(topstr);

        dir_str = configstr
        
        batchfilename = batchfilebase + str(weight_P) + str(taylor_P)
        commstr = "cd " + dir_str  + "; source " + batchfilename + "; cd .. \n"
        echostr = "echo \" " + commstr + " \" "
        subfile.write(echostr)
        subfile.write(commstr)

        if not os.path.exists(dir_str):
            printstr = "making directory " + dir_str
            print (printstr)
            os.mkdir(dir_str)
        
        print ("changing directory to " + dir_str)
        os.chdir(dir_str)

        inputfilename = inputfilebase + str(weight_P) + str(taylor_P)
        print ("processing input file " + inputfilename)
        f_inp = open(inputfilename,'w')
        
        for inputstr in f_inputtemplate:
            t1str = inputstr;
            t2str = t1str.replace("TAYLOR_MAX", str(taylor_P))
            t3str = t2str.replace("WGT_POWER",  str(weight_P))
            f_inp.write(t3str)
        
        f_inp.close()
        
        print ("processing batch file " + batchfilename)
        f_bat = open(batchfilename,'w')
        for batchstr in f_batchtemplate:
            t1str = batchstr
            t2str = t1str.replace("INPUT_FILE", inputfilename)
            t3str = t2str.replace("EXECUTABLE_FILE", executablename)
            f_bat.write(t3str)

        f_bat.close()
        f_inputtemplate.close()
        f_batchtemplate.close()
        
        weight_P +=  1
#       end inner scope        
    taylor_P += 1
#   end outer scope

print("out of loop")
subfile.close();

    
