# platform = multi_platform_all
# reboot = false
# strategy = configure
# complexity = low
# disruption = low

{{%- if RECURSIVE %}}
{{%- set FIND_RECURSE_ARGS_DEP="" %}}
{{%- elif FILE_REGEX %}}
{{%- set FIND_RECURSE_ARGS_DEP="-maxdepth 1" %}}
{{%- else %}}
{{%- set FIND_RECURSE_ARGS_DEP="-maxdepth 0" %}}
{{%- endif %}}

{{%- set ns=namespace(find_groups="") %}}
{{%- if GROUP_REPRESENTED_WITH_GID %}}
{{%- set ns.find_groups=ns.find_groups ~ " ! -group " ~ GID_OR_NAME %}}
- name: Set the {{{ _RULE_ID }}}_newgroup variable if represented by gid
  ansible.builtin.set_fact:
    {{{ _RULE_ID }}}_newgroup: "{{{ GID_OR_NAME }}}"
{{%- else %}}
{{%- set GROUPS=GID_OR_NAME.split("|") %}}
{{%- for grp in GROUPS %}}
{{%- set ns.find_groups=ns.find_groups ~ " ! -group " ~ grp %}}
- name: Check that the {{{ grp }}} group is defined
  ansible.builtin.getent:
    database: group
    key: {{{ grp }}}
  ignore_errors: yes
  when: {{{ _RULE_ID }}}_newgroup is undefined

- name: Set the {{{ _RULE_ID }}}_newgroup variable if {{{ grp }}} found
  ansible.builtin.set_fact:
    {{{ _RULE_ID }}}_newgroup: "{{{ grp }}}"
  when: ansible_facts.getent_group["{{{ grp }}}"] is defined
{{%- endfor %}}
{{%- endif %}}

{{%- for path in FILEPATH %}}
{{%- if IS_DIRECTORY %}}
{{%- if FILE_REGEX %}}

- name: Find {{{ path }}} file(s) matching {{{ FILE_REGEX[loop.index0] }}}{{% if RECURSIVE %}} recursively{{% endif %}}
  ansible.builtin.command: 'find -P {{{ path }}} {{{ FIND_RECURSE_ARGS_DEP }}} -type f {{{ ns.find_groups }}} -regextype posix-extended -regex "{{{ FILE_REGEX[loop.index0] }}}"'
  register: files_found
  changed_when: False
  failed_when: False
  check_mode: no

- name: Ensure group owner on {{{ path }}} file(s) matching {{{ FILE_REGEX[loop.index0] }}}
  ansible.builtin.file:
    path: "{{ item }}"
    follow: false
    group: "{{ {{{ _RULE_ID }}}_newgroup }}"
    state: file
  with_items:
    - "{{ files_found.stdout_lines }}"

{{% else %}}

- name: Ensure group owner on {{{ path }}}{{% if RECURSIVE %}} recursively{{% endif %}}
  ansible.builtin.file:
    path: "{{{ path }}}"
    follow: false
    state: directory
{{% if RECURSIVE %}}
    recurse: yes
{{% endif %}}
    group: "{{ {{{ _RULE_ID }}}_newgroup }}"

{{% endif %}}
{{% else %}}

- name: Test for existence {{{ path }}}
  ansible.builtin.stat:
    path: "{{{ path }}}"
  register: file_exists

- name: Ensure group owner on {{{ path }}}
  ansible.builtin.file:
    path: "{{{ path }}}"
    follow: false
    group: "{{ {{{ _RULE_ID }}}_newgroup }}"
  when: file_exists.stat is defined and file_exists.stat.exists

{{% endif %}}
{{% endfor %}}
