import subprocess
import os
import tempfile
import shutil
from pathlib import Path

source_dir = Path("drawings")

files = list((source_dir / "apps").iterdir()) + list((source_dir / "categories").iterdir()) + list((source_dir / "places").iterdir()) + list((source_dir / "status").iterdir())
sizes = [64, 48, 32, 24, 16]

build_dir = Path("build")

if build_dir.exists():
    shutil.rmtree(build_dir)

build_dir.mkdir()

# Copy the theme file
shutil.copy(Path("index.theme"), build_dir)

for size in sizes:
    (build_dir / f"{size}x{size}").mkdir()

for file in files:
    with tempfile.TemporaryDirectory() as scratch:
        subprocess.run(["inkscape", "--export-page=all", "-Tl", "-o", str(Path(scratch) / "page.svg"), str(file)])
        # Temporary names will be used; move them
        icon_name = file.name
        category_name = file.parent.name
        print("SCRATCH CONTENTS:", list(Path(scratch).iterdir()), file)

        for i, size in enumerate(sizes, 1):
            (build_dir / f"{size}x{size}" / category_name).mkdir(parents=True, exist_ok=True)
            shutil.move(Path(scratch) / f"page_p{i}.svg", build_dir / f"{size}x{size}" / category_name / icon_name)

