---

- name: create ssl cert
  command: certbot certonly --email webmaster@archlinux.org --agree-tos --rsa-key-size 4096 --renew-by-default --webroot -w {{letsencrypt_validation_dir}} -d '{{ matrix_domain }}' creates='/etc/letsencrypt/live/{{ matrix_domain }}/fullchain.pem'
  when: 'matrix_domain is defined'

- name: install packages
  pacman: name=python,git,npm,make,jemalloc,gcc,pkgconf,python2

- name: add synapse group
  group: name=synapse system=yes gid=198

- name: add synapse user
  user: name=synapse system=yes uid=198 group=synapse home=/var/lib/synapse shell=/bin/false createhome=no

- name: create synapse home
  file: path={{ item }} state=directory owner=synapse group=synapse mode=0700
  with_items:
    - /var/lib/synapse
    - /var/lib/synapse/irc-nedb
    - /var/lib/synapse/media_store
    - /var/lib/synapse/uploads

- name: make virtualenv
  command: python -m venv /var/lib/synapse/venv
  args:
    creates: /var/lib/synapse/venv/bin/python
  become: true
  become_user: synapse
  become_method: sudo

- name: install synapse
  pip:
    name:
      - 'matrix-synapse[postgres,systemd,url_preview]'
      - pip
    state: latest
    extra_args: '-U --upgrade-strategy=eager'
    virtualenv: /var/lib/synapse/venv
  become: yes
  become_user: synapse
  become_method: sudo
  notify:
    - restart synapse

- name: download matrix-appservice-irc
  git:
    repo: https://github.com/heftig/matrix-appservice-irc
    dest: /var/lib/synapse/matrix-appservice-irc
  become: yes
  become_user: synapse
  become_method: sudo
  notify:
    - restart matrix-appservice-irc

- name: install matrix-appservice-irc
  npm:
    path: /var/lib/synapse/matrix-appservice-irc
    production: yes
  become: yes
  become_user: synapse
  become_method: sudo
  notify:
    - restart matrix-appservice-irc

- name: add synapse postgres db
  postgresql_db: db=synapse
  become: yes
  become_user: postgres
  become_method: su

- name: add synapse postgres user
  postgresql_user: db=synapse name=synapse password={{ vault_postgres_users.synapse }} encrypted=true
  become: yes
  become_user: postgres
  become_method: su

- name: create synapse config dir
  file: state=directory path=/etc/synapse owner=root group=synapse mode=0750

- name: install homeserver config
  template: src=homeserver.yaml.j2 dest=/etc/synapse/homeserver.yaml owner=root group=synapse mode=0640

- name: install log config
  copy: src=log_config.yaml dest=/etc/synapse/log_config.yaml owner=root group=root mode=0644

- name: install irc-bridge config
  template: src=irc-bridge.yaml.j2 dest=/etc/synapse/irc-bridge.yaml owner=root group=synapse mode=0640

- name: install irc-bridge registration
  template: src=appservice-registration-irc.yaml.j2 dest=/etc/synapse/appservice-registration-irc.yaml owner=root group=synapse mode=0640

- name: install signing key
  copy:
    content: '{{ vault_matrix_secrets[matrix_server_name].signing_key }}'
    dest: /etc/synapse/{{ matrix_server_name }}.signing.key
    owner: root
    group: synapse
    mode: 0640

- name: install ircpass key
  copy:
    content: '{{ vault_matrix_secrets[matrix_server_name].ircpass_key }}'
    dest: /etc/synapse/{{ matrix_server_name }}.ircpass.key
    owner: root
    group: synapse
    mode: 0640

- name: make nginx log dir
  file: path=/var/log/nginx/{{ matrix_domain }} state=directory owner=root group=root mode=0755

- name: set up nginx
  template: src=nginx.d.conf.j2 dest=/etc/nginx/nginx.d/matrix.conf owner=root group=root mode=0644
  notify:
    - reload nginx
  when: 'matrix_domain is defined'
  tags: ['nginx']

- name: install matrix units
  copy: src={{ item }} dest=/etc/systemd/system/{{ item }} owner=root group=root mode=0644
  with_items:
    - synapse.service
    - matrix-appservice-irc.service
  notify:
    - restart synapse
    - restart matrix-appservice-irc

- name: open firewall holes
  firewalld: port={{ item }} permanent=true state=enabled immediate=yes
  with_items:
    - 8848/tcp
    - 113/tcp
  when: configure_firewall
  tags:
    - firewall
