#!/usr/bin/env python

import multiprocessing
import subprocess
from pathlib import Path


def main():
    p = subprocess.run(
        ["git", "rev-parse", "--show-toplevel"],
        capture_output=True,
        text=True,
        check=True,
    )
    root = Path(p.stdout.strip())
    project = root.name

    database = f"{project}_template"
    subprocess.run(["dropdb", "--force", "--if-exists", database], check=True)
    subprocess.run(["createdb", database], check=True)
    subprocess.run(
        [
            "pg_restore",
            "--dbname",
            database,
            "--exit-on-error",
            "--jobs",
            f"{multiprocessing.cpu_count()}",
            "--no-owner",
            "--verbose",
            Path.home().joinpath("dumps", f"{project}-scrubbed.dump"),
        ],
        check=True,
    )
    subprocess.run(["vacuumdb", "--analyze", database], check=True)


if __name__ == "__main__":
    main()
