#!/usr/bin/env python3
from pathlib import Path
import shutil,sys,re
root=Path(sys.argv[1]);front=root/'frontend';src=Path(__file__).resolve().parent
(front/'js').mkdir(parents=True,exist_ok=True)
for name in ['unified_account_v249386.js','creator_publication_v249388.js','professional_flow_v249397.js','unified_publish_scope_security_v249398.js','professional_isolation_v249413.js']:
 shutil.copy2(src/'frontend_bundle/js'/name,front/'js'/name)
shutil.copy2(src/'frontend_bundle/index.js',front/'index.js')
h=(src/'frontend_bundle/index.html').read_text(encoding='utf-8')
h=re.sub(r'\n?<script[^>]+professional_isolation_v249413\.js[^>]*></script>\s*','\n',h)
h=h.replace('</body>','<script src="/js/professional_isolation_v249413.js?v=249413-professional-isolation-no-preview" charset="utf-8" defer></script>\n</body>')
(front/'index.html').write_text(h,encoding='utf-8')
relay=root/'relay_server_clean.py';r=relay.read_text(encoding='utf-8');r=re.sub(r'(?m)^VERSION\s*=\s*["\'][^"\']+["\']','VERSION  = "2.4.9.413-professional-isolation-no-preview"',r,count=1)
if 'BEGIN V249413_PROFESSIONAL_ISOLATION_NO_PREVIEW' not in r:r += "\n# BEGIN V249413_PROFESSIONAL_ISOLATION_NO_PREVIEW\n@app.get('/api/diagnostics/v249413')\ndef api_diagnostics_v249413(room_id: str = Query('ithaki-phone-test')):\n    return {'ok': True, 'version': 'V249413-professional-isolation-no-preview', 'professional_preview': False, 'professional_editing': False, 'professional_contacts': False, 'normal_publish_plus_only': True, 'apk_required_for_this_fix': False}\n# END V249413_PROFESSIONAL_ISOLATION_NO_PREVIEW\n"
relay.write_text(r,encoding='utf-8')
assert h.count('professional_isolation_v249413.js')==1
print('PATCH_SERVER_V249413=OK')
