#!/usr/bin/env python3
from pathlib import Path
import re
import sys

if len(sys.argv) != 2:
    raise SystemExit("usage: patch_index_v249387.py index.html")
path = Path(sys.argv[1])
html = path.read_text(encoding="utf-8", errors="strict")
html = re.sub(r"\s*<!-- V2493(?:7[4-9]|8[0-7])[^>]*-->\s*", "\n", html, flags=re.I)
patterns = (
 r'<script[^>]+src=["\'][^"\']*lexicon_v249374\.js[^"\']*["\'][^>]*>\s*</script>',
 r'<script[^>]+src=["\'][^"\']*experience_v24937[5-9]\.js[^"\']*["\'][^>]*>\s*</script>',
 r'<script[^>]+src=["\'][^"\']*feed_radical_v2493(?:6[7-9]|7[0-9]|8[0-7])\.js[^"\']*["\'][^>]*>\s*</script>',
 r'<script[^>]+src=["\'][^"\']*unified_account_v24938[2-7]\.js[^"\']*["\'][^>]*>\s*</script>',
 r'<script[^>]+src=["\'][^"\']*global_friend_gate_v24938[0-7]\.js[^"\']*["\'][^>]*>\s*</script>',
 r'<script[^>]+src=["\'][^"\']*creator_tools_v249383\.js[^"\']*["\'][^>]*>\s*</script>',
 r'<script[^>]+src=["\'][^"\']*v411_live_wall_v249383\.js[^"\']*["\'][^>]*>\s*</script>',
 r'<script[^>]+src=["\'][^"\']*creator_publication_v249387\.js[^"\']*["\'][^>]*>\s*</script>',
)
for pattern in patterns:
    html = re.sub(r"\s*" + pattern + r"\s*", "\n", html, flags=re.I)
html, changed = re.subn(
    r'(<script[^>]+src=["\'](?:\./|/)?index\.js)(?:\?[^"\']*)?(["\'][^>]*>\s*</script>)',
    r'\1?v=249387-creator-public-link-publication-workspace\2',
    html,
    count=1,
    flags=re.I,
)
if changed != 1:
    raise SystemExit('ERROR: index.js script tag not found exactly once')
block = '''
<!-- V249387_CREATOR_PUBLIC_LINK_PUBLICATION_WORKSPACE_FULL -->
<script src="/js/lexicon_v249374.js?v=249387-full" charset="utf-8" defer></script>
<script src="/js/experience_v249378.js?v=249387-full" charset="utf-8" defer></script>
<script src="/js/feed_radical_v249386.js?v=249387-full" charset="utf-8"></script>
<script src="/js/creator_tools_v249383.js?v=249387-full" charset="utf-8" defer></script>
<script src="/js/v411_live_wall_v249383.js?v=249387-full" charset="utf-8" defer></script>
<script src="/js/unified_account_v249386.js?v=249387-full" charset="utf-8" defer></script>
<script src="/js/global_friend_gate_v249386.js?v=249387-full" charset="utf-8" defer></script>
<script src="/js/creator_publication_v249387.js?v=249387-full" charset="utf-8" defer></script>
'''
pos = html.lower().rfind('</body>')
if pos < 0:
    raise SystemExit('ERROR: </body> not found')
html = html[:pos] + block + html[pos:]
meta = '<meta name="ok-ithaki-build" content="V249387_CREATOR_PUBLIC_LINK_PUBLICATION_WORKSPACE_FULL">'
if re.search(r'<meta\s+name=["\']ok-ithaki-build["\'][^>]*>', html, re.I):
    html = re.sub(r'<meta\s+name=["\']ok-ithaki-build["\'][^>]*>', meta, html, count=1, flags=re.I)
else:
    pos = html.lower().find('</head>')
    if pos < 0:
        raise SystemExit('ERROR: </head> not found')
    html = html[:pos] + meta + '\n' + html[pos:]
checks = {
 'V249387_CREATOR_PUBLIC_LINK_PUBLICATION_WORKSPACE_FULL': 2,
 'lexicon_v249374.js': 1,
 'experience_v249378.js': 1,
 'feed_radical_v249386.js': 1,
 'creator_tools_v249383.js': 1,
 'v411_live_wall_v249383.js': 1,
 'unified_account_v249386.js': 1,
 'global_friend_gate_v249386.js': 1,
 'creator_publication_v249387.js': 1,
}
for marker, expected in checks.items():
    actual = html.count(marker)
    if actual != expected:
        raise SystemExit(f'ERROR: {marker} count={actual}, expected={expected}')
if html.count('index.js?v=249387-creator-public-link-publication-workspace') != 1:
    raise SystemExit('ERROR: index cache marker')
tmp = path.with_suffix(path.suffix + '.v249387.tmp')
with tmp.open('w', encoding='utf-8') as handle:
    handle.write(html)
tmp.replace(path)
print('INDEX_HTML_PATCH=V249387')
print('CREATOR_PUBLIC_LINK_SCRIPT_COUNT=1')
print('PUBLICATION_WORKSPACE_SCRIPT_COUNT=1')
print('PROFESSIONAL_CODE_CONFIRMATION_SCRIPT_COUNT=1')
print('GLOBAL_FRIEND_GATE_SCRIPT_COUNT=1')
print('CREATOR_TOOLS_SCRIPT_COUNT=1')
print('V411_SCRIPT_COUNT=1')
