---
- name: Dependencies installed
  # We install xmlsec and cairocffi using pkg because python-Levenshtein is tricky to install on FreeBSD
  community.general.pkgng:
    name: git-tiny,py310-pip,jpeg-turbo,tiff,webp,lcms2,freetype2,cairo,pango,rust,libxml2,libxslt,xmlsec1,pkgconf,postgresql13-client,cmake,gdal,geos,direnv,bash
    state: latest

- name: Poetry & awscli installed
  pip:
    name: poetry,awscli

- name: Github SSH key installed
  template:
    src: sshkey.j2
    dest: /root/.ssh/id_ed25519
    mode: 0400

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

- name: PorchPals source deployed
  git:
    repo: "git@github.com:sureapp/platform-porchpals.git"
    dest: /usr/local/src/platform-porchpals
    accept_hostkey: yes
    update: yes
  ignore_errors: true
  tags:
    - deploy

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

- name: Syncthing running as root
  shell: sysrc syncthing_user="root"
  notify: Syncthing restarted

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

- cron:
    name: "Cron job to clear out tmp files every 5 minutes installed"
    minute: "5"
    job: 'find /usr/local/src/platform-porchpals -type f -name ".syncthing*.tmp" -delete'

- cron:
    name: "Cron job to clear out syncthing conflicts every 30 min"
    minute: "30"
    job: 'find /usr/local/src/platform-porchpals -type f -name "*.sync-conflict*" -delete'

- name: Configure ports
  command: make
  args:
    chdir: /usr/ports/devel/py-pip
    chdir: /usr/ports/devel/py-pkgconfig # may not be needed with pkgconf
- name: PorchPals dependecies installed
  command: poetry install
  args:
    chdir: /usr/local/src/platform-porchpals


- name: PorchPals migrations up to date
  command: poetry run python manage.py migrate
  args:
    chdir: /usr/local/src/platform-porchpals

- name: PorchPals immortal file installed
  template:
    src: porchpals.yml.j2
    dest: /usr/local/etc/immortal/porchpals.yml
    owner: root
    mode: 0600
  notify: Immortal restarted
