"""Verify the example and exercise solution in a disposable copy."""
import os
import shutil
import subprocess
import tempfile
from pathlib import Path

ROOT = Path(__file__).resolve().parent
FILES = ['main.cpp', 'solution/main.cpp']
COMMANDS = [['g++', '-std=c++20', 'main.cpp', '-o', 'lesson'], ['./lesson'], ['g++', '-std=c++20', 'solution/main.cpp', '-o', 'answer'], ['./answer']]

with tempfile.TemporaryDirectory(prefix="appfabrik-learning-") as temporary:
    work = Path(temporary)
    for name in FILES:
        target = work / name
        target.parent.mkdir(parents=True, exist_ok=True)
        shutil.copyfile(ROOT / name, target)
    for command in COMMANDS:
        print("Running:", " ".join(command), flush=True)
        subprocess.run(command, cwd=work, check=True, timeout=900)
print("Example and solution checks completed.")
