#!/usr/bin/env python3

from sys import argv, exit

if len(argv) != 4:
    print("Usage: %s [input xp file] [input DOT file] [output DOT file]" % argv[0])
    exit(1)

critical = []
nodecolor = []

with open(argv[1], 'r') as myfile:
    text = myfile.read()
splits = text.split('.')
exploits = len(splits) -1

for i in range(exploits):
    if "is_critical=true" in splits[i]:
            critical.append("label="+str(i))

with open(argv[2], 'r') as inDOT:
        DOTtext = inDOT.read()
dotsplits = DOTtext.split("\n")

for x in range(len(dotsplits)):
    if (any(ele in (dotsplits[x]) for ele in critical)):
        nodestr=dotsplits[x].rsplit(' ')[0]
        node=nodestr.split("->")[1]        
        if node not in nodecolor:
            nodecolor.append(node)
        
for x in range(len(dotsplits)):
    if ("label" not in dotsplits[x]):
        if (any(ele in (dotsplits[x]) for ele in nodecolor)):
            tmpstr=dotsplits[x].split(";")[0]
            newstr=tmpstr
            finalstr=newstr.replace(tmpstr, tmpstr+"[fillcolor = \"#FF2C2C\", style=filled, fontname=\"times-bold\"];")
            (dotsplits[x])=finalstr

sep="\n"
with open(argv[3], 'w') as outDOT:
       outDOT.write(sep.join(dotsplits))


