# -*- coding: utf-8 -*-
"""r379 附带：执行第八.1 已授权合并 ucla-masds ↔ ucla-applied-stats-ds-ms。
新 masds 页已按官方 44 units / GRE strongly recommended / March 1 + July 1 重建并通过密度 A(11)，
旧的 ucla-applied-stats-ds-ms 条目数据与官网不符（36 学分、TOEFL 100、GRE required、ddl 2026-12-15），
保留 ucla-masds，删重复块 + VD 条目 + 旧详情页文件。"""
import io, os, re, shutil

P = 'index_main.html'
if not os.path.exists('index_main.html.bak_r379_merge'):
    shutil.copyfile(P, 'index_main.html.bak_r379_merge')
t = io.open(P, encoding='utf-8').read()

# 1) PROGRAMS block
pid = 'ucla-applied-stats-ds-ms'
i = t.find('{id:"%s"' % pid)
assert i > 0, 'id not found'
nxt = t.find('{id:"', i + 6)
assert nxt > i
# include preceding comma + whitespace
j = i - 1
while j > 0 and t[j] in ' \n\r\t':
    j -= 1
assert t[j] == ',', 'no preceding comma'
block = t[j:nxt]
assert 'url:"details/ucla-applied-stats-ds-ms.html"' in block
assert block.count('{id:"') == 1, 'multi-block'
t2 = t[:j] + t[nxt:]

# 2) VD entry
vd = '"details/ucla-applied-stats-ds-ms.html",'
assert t2.count(vd) == 1, 'vd entry count %d' % t2.count(vd)
t2 = t2.replace(vd, '')

io.open(P, 'w', encoding='utf-8').write(t2)
print('removed block chars', len(t) - len(t2), 'bytes', os.stat(P).st_size)
print('remaining id refs', t2.count(pid), 'url refs', t2.count('ucla-applied-stats-ds-ms.html'))

f = 'details/ucla-applied-stats-ds-ms.html'
if os.path.exists(f):
    os.remove(f)
    print('removed file', f)
