#!/usr/bin/env python3

import os.path
import subprocess
import sys


def run(cmd: list[str]) -> None:
    print(" ".join(cmd), file=sys.stderr)
    subprocess.run(cmd, check=True)

SLIDES_FILE = "Kaninchenlöcher.odp"
PDF_FILE = f"{SLIDES_FILE.removesuffix('.odp')}.pdf"
WEB_ROOT = "/content/www/dj3ei.famsik.de/2025-Kaninchenlöcher-Afu"

if os.path.exists(f".~lock.{SLIDES_FILE}#"):
    raise RuntimeError("You're still editing. Close libreoffice!")

run(["git", "commit", "-am", "Automatic checkin prior to push"])

push_us = [
    "index.html",
    "main.css",
    "spectrum.py",
    "spectrum.svg",
    "dodo.py",
    "requirements.txt",
    "requirements-frozen.txt",
    "publish",
    "mk_qr_code",
    ".htaccess",
    SLIDES_FILE
]

if not(os.path.exists(PDF_FILE)) or \
   os.path.getmtime(PDF_FILE) <= os.path.getmtime(SLIDES_FILE):
    run(["ssh", "andreas@ara.famsik.de", "rm", "-f", f"{WEB_ROOT}/{PDF_FILE}"])
else:
    push_us.append(PDF_FILE)

run(["rsync", "-zt", "--info=PROGRESS2", *push_us, f"andreas@ara.famsik.de:{WEB_ROOT}"])
