from pathlib import Path
import sys,re
s=Path(sys.argv[1]);f=s/'frontend'
# Frontend bundle has already been replaced. Patch the copied relay only.
rp=s/'relay_server_clean.py';r=rp.read_text(encoding='utf-8')
r=re.sub(r'(?m)^VERSION\s*=\s*["\'][^"\']+["\']','VERSION  = "2.4.9.418-simple-messages-feed-rooms-optional-encryption"',r,count=1)
if 'BEGIN V249418_SIMPLE_MESSAGES_FEED_ROOMS' not in r:
 r += """
# BEGIN V249418_SIMPLE_MESSAGES_FEED_ROOMS
@app.get('/api/diagnostics/v249418')
def api_diagnostics_v249418(room_id: str = Query('ithaki-phone-test')):
    return {'ok': True, 'version': 'V249418-simple-messages-feed-rooms-optional-encryption', 'normal_message_endpoint': '/api/messages/send', 'normal_message_endpoint_push_notify': True, 'secure_script_loaded': False, 'communication_flow_loaded': False, 'visible_encryption_locks': 1, 'encryption_optional': True, 'feed_simple_render': True, 'rooms_simple_render': True, 'apk_required_for_this_fix': False}
# END V249418_SIMPLE_MESSAGES_FEED_ROOMS
"""
rp.write_text(r,encoding='utf-8')
h=(f/'index.html').read_text(encoding='utf-8');i=(f/'index.js').read_text(encoding='utf-8');g=(f/'js/final_guard_v249418.js').read_text(encoding='utf-8')
assert h.count('final_guard_v249418.js')==1
assert 'secure_messaging_v249389.js' not in h
assert 'communication_flow_v249391.js' not in h
assert 'professional_security_v249396.js' not in h
assert 'V249418_SIMPLE_MESSAGES_NORMAL_ENDPOINT' in i
assert 'openEncryptionChoice418' in g
print('PATCH_SERVER_V249418=OK')
