- name: UFW - Allow connections to GoTrue metrics exporter
  ufw:
    rule: allow
    port: "9122"
    
# use this user for the Gotrue build and for running the service
- name: Gotrue - system user
  user: name=gotrue

- name: Setting arch (x86)
  set_fact:
    arch: "x86"
  when: platform == "amd64"

- name: Setting arch (arm)
  set_fact:
    arch: "arm64"
  when: platform == "arm64"

- name: gotrue - download commit archive
  get_url:
    url: "https://github.com/supabase/gotrue/releases/download/v{{ gotrue_release }}/auth-v{{ gotrue_release }}-{{ arch }}.tar.gz"
    dest: /tmp/gotrue.tar.gz
    checksum: "{{ gotrue_release_checksum }}"

- name: gotrue - create /opt/gotrue
  file:
    path: /opt/gotrue
    state: directory
    owner: gotrue
    mode: 0775

- name: gotrue - unpack archive in /opt/gotrue
  unarchive:
    remote_src: yes
    src: /tmp/gotrue.tar.gz
    dest: /opt/gotrue
    owner: gotrue

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

- name: gotrue - create service file
  template:
    src: files/gotrue.service.j2
    dest: /etc/systemd/system/gotrue.service

- name: gotrue - create optimizations file
  template:
    src: files/gotrue-optimizations.service.j2
    dest: /etc/systemd/system/gotrue-optimizations.service

- name: gotrue - reload systemd
  systemd:
    daemon_reload: yes
