# -*- coding: utf-8 -*-
from pathlib import Path
import re, sys
server = Path(sys.argv[1]).resolve()
relay = server / 'relay_server_clean.py'
if not relay.is_file(): raise SystemExit('relay_server_clean.py not found')
text = relay.read_text(encoding='utf-8')
text = re.sub(r'^VERSION\s*=\s*["\'][^"\']+["\']', 'VERSION  = "2.4.9.401-immediate-file-preview"', text, count=1, flags=re.M)
marker = '# BEGIN V249401_IMMEDIATE_FILE_PREVIEW'
if marker not in text:
    text += r'''

# BEGIN V249401_IMMEDIATE_FILE_PREVIEW
@app.get("/api/diagnostics/file_preview_v249401")
def api_diagnostics_file_preview_v249401():
    return {
        "ok": True,
        "version": "V249401-immediate-file-preview",
        "file_selection_immediate_preview": True,
        "workspace_onchange_owns_preview": True,
        "competing_workspace_change_handlers": 0,
        "apk_required_for_this_fix": False,
        "database_deleted": False,
        "media_deleted": False,
        "friendship_v249400_preserved": True,
    }
# END V249401_IMMEDIATE_FILE_PREVIEW
'''
relay.write_text(text, encoding='utf-8')
print('PATCH_SERVER_V249401=OK')
