# -*- 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.399-preview-stability-white-screen-guard"',text,count=1,flags=re.M)
marker='# BEGIN V249399_PREVIEW_STABILITY_WHITE_SCREEN_GUARD_DIAGNOSTIC'
if marker not in text:
 text += r'''

# BEGIN V249399_PREVIEW_STABILITY_WHITE_SCREEN_GUARD_DIAGNOSTIC
@app.get("/api/diagnostics/preview_stability_v249399")
def api_diagnostics_preview_stability_v249399():
    return {
        "ok": True,
        "version": "V249399-preview-stability-white-screen-guard",
        "preview_shell_before_media_decode": True,
        "large_media_auto_decode_guard": True,
        "android_file_chooser_return_guard": True,
        "android_renderer_recovery_source_included": True,
        "publication_file_preserved_when_visual_preview_fails": True,
        "database_deleted": False,
        "media_deleted": False,
    }
# END V249399_PREVIEW_STABILITY_WHITE_SCREEN_GUARD_DIAGNOSTIC
'''
relay.write_text(text,encoding='utf-8')
print('PATCH_SERVER_V249399=OK')
