import re

with open("boardloaf-he/boardloafhe.kicad_sch", "r") as f:
    sch = f.read()

# Let's find the specific block for SW3 at 101.346
# We can just use a regex to replace exactly the SW3 that has 101.346
# Something like: (property "Reference" "SW3"\n\t\t\t(at 101.346
# Or just find the UUID of the second SW3.
# Earlier I found UUID "235a4b2f-c283-4e25-bb87-375df8e95542" was for 101.346
# Wait, let's just replace `(reference "SW3")` and `"SW3"` inside that specific UUID block.
# Actually, the easiest way is to split by `(uuid ` and check.

blocks = sch.split('(uuid "')
for i in range(len(blocks)):
    if (
        '235a4b2f-c283-4e25-bb87-375df8e95542")\n\t\t(property "Reference" "SW3"'
        in blocks[i]
    ):
        blocks[i] = blocks[i].replace('"SW3"', '"SW4"')
    if (
        'eb37ad46-a79d-4079-8785-b78785a629ef")\n\t\t)\n\t\t(instances\n\t\t\t(project "boardloafhe"\n\t\t\t\t(path "/3a093344-253b-4353-a388-64989d9a245c"\n\t\t\t\t\t(reference "SW3")'
        in blocks[i]
    ):
        blocks[i] = blocks[i].replace('"SW3"', '"SW4"')

sch = '(uuid "'.join(blocks)

# Also fix the +3V3 to VCC
sch = sch.replace("power:+3V3", "power:VCC")
sch = sch.replace('"+3V3"', '"VCC"')
sch = sch.replace('"+3V3_0_1"', '"VCC_0_1"')
sch = sch.replace('"+3V3_1_1"', '"VCC_1_1"')

with open("boardloaf-he/boardloafhe.kicad_sch", "w") as f:
    f.write(sch)
