#!/usr/bin/env python3
"""Usage: ./autoreload.py [MIRAGE_ARGUMENTS]...

Automatically rebuild and restart the application when source files change.
CONFIG+=dev will be passed to qmake, see moment.pro.
The application will be launched with `-name dev`, which sets the first
part of the WM_CLASS as returned by xprop on Linux.
Any other arguments will be passed to the app, see `moment --help`.

Use `pip3 install --user -U requirements-dev.txt` before running this."""

import os
import subprocess
import shutil
import sys
from contextlib import suppress
from pathlib import Path
from shutil import get_terminal_size as term_size

from watchfiles import DefaultFilter, run_process

ROOT = Path(__file__).parent


class Filter(DefaultFilter):
    def __call__(self, change, path):
        if not super().__call__(change, path):
            return False

        path = Path(path)

        for bad in ("src/config", "src/themes"):
            if path.is_relative_to(ROOT / bad):
                return False

        for good in ("src", "submodules"):
            if path.is_relative_to(ROOT / good):
                return True

        return False


def cmd(*parts) -> subprocess.CompletedProcess:
    return subprocess.run(parts, cwd=ROOT, check=True)


def run_app(args=sys.argv[1:]) -> None:
    print("\n\x1b[36m", "─" * term_size().columns, "\x1b[0m\n", sep="")

    if shutil.which("qmake-qt5"):
        QMAKE_CMD = "qmake-qt5"
    else:
        QMAKE_CMD = "qmake"

    with suppress(KeyboardInterrupt):
        cmd(QMAKE_CMD, "moment.pro", "CONFIG+=dev")
        cmd("make")
        cmd("./moment", "-name", "dev", *args)


if __name__ == "__main__":
    if len(sys.argv) > 2 and sys.argv[1] in ("-h", "--help"):
        print(__doc__)
    else:
        (ROOT / "Makefile").exists() and cmd("make", "clean")
        run_process(
            ROOT,
            target=run_app,
            callback=print,
            watch_filter=Filter(),
        )
