---
# ---
# Some Checks
# ---

- name: Check if file '/etc/postfix/relay_domains' exists
  ansible.builtin.stat:
    path: /etc/postfix/relay_domains
  register: common_relay_domains_actual

- name: (copy_files.yml) Get checksum of '/etc/postfix/relay_domains'
  ansible.builtin.set_fact:
    common_relay_domains_sha1: "{{ common_relay_domains_actual.stat.checksum }}"
  when:
    - common_relay_domains_actual.stat.exists

# ---
# Copy files - main
# ---

- name: (copy_files.yml) Copy plain files
  ansible.builtin.copy:
    src: "{{ item.src_path }}"
    dest: "{{ item.dest_path }}"
    owner: root
    group: root
    mode: "0644"
  loop: "{{ copy_plain_files }}"
  loop_control:
    label: "dest: {{ item.name }}"
  when:
    - copy_plain_files is defined
    - copy_plain_files|length > 0
  tags:
    - copy-files
    - copy-plain-files

- name: (copy_files.yml) HOST-TREE precheck host specific source directory on controller
  ansible.builtin.stat:
    path: "{{ role_path }}/files/{{ inventory_hostname }}"
  delegate_to: localhost
  become: false
  register: common_host_dir
  when:
    - inventory_hostname in groups['mail_server']
  tags:
    - copy-files
    - copy-plain-files

- name: (copy_files.yml) HOST-TREE precheck host specific template directory on controller
  ansible.builtin.stat:
    path: "{{ role_path }}/templates/{{ inventory_hostname }}"
  delegate_to: localhost
  become: false
  register: common_host_template_dir
  when:
    - inventory_hostname in groups['mail_server']
  tags:
    - copy-files
    - copy-template-files

- name: (copy_files.yml) GLOBAL copy plain files Postfix (/etc/postfix)
  ansible.builtin.copy:
    src: "{{ item.src_path }}"
    dest: "{{ item.dest_path }}"
    owner: root
    group: root
    mode: "0644"
  loop: "{{ copy_plain_files_postfix }}"
  loop_control:
    label: "dest: {{ item.name }}"
  when:
    - inventory_hostname in groups['mail_server']
    - copy_plain_files_postfix is defined
    - copy_plain_files_postfix|length > 0
    - not (
      common_host_dir.stat.exists and
      (lookup('ansible.builtin.fileglob', role_path ~ '/files/' ~ inventory_hostname ~ item.dest_path, wantlist=True) | length > 0)
      )
  tags:
    - copy-files
    - copy-plain-files
  notify: "Reload postfwd"

- name: (copy_files.yml) FALLBACK legacy copy host specific plain files Postfix (/etc/postfix)
  ansible.builtin.copy:
    src: "{{ item.src_path }}"
    dest: "{{ item.dest_path }}"
    owner: root
    group: root
    mode: "0644"
  loop: "{{ copy_plain_files_postfix_host_specific }}"
  loop_control:
    label: "dest: {{ item.name }}"
  when:
    - inventory_hostname in groups['mail_server']
    - copy_plain_files_postfix_host_specific is defined
    - copy_plain_files_postfix_host_specific|length > 0
    - not common_host_dir.stat.exists
  tags:
    - copy-files
    - copy-plain-files
  notify: "Reload postfwd"

- name: (copy_files.yml) GLOBAL copy plain files Postfix Firewall (postfwd)
  ansible.builtin.copy:
    src: "{{ item.src_path }}"
    dest: "{{ item.dest_path }}"
    owner: root
    group: root
    mode: "0644"
  loop: "{{ copy_plain_files_postfwd }}"
  loop_control:
    label: "dest: {{ item.name }}"
  when:
    - inventory_hostname in groups['mail_server']
    - copy_plain_files_postfwd is defined
    - copy_plain_files_postfwd|length > 0
    - not (
      common_host_dir.stat.exists and
      (lookup('ansible.builtin.fileglob', role_path ~ '/files/' ~ inventory_hostname ~ item.dest_path, wantlist=True) | length > 0)
      )
  tags:
    - copy-files
    - copy-plain-files
  notify: "Reload postfwd"

- name: (copy_files.yml) FALLBACK legacy copy host specific plain files Postfix Firewall (postfwd)
  ansible.builtin.copy:
    src: "{{ item.src_path }}"
    dest: "{{ item.dest_path }}"
    owner: root
    group: root
    mode: "0644"
  loop: "{{ copy_plain_files_postfwd_host_specific }}"
  loop_control:
    label: "dest: {{ item.name }}"
  when:
    - inventory_hostname in groups['mail_server']
    - copy_plain_files_postfwd_host_specific is defined
    - copy_plain_files_postfwd_host_specific|length > 0
    - not common_host_dir.stat.exists
  tags:
    - copy-files
    - copy-plain-files
  notify: "Reload postfwd"

- name: (copy_files.yml) HOST-TREE copy all host specific files by directory structure
  ansible.builtin.copy:
    src: "{{ role_path }}/files/{{ inventory_hostname }}/"
    dest: "/"
    owner: root
    group: root
    mode: preserve
  when:
    - inventory_hostname in groups['mail_server']
    - common_host_dir.stat.exists
    - common_host_dir.stat.isdir
  tags:
    - copy-files
    - copy-plain-files
  notify: "Reload postfwd"


- name: (copy_files.yml) copy template files
  ansible.builtin.template:
    src: "{{ item.src_path }}"
    dest: "{{ item.dest_path }}"
    owner: root
    group: root
    mode: "0644"
  loop: "{{ copy_template_files }}"
  loop_control:
    label: "dest: {{ item.name }}"
  when:
    - copy_template_files is defined
    - copy_template_files|length > 0
  tags:
    - copy-files
    - copy-template-files

# ---
# Some final tasks
# ---

- name: Get checksum oif (possible upodated) file '/etc/postfix/relay_domains' exists
  ansible.builtin.stat:
    path: /etc/postfix/relay_domains
  register: common_relay_domains_new

- name: (copy_files.yml) Get checksum of '/etc/postfix/relay_domains'
  ansible.builtin.set_fact:
    common_relay_domains_sha1_new: "{{ common_relay_domains_new.stat.checksum }}"
  when:
    - common_relay_domains_new.stat.exists

- name: (copy_files.yml) Renew database /etc/postfix/relay_domains.db
  ansible.builtin.command: "/usr/sbin/postmap btree:/etc/postfix/relay_domains"
  changed_when: true
  when:
    - common_relay_domains_actual.stat.exists
    - common_relay_domains_new.stat.exists
    - common_relay_domains_actual.stat.checksum != common_relay_domains_new.stat.checksum
  notify: "Reload postfwd"
