import os
import sys
import logging
import fcntl

from .config import init_config

# When running under gunicorn/uWSGI, sys.argv usually belongs to the process
# manager, not to madblog. Prefer environment variables for configuration.
env_blog_dir = os.environ.get("MADBLOG_CONTENT_DIR")
env_config_file = os.environ.get("MADBLOG_CONFIG")

opts = None
blog_dir = env_blog_dir
config_file = env_config_file

if not blog_dir or not config_file:
    # Under gunicorn/uWSGI, sys.argv contains the process manager's arguments.
    # --config in particular conflicts with gunicorn's own --config flag, so we
    # only inspect positional args (potential blog dir) from the raw argv, and
    # skip argparse entirely to avoid misinterpreting process-manager flags.
    for arg in reversed(sys.argv[1:]):
        if not arg.startswith("-") and os.path.isdir(arg):
            blog_dir = blog_dir or arg
            break

if not config_file:
    blog_dir = blog_dir or "."
    config_file = os.path.join(blog_dir, "config.yaml")

config = init_config(config_file=config_file, args=opts)

logging.basicConfig(
    level=logging.DEBUG if getattr(config, "debug", False) else logging.INFO,
    format="%(levelname)s: %(message)s",
)

from .state import ensure_state_directory

ensure_state_directory()

from .app import app

# For gunicorn/uWSGI compatibility
application = app

_monitor_lock_f = None
lock_path = os.path.join(
    getattr(config, "content_dir", "."), ".madblog-content-monitor.lock"
)


def _start_monitor_once() -> None:
    global _monitor_lock_f

    try:
        _monitor_lock_f = open(lock_path, "w")
        fcntl.flock(_monitor_lock_f, fcntl.LOCK_EX | fcntl.LOCK_NB)
    except Exception:
        return

    try:
        app.start()
    except Exception:
        logging.getLogger(__name__).exception("Failed to start content monitor")


try:
    import uwsgi  # type: ignore

    @uwsgi.postfork
    def _uwsgi_postfork():
        # Only start the monitor in one worker to avoid duplicated watchers.
        # (If you want one monitor per worker, remove this guard.)
        if getattr(uwsgi, "worker_id", lambda: 0)() != 1:
            return
        _start_monitor_once()

except Exception:
    # Not running under uWSGI - start when imported (e.g. gunicorn workers).
    _start_monitor_once()


# vim:sw=4:ts=4:et:
