#!/usr/bin/env python3
"""Read the local .env and emit the env-vars JSON for CloudLinux Python Selector.

Only the variables that must live in the Passenger process environment are
included here: MySQL credentials and ``FLASK_SECRET_KEY``.  Everything else is
seeded into the ``config_vars`` table by ``seed_config.py``.

Usage::

    python3 scripts/selector_env.py --env .env
"""

from __future__ import annotations

import argparse
import json
from pathlib import Path

from common import load_env

_SELECTOR_KEYS = {
    "MYSQL_HOST",
    "MYSQL_USER",
    "MYSQL_PASSWORD",
    "MYSQL_DB",
    "FLASK_SECRET_KEY",
    "AUTO_START_CORE",
}


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument("--env", default=".env", help="Path to .env file")
    args = parser.parse_args(argv)

    env = load_env(Path(args.env))
    out = {k: env[k] for k in _SELECTOR_KEYS if env.get(k)}
    if not out:
        raise SystemExit("No selector env vars found in .env")

    # Compact single-line JSON so it is easy to embed in a shell command.
    print(json.dumps(out, separators=(",", ":"), ensure_ascii=True))
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
