---
- name: Ensure /etc/systemd/system/postgresql.service.d exists
  ansible.builtin.file:
    path: /etc/systemd/system/postgresql@.service.d
    state: directory
    recurse: true

- name: Override PostgreSQL systemd unit
  ansible.builtin.copy:
    src: postgresql.service.override.conf
    dest: /etc/systemd/system/postgresql@.service.d/override.conf
    force: true
    mode: "0644"
  notify:
    - reload systemd
    - restart postgresql

- name: Allow conf.d/*.conf files to be included in PostgreSQL configuration
  ansible.builtin.lineinfile:
    name: "/etc/postgresql/{{ postgresql_version }}/main/postgresql.conf"
    line: include_dir = 'conf.d'
  notify: restart postgresql

- name: Create conf.d directory
  ansible.builtin.file:
    name: "/etc/postgresql/{{ postgresql_version }}/main/conf.d/"
    state: directory
    owner: postgres
    group: postgres
    mode: "0755"

- name: Copy PostgreSQL config file
  ansible.builtin.template:
    src: postgresql.conf.j2
    dest: "/etc/postgresql/{{ postgresql_version }}/main/conf.d/zz-evolinux.conf"
    owner: postgres
    group: postgres
    mode: "0644"
  notify: restart postgresql

- ansible.builtin.meta: flush_handlers
