from pathlib import Path
p=Path('/opt/struktur/asol-runtime/app.py')
s=p.read_text()
old="<button onclick='saveDraft()'>Speichern</button> <span id='saveStatus' class='meta'></span>"
new="<button onclick='saveDraft()'>Speichern</button> <button onclick='sendBuffer()'>An Buffer</button> <span id='saveStatus' class='meta'></span>"
if old not in s: raise SystemExit('button anchor not found')
s=s.replace(old,new,1)
oldjs="function saveDraft(){{fetch('/api/draft/save',{{method:'POST',headers:{{'Content-Type':'application/json'}},body:JSON.stringify({{signal_id:{item['signal_id']!r},platform:{item['platform']!r},body:document.getElementById('body').value}})}}).then(r=>r.json()).then(d=>document.getElementById('saveStatus').textContent=d.ok?'Gespeichert':'Fehler beim Speichern');}}"
newjs=oldjs+"function sendBuffer(){{fetch('/api/draft/buffer',{{method:'POST',headers:{{'Content-Type':'application/json'}},body:JSON.stringify({{signal_id:{item['signal_id']!r},platform:{item['platform']!r},body:document.getElementById('body').value}})}}).then(r=>r.json()).then(d=>document.getElementById('saveStatus').textContent=d.ok?'Buffer-Draft erstellt':('Buffer-Fehler: '+(d.error||'unbekannt')));}}"
if oldjs not in s: raise SystemExit('js anchor not found')
s=s.replace(oldjs,newjs,1)
p.write_text(s)
print('UI_PATCH_OK')