# -*- coding: utf-8 -*-

"""
This program inject the content of a (xml) file [3] into a json file [1] as a string replacing a tag [2]
The output file is written to the fourth argument [4]
"""

import sys

inputfile = sys.argv[1]
tag = sys.argv[2]
xmlfile = sys.argv[3]
outputfile = sys.argv[4]

# read the content of the injecting file into a string, trimming spaces and newline. double quotes are escaped.
sj = ''
with open(xmlfile) as ifile:
   line = ifile.readline()
   while line:
       sj += line.strip().replace('"', '\\"')
       line = ifile.readline()

# does the job
with open(outputfile, 'w') as ofile:
    with open(inputfile) as ifile:
       line = ifile.readline()
       while line:
           ofile.write (line.replace(tag, sj))
           line = ifile.readline()
