- name: Copy extension packages
  copy:
    src: files/extensions/
    dest: /tmp/extensions/
  when: debpkg_mode

# Builtin apt module does not support wildcard for deb paths
- name: Install extensions
  shell: |
    set -e
    apt-get update
    apt-get install -y --no-install-recommends /tmp/extensions/*.deb
  when: debpkg_mode

- name: pgsodium - determine postgres bin directory
  shell: pg_config --bindir
  register: pg_bindir_output
  when: debpkg_mode
  
- set_fact:
    pg_bindir: "{{ pg_bindir_output.stdout }}"
  when: debpkg_mode 

- name: pgsodium - set pgsodium.getkey_script
  become: yes
  lineinfile:
    path: /etc/postgresql/postgresql.conf
    state: present
    # script is expected to be placed by finalization tasks for different target platforms
    line: pgsodium.getkey_script= '{{ pg_bindir }}/pgsodium_getkey.sh'
  when: debpkg_mode

# supautils
- name: supautils - add supautils to session_preload_libraries
  become: yes
  replace:
    path: /etc/postgresql/postgresql.conf
    regexp: "#session_preload_libraries = ''"
    replace: session_preload_libraries = 'supautils'
  when: debpkg_mode or stage2_nix

- name: supautils - write custom supautils.conf
  template:
    src: "files/postgresql_config/supautils.conf.j2"
    dest: /etc/postgresql-custom/supautils.conf
    mode: 0664
    owner: postgres
    group: postgres
  when: debpkg_mode or stage2_nix

- name: supautils - copy extension custom scripts
  copy:
    src: files/postgresql_extension_custom_scripts/
    dest: /etc/postgresql-custom/extension-custom-scripts
  become: yes
  when: debpkg_mode or stage2_nix

- name: supautils - chown extension custom scripts
  file:
    mode: 0775
    owner: postgres
    group: postgres
    path: /etc/postgresql-custom/extension-custom-scripts
    recurse: yes
  become: yes
  when: debpkg_mode or stage2_nix

- name: supautils - include /etc/postgresql-custom/supautils.conf in postgresql.conf
  become: yes
  replace:
    path: /etc/postgresql/postgresql.conf
    regexp: "#include = '/etc/postgresql-custom/supautils.conf'"
    replace: "include = '/etc/postgresql-custom/supautils.conf'"
  when: debpkg_mode or stage2_nix

- name: Cleanup - extension packages
  file:
    path: /tmp/extensions
    state: absent
  when: debpkg_mode
