"""Write lib/XFM5050UV.pretty/Texas_HSOP-8-1EP_ThermalVias_1col.kicad_mod for U3 (LMR33640) on the 19 mm S2 board.

U3 sits on the bottom of S2 directly under L1 (XAL5050) on the top. KiCad's stock ThermalVias HSOP footprint
has two via columns at x = +/-0.65 mm and a copy of the exposed pad on the opposite copper layer; both would
land on L1's pads (inner edges at x = +/-1.07 mm). This copy drops the opposite-layer pad and puts the four
0.6/0.3 mm vias in a single column at x = 0, between L1's pads when U3 and L1 share a centre at 0 deg.
Text edit of the stock file (pcbnew.FootprintSave is broken in KiCad 10's Python).
    python make_hsop_fp.py <project dir>
"""
import os, re, sys, uuid

SRC = r"C:\Program Files\KiCad\10.0\share\kicad\footprints\Package_SO.pretty\Texas_HSOP-8-1EP_3.9x4.9mm_P1.27mm_ThermalVias.kicad_mod"
NAME = 'Texas_HSOP-8-1EP_ThermalVias_1col'
dst = os.path.join(sys.argv[1], 'lib', 'XFM5050UV.pretty', NAME + '.kicad_mod')
t = open(SRC, encoding='utf-8').read()
pad = re.compile(r'\n\t\(pad "9" (?:thru_hole circle|smd rect)\n(?:\t\t[^\n]*\n)*?\t\)')
removed = [m.group(0) for m in pad.finditer(t) if 'thru_hole' in m.group(0) or '(layers "B.Cu")' in m.group(0)]
for r in removed:
    t = t.replace(r, '', 1)
assert len(removed) == 9, len(removed)          # 8 vias + the bottom-layer copy of the exposed pad
vias = ''.join(f'\n\t(pad "9" thru_hole circle\n\t\t(at 0 {y})\n\t\t(size 0.6 0.6)\n\t\t(drill 0.3)\n\t\t(layers "*.Cu")'
               f'\n\t\t(remove_unused_layers no)\n\t\t(zone_connect 2)\n\t\t(uuid "{uuid.uuid4()}")\n\t)'
               for y in (-1.95, -0.65, 0.65, 1.95))
t = t.rstrip()
assert t.endswith(')')
t = t[:-1].rstrip() + vias + '\n)\n'
t = re.sub(r'\(footprint "[^"]+"', f'(footprint "{NAME}"', t, count=1)
t = re.sub(r'\(descr "', '(descr "Single via column at x=0 and no opposite-side pad (sits under an XAL5050); derived from '
           'KiCad Texas_HSOP-8-1EP_3.9x4.9mm_P1.27mm_ThermalVias. ', t, count=1)
open(dst, 'w', encoding='utf-8').write(t)
print('saved', dst)
