#### Edit cafemol input file for path and md step
#### Usage: $ python3 edit_input.py input_file output_path pdb_path
#### given input file is edited in place.

import sys
import fileinput

if len( sys.argv ) < 5:
    print("provide cafemol_input_file, pdb_file, output_path, pdb_path")
    sys.exit()

input_file = sys.argv[1]
pdb_file = sys.argv[2]
out_path = sys.argv[3]
pdb_path = sys.argv[4]
out_file = "format.inp"
with open(input_file,'r') as fin, open(out_file, 'w') as fout :
    flag1,flag2 = 0,0
    for line in fin:
        if line.startswith("path "):
            line_string = "path = "+out_path
            fout.write(line_string+"\n")
        elif line.startswith("path_pdb"):
            line_string = "path_pdb = "+pdb_path
            fout.write(line_string+"\n")
        elif line.startswith("path_ini"):
            line_string = "path_ini = "+pdb_path
            fout.write(line_string+"\n")
        elif line.startswith("i_run_mode"):
            line_string = "i_run_mode = 2"
            fout.write(line_string+"\n")
        elif line.startswith("n_tstep(1)"):
            line_string = "n_tstep(1) = 1"
            fout.write(line_string+"\n")
        elif line.startswith("<<<< unit_and_state"):
            flag1 = 1
            fout.write(line)
        elif (flag1 == 1 and line.startswith("1-2")):
            line_string = "1-2    rna " + pdb_file
            fout.write(line_string+"\n")
            flag1 = 0
        elif line.startswith("<<<< initial_struct"):
            flag2 = 1
            fout.write(line)
        elif (flag2 == 1 and line.startswith("1-2")):
            line_string = "1-2 " + pdb_file
            fout.write(line_string+"\n")
            flag2 = 0
        else:
            fout.write(line)
