---
- name: install required packages
  pacman:
    state: present
    name:
      - asciidoc
      - highlight
      - make
      - sudo
      - uwsgi-plugin-cgi
      - python-poetry
      - gcc
      - pkg-config

- name: install the cgit package
  pacman:
    state: present
    name:
      - cgit-aurweb
  register: cgit

- name: install the git package
  pacman:
    state: present
    name:
      - git
  register: git

- name: make aur user
  user: name="{{ aurweb_user }}" shell=/bin/bash createhome=yes

- name: Create directory
  file: path={{ aurweb_dir }} state=directory owner={{ aurweb_user }} group=http mode=0775

- name: receive valid signing keys
  command: /usr/bin/gpg --keyserver keys.openpgp.org --recv {{ item }}
  loop: '{{ aurweb_pgp_keys }}'
  become: true
  become_user: "{{ aurweb_user }}"
  register: gpg
  changed_when: "gpg.rc == 0"

- name: clone aurweb repo
  git: >
    repo={{ aurweb_repository }}
    dest="{{ aurweb_dir }}"
    version={{ aurweb_version }}
    verify_commit: true
    gpg_whitelist: '{{ aurweb_pgp_keys }}'
  become: true
  become_user: "{{ aurweb_user }}"
  register: release

- name: create necessary directories
  file: path={{ aurweb_dir }}/{{ item }} state=directory owner={{ aurweb_user }} group={{ aurweb_user }} mode=0755
  with_items:
    - 'aurblup'
    - 'sessions'
    - 'uploads'

- name: create aurweb conf dir
  file: path={{ aurweb_conf_dir }} state=directory owner=root group=root mode=0755

- name: copy aurweb configuration file
  copy: src={{ aurweb_dir }}/conf/config.defaults dest={{ aurweb_conf_dir }}/config.defaults remote_src=yes owner=root group=root mode=0644

# Note: initdb needs the config
- name: install custom aurweb configuration
  template: src=config.j2 dest={{ aurweb_conf_dir }}/config owner=root group=root mode=0644

- name: create aur db
  mysql_db: name="{{ aurweb_db }}" login_host="{{ aurweb_db_host }}" login_password="{{ vault_mariadb_users.root }}" encoding=utf8
  register: db_created
  no_log: true

- name: create aur db user
  mysql_user: name={{ aurweb_db_user }} password={{ vault_aurweb_db_password }}
              login_host="{{ aurweb_db_host }}" login_password="{{ vault_mariadb_users.root }}"
              priv="{{ aurweb_db }}.*:ALL"
  no_log: true

- name: initialize the database
  command: poetry run python -m aurweb.initdb
  args:
    chdir: "{{ aurweb_dir }}"
  become: true
  become_user: "{{ aurweb_user }}"
  when: db_created.changed

- name: run migrations
  command: poetry run alembic upgrade head
  args:
    chdir: "{{ aurweb_dir }}"
  environment:
    - PYTHONPATH: .
  become: true
  become_user: "{{ aurweb_user }}"
  when: release.changed or db_created.changed

- name: Check python module availability
  command: poetry run python3 -c 'import aurweb'
  args:
    chdir: "{{ aurweb_dir }}"
  become: true
  become_user: "{{ aurweb_user }}"
  ignore_errors: true
  register: aurweb_installed
  tags:
    - skip_ansible_lint

- name: Install python module
  command: poetry install
  args:
    chdir: "{{ aurweb_dir }}"
  become: true
  become_user: "{{ aurweb_user }}"
  when: release.changed or aurweb_installed.rc != 0

- name: install custom aurweb-git-auth wrapper script
  template: src=aurweb-git-auth.sh.j2 dest=/usr/local/bin/aurweb-git-auth.sh owner=root group=root mode=0755
  when: release.changed

- name: install custom aurweb-git-serve wrapper script
  template: src=aurweb-git-serve.sh.j2 dest=/usr/local/bin/aurweb-git-serve.sh owner=root group=root mode=0755
  when: release.changed

- name: install custom aurweb-git-update wrapper script
  template: src=aurweb-git-update.sh.j2 dest=/usr/local/bin/aurweb-git-update.sh owner=root group=root mode=0755
  when: release.changed

- name: link custom aurweb-git-update wrapper to hooks/update
  file:
    src: /usr/local/bin/aurweb-git-update.sh
    dest: "{{ aurweb_dir }}/aur.git/hooks/update"
    state: link
  when: release.changed

- name: Generate HTML documentation
  make:
    chdir: "{{ aurweb_dir }}/doc"
  become: true
  become_user: "{{ aurweb_user }}"

- name: Generate Translations
  make:
    chdir: "{{ aurweb_dir }}/po"
    target: "install"
  become: true
  become_user: "{{ aurweb_user }}"

- name: create ssl cert
  include_role:
    name: certificate
  vars:
    domains: ["{{ aurweb_domain }}"]

- name: set up nginx
  template: src=nginx.d.conf.j2 dest={{ aurweb_nginx_conf }} owner=root group=root mode=644
  notify: reload nginx
  tags: ['nginx']

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

- name: install cgit configuration
  template: src=cgitrc.j2 dest="{{ aurweb_conf_dir }}/cgitrc" owner=root group=root mode=0644

- name: configure cgit uwsgi service
  template: src=cgit.ini.j2 dest=/etc/uwsgi/vassals/cgit.ini owner={{ aurweb_user }} group=http mode=0644

- name: deploy new cgit release
  become: true
  become_user: "{{ aurweb_user }}"
  file: path=/etc/uwsgi/vassals/cgit.ini state=touch owner=root group=root mode=0644
  when: cgit.changed

- name: configure smartgit uwsgi service
  template: src=smartgit.ini.j2 dest=/etc/uwsgi/vassals/smartgit.ini owner={{ aurweb_user }} group=http mode=0644

- name: deploy new smartgit release
  become: true
  become_user: "{{ aurweb_user }}"
  file:
    path: /etc/uwsgi/vassals/smartgit.ini
    state: touch
    owner: "{{ aurweb_user }}"
    group: http
    mode: 0644
  when: git.changed

- name: create git repo dir
  file: path={{ aurweb_git_dir }} state=directory owner={{ aurweb_user }} group=http mode=0775

- name: init git directory
  command: git init --bare {{ aurweb_git_dir }}
  args:
    creates: "{{ aurweb_git_dir }}/HEAD"
  become: true
  become_user: "{{ aurweb_user }}"
  tags:
    - skip_ansible_lint

- name: save hideRefs setting on var
  command: git config --local --get-all transfer.hideRefs
  register: git_config
  args:
    chdir: "{{ aurweb_git_dir }}"
  failed_when: git_config.rc == 2  # FIXME: does not work.
  tags:
    - skip_ansible_lint

- name: configure git tranfser.hideRefs
  command: git config --local transfer.hideRefs '^refs/'
  args:
    chdir: "{{ aurweb_git_dir }}"
  become: true
  become_user: "{{ aurweb_user }}"
  when: git_config.stdout.find('^refs/') == -1
  tags:
    - skip_ansible_lint

- name: configure git transfer.hideRefs second
  command: git config --local --add transfer.hideRefs '!refs/'
  args:
    chdir: "{{ aurweb_git_dir }}"
  become: true
  become_user: "{{ aurweb_user }}"
  when: git_config.stdout.find('!refs/') == -1
  tags:
    - skip_ansible_lint

- name: configure git transfer.hideRefs third
  command: git config --local --add transfer.hideRefs '!HEAD'
  args:
    chdir: "{{ aurweb_git_dir }}"
  become: true
  become_user: "{{ aurweb_user }}"
  when: git_config.stdout.find('!HEAD') == -1
  tags:
    - skip_ansible_lint

- name: install AUR systemd service and timers
  template: src={{ item }}.j2 dest=/etc/systemd/system/{{ item }} owner=root group=root mode=0644
  with_items:
    - aurweb-git.service
    - aurweb-git.timer
    - aurweb-aurblup.service
    - aurweb-aurblup.timer
    - aurweb-mkpkglists.service
    - aurweb-mkpkglists.timer
    - aurweb-pkgmaint.service
    - aurweb-pkgmaint.timer
    - aurweb-popupdate.service
    - aurweb-popupdate.timer
    - aurweb-tuvotereminder.service
    - aurweb-tuvotereminder.timer
    - aurweb-usermaint.service
    - aurweb-usermaint.timer
    - aurweb.service

- name: configure sshd
  template: src=aurweb_config.j2 dest={{ sshd_includes_dir }}/aurweb_config owner=root group=root mode=0600 validate='/usr/sbin/sshd -t -f %s'
  notify:
    - restart sshd

- name: start and enable AUR systemd services and timers
  service: name={{ item }} enabled=yes state=restarted daemon_reload=yes
  with_items:
    - aurweb-git.timer
    - aurweb-aurblup.timer
    - aurweb-mkpkglists.timer
    - aurweb-pkgmaint.timer
    - aurweb-popupdate.timer
    - aurweb-tuvotereminder.timer
    - aurweb-usermaint.timer
    - aurweb.service
  when: release.changed
