import logging
import os

import salt.utils.data
import salt.utils.files
import salt.utils.platform
import salt.utils.yaml

__proxyenabled__ = ["*"]
log = logging.getLogger(__name__)


def shell():
    """
    Return the default shell to use on this system
    """
    # Provides:
    #   shell
    if salt.utils.platform.is_windows():
        env_var = "COMSPEC"
        default = r"C:\Windows\system32\cmd.exe"
    else:
        env_var = "SHELL"
        default = "/bin/sh"

    return {"shell": os.environ.get(env_var, default)}


def config():
    """
    Return the grains set in the grains file
    """
    if "conf_file" not in __opts__:
        return {}
    if os.path.isdir(__opts__["conf_file"]):
        if salt.utils.platform.is_proxy():
            gfn = os.path.join(
                __opts__["conf_file"], "proxy.d", __opts__["id"], "grains"
            )
        else:
            gfn = os.path.join(__opts__["conf_file"], "grains")
    else:
        if salt.utils.platform.is_proxy():
            gfn = os.path.join(
                os.path.dirname(__opts__["conf_file"]),
                "proxy.d",
                __opts__["id"],
                "grains",
            )
        else:
            gfn = os.path.join(os.path.dirname(__opts__["conf_file"]), "grains")
    if os.path.isfile(gfn):
        log.debug("Loading static grains from %s", gfn)
        with salt.utils.files.fopen(gfn, "rb") as fp_:
            try:
                return salt.utils.data.decode(salt.utils.yaml.safe_load(fp_))
            except Exception:  # pylint: disable=broad-except
                log.warning("Bad syntax in grains file! Skipping.")
                return {}
    return {}

@@ -1,12 +1,24 @@
+import glob%0A
 import loggi
@@ -31,16 +31,34 @@
 ort os%0A%0A
+import salt.utils%0A
 import s
@@ -96,16 +96,39 @@
 s.files%0A
+import salt.utils.path%0A
 import s
@@ -1724,20 +1724,1109 @@
 rn %7B%7D%0A    return %7B%7D%0A
+%0A%0Adef __secure_boot(efivars_dir):%0A    %22%22%22Detect if secure-boot is enabled.%22%22%22%0A    enabled = False%0A    sboot = glob.glob(os.path.join(efivars_dir, %22SecureBoot-*/data%22))%0A    if len(sboot) == 1:%0A        # The minion is usually running as a privileged user, but is%0A        # not the case for the master.  Seems that the master can also%0A        # pick the grains, and this file can only be readed by %22root%22%0A        try:%0A            with salt.utils.files.fopen(sboot%5B0%5D, %22rb%22) as fd:%0A                enabled = fd.read()%5B-1:%5D == b%22%5Cx01%22%0A        except PermissionError:%0A            pass%0A    return enabled%0A%0A%0Adef uefi():%0A    %22%22%22Populate UEFI grains.%22%22%22%0A    efivars_dir = next(%0A        filter(os.path.exists, %5B%22/sys/firmware/efi/efivars%22, %22/sys/firmware/efi/vars%22%5D),%0A        None,%0A    )%0A    grains = %7B%0A        %22efi%22: bool(efivars_dir),%0A        %22efi-secure-boot%22: __secure_boot(efivars_dir) if efivars_dir else False,%0A    %7D%0A%0A    return grains%0A%0A%0Adef transactional():%0A    %22%22%22Determine if the system is transactional.%22%22%22%0A    return %7B%22transactional%22: bool(salt.utils.path.which(%22transactional-update%22))%7D%0A

add EFI and transactional grains