# SPDX-License-Identifier: MIT
---
- name: Get the setting {{ __mssql_conf_setting }}
# noqa jinja[spacing]
  command: >-
    grep '^{{ __mssql_conf_setting | regex_replace('^.*\s' ,'') }} = '
    {{ __mssql_conf_path }}
  changed_when: false
  failed_when: false
  register: __mssql_conf_get_setting

- name: Configure the setting {{ __mssql_conf_setting }}
# noqa jinja[spacing]
  command: >-
    {{ __mssql_conf_cli }} set {{ __mssql_conf_setting |
    regex_replace(' ','.') }} {{ __mssql_conf_setting_value | quote }}
  vars:
    __mssql_conf_get_value: >-
      {{ __mssql_conf_get_setting.stdout |
      regex_replace('^.*\s=\s', '') }}
  when: >-
    (__mssql_conf_setting_value != 'unset') and
    (("No setting for the given" in __mssql_conf_get_setting.stdout) or
    ((__mssql_conf_setting_value | type_debug != "bool") and
    (__mssql_conf_setting_value | string | lower not in
    __mssql_conf_get_value | lower)) or
    ((__mssql_conf_setting_value | type_debug == "bool") and
    (__mssql_conf_setting_value != __mssql_conf_get_value | bool)))
  register: __mssql_conf_set
  failed_when:
    - >-
      ("error" in __mssql_conf_set.stdout | lower) or
      (__mssql_conf_set is failed)
    - >-
      "is already in use. Please use another port" not in
      __mssql_conf_set.stdout
  changed_when: >-
    "SQL Server needs to be restarted in order to apply this setting." in
    __mssql_conf_set.stdout
  notify: Restart the mssql-server service

- name: Unset the setting {{ __mssql_conf_setting }}
  command: "{{ __mssql_conf_cli }} unset {{ __mssql_conf_setting }}"
  when:
    - __mssql_conf_setting_value == "unset"
    - '"No setting for the given" not in __mssql_conf_get_setting.stdout'
  register: __mssql_conf_unset
  failed_when: >-
    ("error" in __mssql_conf_unset.stdout | lower) or
    (__mssql_conf_unset is failed)
  notify: Restart the mssql-server service
