import re
import argparse

# Copyright (c) Efabless Corporation. All rights reserved.
# See LICENSE file in the project root for full license information.

#for i in range(len(sys.argv)):

parser = argparse.ArgumentParser(
    description='initiates the li1 hack')

parser.add_argument('--defFile', '-d',required=True,
                    help='Input DEF')

parser.add_argument('--viosFile', '-v', required=True,
                    help='vios.txt containing white space separated cells that cause antenna violations')

parser.add_argument('--fakeDiode', '-f', required=True,
                    help='the name of the fake diode')
parser.add_argument('--trueDiode', '-t', required=True,
                    help='the name of the true diode')

args = parser.parse_args()

viosFile    =   args.viosFile
defFile     =   args.defFile

fakeDiode   =   args.fakeDiode
trueDiode   =   args.trueDiode




tmpFile = open(viosFile,"r")
if tmpFile.mode == 'r':
    listOfVios =tmpFile.read().split()
tmpFile.close()

tmpFile = open(defFile,"r")
if tmpFile.mode == 'r':
    defContent = tmpFile.read().split("\n")
tmpFile.close()

exitFlag = False
for i in range(len(defContent)):
    if defContent[i].find("COMPONENTS") != -1:
        if exitFlag == True:
            break
        else:
            exitFlag = True
            continue

    antennaPos = defContent[i].find("ANTENNA_")
    if antennaPos != -1:
        cell = re.findall(r'- ANTENNA_(\S+)_.* '+fakeDiode+'.*', defContent[i])
        if len(cell) >= 1:
            if cell[0] in listOfVios:
                defContent[i] = defContent[i].replace(fakeDiode, trueDiode)
                
tmpFile = open(defFile,"w")
tmpFile.write("\n".join(defContent))
tmpFile.close()



