---
- name: Dependencies installed
  # We install xmlsec and cairocffi using pkg because python-Levenshtein is tricky to install on FreeBSD
  community.general.pkgng:
    name: git,py37-pip,vim-console,tmux,fish,direnv,postgresql12-client,py37-sqlite3
    state: latest

- name: Root shell is fish
  ansible.builtin.user: name=root shell=/usr/local/bin/fish

- name: Nginx location file installed
  copy:
    src: nginx-location.conf
    dest: /usr/local/etc/nginx/locations/mathesar.conf
    mode: 0644
  notify: Nginx restarted

- name: Source folder exists
  file:
    path: /usr/local/src
    mode: 0755
    state: directory

- name: Create python bin link for yarn
  file:
    src: /usr/local/bin/python3.8
    dest: /usr/bin/python
    owner: root
    state: link

- name: 15Five source deployed
  git:
    repo: "https://github.com/centerofci/mathesar"
    dest: /usr/local/src/mathesar
    accept_hostkey: yes
    update: yes
  ignore_errors: true
  tags:
    - deploy

- name: Syncthing ignore file installed
  copy:
    src: stignore
    dest: /usr/local/src/fifteen5/.stignore
    mode: 0775

- name: Syncthing running as root
  shell:
    cmd: 'sed -i -e ''/^: ${syncthing_user/s/"syncthing"/"root"/'' /usr/local/etc/rc.d/syncthing'
    warn: false
  notify: Syncthing restarted

- name: Environment file installed
  template:
    src: envrc.j2
    dest: /usr/local/src/mathesar/.envrc
    mode: 0600
  tags:
    - deploy

- name: Allow environment file
  shell:
    cmd: direnv allow
    chdir: /usr/local/src/mathesar
  tags:
    - deploy

- name: Requirements up to date
  pip:
    requirements: /usr/local/src/mathesar/requirements.txt
    executable: pip
  tags:
    - deploy

- name: Migrations up to date
  command: bash -lc "direnv exec /usr/local/src/mathesar /usr/local/bin/python3.8 /usr/local/src/mathesar/manage.py migrate"
  tags:
    - deploy

- name: Install base tables
  command: bash -lc "direnv exec /usr/local/src/mathesar /usr/local/bin/python3.8 /usr/local/src/mathesar/install.py"
  tags:
    - deploy

- name: Frontend files generated and copied
  command: bash -lc "direnv exec /usr/local/src/mathesar/usr/local/bin/npm install"
  args:
    chdir: "/usr/local/src/mathesar"
  tags:
    - deploy
    - never

- name: API supervisor file installed
  copy:
    src: mathesar-api-supervisor.conf
    dest: /usr/local/etc/supervisor/conf.d/mathesar-api.conf
    owner: root
    mode: 0644
  notify: Supervisor restarted

- name: UI supervisor file installed
  copy:
    src: mathesar-ui-supervisor.conf
    dest: /usr/local/etc/supervisor/conf.d/mathesar-ui.conf
    owner: root
    mode: 0644
  notify: Supervisor restarted

