# Get the SYSTEMROOT value from the registry
{% set systemroot = salt.reg.read_value(
    'HKEY_LOCAL_MACHINE',
    'SOFTWARE\Microsoft\Windows NT\CurrentVersion',
    'SystemRoot').vdata
%}

# Set the SYSTEMDRIVE value to the first two characters of SYSTEMROOT
# See http://msdn.microsoft.com/en-us/library/cc231436.aspx
{% set systemdrive = systemroot|truncate(2, True, '') %}

# Get the OS version from the grain 'osfullname'
{% set os_ver = salt.grains.get('osfullname') %}

# Get the IE version from the registry for IE 10 and 11
{% set ie_ver = salt.reg.read_value(
    'HKEY_LOCAL_MACHINE',
    'SOFTWARE\Microsoft\Internet Explorer',
    'svcVersion').vdata
%}

# Get the IE version from the registry for IE 8 and 9
{% if not ie_ver or ie_ver == '(value not set)' %}
    {% set ie_ver = salt.reg.read_value(
        'HKEY_LOCAL_MACHINE',
        'SOFTWARE\Microsoft\Internet Explorer',
        'Version').vdata
    %}
{% endif %}

# Define default role for supported Operating System versions
{% set default_role = salt.grains.filter_by(
    {
        '2016Server' : 'MemberServer',
        '2012ServerR2' : 'MemberServer',
        '2008ServerR2' : 'MemberServer',
        '8.1' : 'Workstation',
        '10' : 'Workstation',
    },
    grain='osrelease'
) %}

# Define lookup dictionary of default settings for the supported role types
{% set role_path_map = {
    'MemberServer' : '_MS',
    'DomainController' : '_DC',
    'Workstation' : '',
} %}

# Get the role
{% set role = salt.pillar.get(
    'ash-windows:lookup:role',
    salt.grains.get(
        'ash-windows:lookup:role',
        default_role
    )
) %}

# Define lookup table with default paths
{% set defaults = {
    'logdir' : systemdrive ~ '\\Ash\\Logs',
    'apply_lgpo_source' : 'https://s3.amazonaws.com/systemprep-repo/windows/lgpo-utilities/Apply_LGPO_Delta.exe',
    'apply_lgpo_source_hash' : 'https://s3.amazonaws.com/systemprep-repo/windows/lgpo-utilities/Apply_LGPO_Delta.exe.SHA512',
    'apply_lgpo_filename' : systemroot ~ '\\system32\\Apply_LGPO_Delta.exe',
} %}

# Import user defined settings from pillar (if available)
{% set ash = salt.pillar.get(
    'ash-windows:lookup',
    default=defaults,
    merge=True
) %}

# Define the lookup table to return to the ash state files
{% do ash.update({
    'ie_path' : 'IE_' ~ ie_ver.split('.')[0],
    'os_path' : 'Windows_' ~ grains.osrelease |replace('.','_') ~ role_path_map[role],
    'pth_admx_name' : systemroot ~ '\\PolicyDefinitions\\PtH.admx',
    'pth_adml_name' : systemroot ~ '\\PolicyDefinitions\\en-US\\PtH.adml',
    'win_audit_dir' : systemroot ~ '\\System32\GroupPolicy\\Machine\\Microsoft\\Windows NT\\Audit',
    'win_audit_file_name' : systemroot ~ '\\System32\\GroupPolicy\\Machine\\Microsoft\\Windows NT\\Audit\\Audit.csv',
}) %}
