"""
Passenger entry point for cPanel (Application Manager / Passenger).
The venv is found via a `venv` symlink next to this file, which `deploy.sh`
creates on every deploy (pointing at the real cPanel venv). The symlink is
excluded from rsync, so deploys never remove it.
"""
import os
import sys

HERE = os.path.dirname(os.path.abspath(__file__))


if not os.environ.get("_PASSENGER_VENV_REEXEC"):
    for _candidate in (
        os.environ.get("VENV_PYTHON"),
        os.path.join(HERE, "venv", "bin", "python3"),
        os.path.join(HERE, "venv", "bin", "python"),
        os.path.join(HERE, ".venv", "bin", "python3"),
    ):
        if (
            _candidate
            and os.path.exists(_candidate)
            and os.path.realpath(_candidate) != os.path.realpath(sys.executable)
        ):
            os.environ["_PASSENGER_VENV_REEXEC"] = "1"
            os.execl(_candidate, _candidate, *sys.argv)

sys.path.insert(0, HERE)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")

from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()
