#!/usr/bin/env python3
"""The 1 September switch for the META 2027 submission CTAs.

Every submission CTA exists twice in the markup — a `cta-open` variant (Start a Submission,
linking to the OJS submission wizard) and a `cta-preopen` variant (Prepare Your Submission /
Create Your META Account, with the "opens September 1, 2026" note). "Sign In" carries neither
class and stays visible in both states — signing in is useful before and after the switch.
One CSS rule in meta27.css decides which of the two is visible, so opening day is one command,
not a hunt through fifteen files:

    python3 switch_cta.py open      # September 1, 2026
    python3 switch_cta.py preopen   # roll back if ever needed

The script flips the rule, bumps the stylesheet version in every page so no browser keeps the old
CSS, and prints the scp line to run. It refuses to run against a tree where the marker rule is
absent, and it is idempotent — running `open` twice changes nothing the second time.
"""

import glob
import re
import sys

MODE = sys.argv[1] if len(sys.argv) > 1 else ''
if MODE not in ('open', 'preopen'):
    sys.exit('usage: switch_cta.py open|preopen   (run inside the site directory)')

css = open('meta27.css', encoding='utf-8').read()
current = ('preopen' if '.cta-open{display:none' in css else
           'open' if '.cta-preopen{display:none' in css else None)
if current is None:
    sys.exit('meta27.css: CTA state rule not found — refusing to guess')
print('état actuel : %s' % current)

if current == MODE:
    print('déjà en état %s — rien à faire' % MODE)
    sys.exit(0)

if MODE == 'open':
    css = css.replace('.cta-open{display:none !important}',
                      '.cta-preopen{display:none !important}')
else:
    css = css.replace('.cta-preopen{display:none !important}',
                      '.cta-open{display:none !important}')
open('meta27.css', 'w', encoding='utf-8').write(css)

# The gold "next milestone" dot on the timeline has to move on the same day, or the page keeps
# pointing at an opening that has already happened. Once the call is open, the next thing an
# author must act on is the submission deadline.
NEXT_FOR = {'open': 'Submission Deadline', 'preopen': 'Call for Papers'}
dates = open('dates.html', encoding='utf-8').read()
if dates.count('class="step next"') != 1:
    sys.exit('dates.html: expected exactly one "next" marker — refusing to guess')
dates = dates.replace('class="step next"', 'class="step"')
target = '<div class="st">%s</div>' % NEXT_FOR[MODE]
if target not in dates:
    sys.exit('dates.html: cannot find the %r step' % NEXT_FOR[MODE])
head, sep, tail = dates.rpartition('<div class="step">' + '<span class="dot"></span>' + target)
if not sep:
    sys.exit('dates.html: the %r step is not in the expected shape' % NEXT_FOR[MODE])
dates = head + sep.replace('class="step"', 'class="step next"', 1) + tail
open('dates.html', 'w', encoding='utf-8').write(dates)
print('marqueur « prochaine échéance » déplacé sur : %s' % NEXT_FOR[MODE])

versions = re.findall(r'meta27\.css\?v=(\d+)', ' '.join(
    open(f, encoding='utf-8').read() for f in glob.glob('*.html')))
new_v = max(int(v) for v in versions) + 1
changed = 0
for f in glob.glob('*.html'):
    s = open(f, encoding='utf-8').read()
    s2 = re.sub(r'meta27\.css\?v=\d+', 'meta27.css?v=%d' % new_v, s)
    if s2 != s:
        open(f, 'w', encoding='utf-8').write(s2)
        changed += 1

print('bascule %s → %s ; version CSS v=%d sur %d pages' % (current, MODE, new_v, changed))
print('\nreste à déployer :')
print('  scp *.html meta27.css meta-legacy:/home/directev/public_html/META27/')
