- name: PostgREST - system user
  user: name=postgrest

- name: PostgREST - add Postgres PPA gpg key
  apt_key:
    url: https://www.postgresql.org/media/keys/ACCC4CF8.asc
    state: present

- name: PostgREST - add Postgres PPA
  apt_repository:
    repo: "deb http://apt.postgresql.org/pub/repos/apt/ focal-pgdg {{ postgresql_major }}"
    state: present

- name: PostgREST - update apt cache
  apt:
    update_cache: yes

# libpq is a C library that enables user programs to communicate with
# the PostgreSQL database server.
- name: PostgREST - system dependencies
  apt:
    pkg:
      - libpq5
      - libnuma-dev

- name: PostgREST - remove Postgres PPA gpg key
  apt_key:
    url: https://www.postgresql.org/media/keys/ACCC4CF8.asc
    state: absent

- name: PostgREST - remove Postgres PPA
  apt_repository:
    repo: "deb http://apt.postgresql.org/pub/repos/apt/ focal-pgdg {{ postgresql_major }}"
    state: absent

- name: postgis - ensure dependencies do not get autoremoved
  shell: |
    set -e
    apt-mark manual libpq5*
    apt-mark manual libnuma*
    apt-mark auto libnuma*-dev

- name: PostgREST - download ubuntu binary archive (arm)
  get_url:
    url: "https://github.com/PostgREST/postgrest/releases/download/v{{ postgrest_release }}/postgrest-v{{ postgrest_release }}-ubuntu-aarch64.tar.xz"
    dest: /tmp/postgrest.tar.xz
    checksum: "{{ postgrest_arm_release_checksum }}"
    timeout: 60
  when: platform == "arm64"

- name: PostgREST - download ubuntu binary archive (x86)
  get_url:
    url: "https://github.com/PostgREST/postgrest/releases/download/v{{ postgrest_release }}/postgrest-v{{ postgrest_release }}-linux-static-x64.tar.xz"
    dest: /tmp/postgrest.tar.xz
    checksum: "{{ postgrest_x86_release_checksum }}"
    timeout: 60    
  when: platform == "amd64"

- name: PostgREST - unpack archive in /opt
  unarchive:
    remote_src: yes
    src: /tmp/postgrest.tar.xz
    dest: /opt
    owner: postgrest
    mode: '0755'

- name: create directories
  file:
    state: directory
    owner: postgrest
    group: postgrest
    mode: '0775'
    path: /etc/postgrest

- name: empty files
  file:
    state: touch
    owner: postgrest
    group: postgrest
    path: /etc/postgrest/{{ item }}
  with_items:
    - base.conf
    - generated.conf

- name: create conf merging script
  copy:
    content: |
      #! /usr/bin/env bash
      set -euo pipefail
      set -x

      cd "$(dirname "$0")"
      cat $@ > merged.conf
    dest: /etc/postgrest/merge.sh
    mode: 0750
    owner: postgrest
    group: postgrest

- name: PostgREST - create service files
  template:
    src: files/{{ item }}.j2
    dest: /etc/systemd/system/{{ item }}
  with_items:
    - postgrest.service
    - postgrest-optimizations.service

- name: PostgREST - reload systemd
  systemd:
    daemon_reload: yes
