---

- become: yes
  block:

    - name: "Install gitolite"
      apt:
        name: 'gitolite3'
        state: 'present'

      # https://docs.ansible.com/ansible/latest/user_guide/become.html#risks-of-becoming-an-unprivileged-user
    - name: "Install acl"
      apt:
        name: 'acl'
        state: 'present'

    - name: "Create gitolite user '{{ gitolite_user_name }}'"
      user:
        name: "{{ gitolite_user_name }}"
        home: "{{ gitolite_user_home }}"
        shell: "/bin/bash"

- become: yes
  become_user: "{{ gitolite_user_name }}"
  vars:
    default_repos:
      - "gitolite-admin"
      - "testing"
  block:

    - name: "Run dummy setup"
      command:
        cmd: "gitolite setup -a dummy"
        creates: "{{ gitolite_user_home }}/.gitolite"

    - name: "Remove directories of default repos"
      file:
        path: "{{ gitolite_user_home }}/repositories/{{ item }}.git"
        state: 'absent'
      notify: 'gitolite compile'
      loop: "{{ default_repos }}"

    - name: "Remove config of default repos"
      replace:
        path: "{{ gitolite_user_home }}/.gitolite/conf/gitolite.conf"
        regexp: '^[ \t\n]*repo[ \t]+{{ item }}((?!^repo)(.|\n))*'
      notify: 'gitolite compile'
      loop: "{{ default_repos }}"

    - name: "Create keydir"
      file:
        path: "{{ gitolite_user_home }}/.gitolite/keydir"
        state: 'directory'
      notify: 'gitolite compile'