from configparser import ConfigParser
from defence360agent.utils import is_cloudways

# This code is a ported copy-paste from Go agent.


def _is_nginx_server(path: str, raise_err: bool) -> bool:
    try:
        cfg = ConfigParser()
        cfg.read(path)
        return cfg.get("web_server", "server_type", fallback="") == "nginx"
    except Exception:
        if raise_err:
            raise
    return False


def is_force_use_coraza(
    *,
    path: str = "/etc/sysconfig/imunify360/integration.conf",
    raise_err: bool = False,
    detect_cloudways: bool = True,
) -> bool:
    if not _is_nginx_server(path, raise_err):
        return False
    if detect_cloudways:
        return is_cloudways()
    return True
