"""Replace "Autumn 2026" with the date, but only where the subject is the Call for Papers.

Fourteen occurrences across six files, and they are two different statements. Seven are about the
call opening — now dated 1 September 2026. Seven are about registration opening, which is a
separate commitment: it implies published fees and a live payment path, and nobody has said those
land the same day. Those are left alone.

attend.html is the awkward one and is left too: "Registration opens with the Call for Papers —
Autumn 2026" couples the two, so dating it would set a registration date by implication rather
than by decision.

Every replacement is keyed on the surrounding words, never on "Autumn 2026" alone, and the script
reports what it changed and refuses to run twice.
"""

import hashlib
import os
import re

os.chdir('/home/directev/public_html/META27')

REPLACEMENTS = [
    # (file, exact search, replacement)
    ('ethics.html', 'Call opens Autumn 2026', 'Call opens 1 September 2026'),
    ('faq.html', 'Call for Papers will open in Autumn 2026',
     'Call for Papers will open on 1 September 2026'),
    ('home.html', 'Call for Papers opens Autumn 2026', 'Call for Papers opens 1 September 2026'),
    ('submit.html', 'Call for Papers opens Autumn 2026', 'Call for Papers opens 1 September 2026'),
]

# The stepper markup carries the label and the date in separate elements, so it needs its own
# pattern: "Call for Papers" then, within a short span, "Autumn 2026".
STEP = re.compile(r'(Call for Papers</[a-z]+>\s*<[^>]+>\s*)Autumn 2026')

# submit.html wraps the date in <strong>, so the literal sentence never appears and the plain
# replacement above matched nothing there. Keyed on "Call for Papers opens" so it cannot reach the
# registration sentences.
STRONG = re.compile(r'(Call for Papers opens\s*<strong>)Autumn 2026')

apply = os.environ.get('APPLY') == '1'
before = {}
total = 0

for f in sorted({r[0] for r in REPLACEMENTS} | {'home.html', 'submit.html'}):
    s = open(f, encoding='utf-8').read()
    before[f] = hashlib.md5(s.encode()).hexdigest()
    n = 0
    for target, search, replace in REPLACEMENTS:
        if target != f:
            continue
        n += s.count(search)
        s = s.replace(search, replace)
    for pattern in (STEP, STRONG):
        n += len(pattern.findall(s))
        s = pattern.sub(lambda m: m.group(1) + '1 September 2026', s)

    remaining = s.count('Autumn 2026')
    print('%-18s %d remplacement(s), %d mention(s) restante(s)' % (f, n, remaining))
    total += n
    if apply and n:
        open(f, 'w', encoding='utf-8').write(s)

print('\ntotal : %d' % total)
if not apply:
    print('essai à blanc. APPLY=1 pour écrire.')
