"""
Control the OpenBSD packet filter (PF).

:codeauthor: Jasper Lievisse Adriaanse <j@jasper.la>

.. versionadded:: 2019.2.0
"""


import logging
import re

import salt.utils.path
from salt.exceptions import CommandExecutionError, SaltInvocationError

log = logging.getLogger(__name__)


def __virtual__():
    """
    Only works on OpenBSD and FreeBSD for now; other systems with pf (macOS,
    FreeBSD, etc) need to be tested before enabling them.
    """
    tested_oses = ["FreeBSD", "OpenBSD"]
    if __grains__["os"] in tested_oses and salt.utils.path.which("pfctl"):
        return True

    return (
        False,
        "The pf execution module cannot be loaded: either the OS ({}) is not "
        "tested or the pfctl binary was not found".format(__grains__["os"]),
    )


def enable():
    """
    Enable the Packet Filter.

    CLI Example:

    .. code-block:: bash

        salt '*' pf.enable
    """
    ret = {}
    result = __salt__["cmd.run_all"](
        "pfctl -e", output_loglevel="trace", python_shell=False
    )

    if result["retcode"] == 0: