# supautils
- name: supautils - download & install dependencies
  apt:
    pkg:
      - build-essential
      - clang-11
    update_cache: yes
    cache_valid_time: 3600

- name: supautils - download latest release
  get_url:
    url: "https://github.com/supabase/supautils/archive/refs/tags/v{{ supautils_release }}.tar.gz"
    dest: /tmp/supautils-{{ supautils_release }}.tar.gz
    checksum: "{{ supautils_release_checksum }}"
    timeout: 60

- name: supautils - unpack archive
  unarchive:
    remote_src: yes
    src: /tmp/supautils-{{ supautils_release }}.tar.gz
    dest: /tmp
  become: yes

- name: supautils - build
  make:
    chdir: /tmp/supautils-{{ supautils_release }}
  become: yes

- name: supautils - install
  make:
    chdir: /tmp/supautils-{{ supautils_release }}
    target: install
  become: yes

- 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'

- 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

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

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

- 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'"

- name: supautils - remove build dependencies
  apt:
    pkg:
      - build-essential
      - clang-11
    state: absent
