- include_role:
    name: per_user
    apply:
      become: yes
      become_user: "{{ build_user.name }}"

- name: Install packages
  package:
    name:
      - base-devel
      - gnupg
      - devtools # Creates /usr/share/devtools/makepkg-x86_64.conf and provides mkarchroot
      - base-devel
      - vifm # For aurutils
      - vim # For aurutils
      - expect # For aurutils
      - pacman-contrib # For paccache for aurutils-purge
    state: present

- name: Extract aurutils aur entry
  become_user: "{{ build_user.name }}"
  unarchive:
    src: https://aur.archlinux.org/cgit/aur.git/snapshot/aurutils.tar.gz
    dest: "/home/{{ build_user.name }}/.config/ansible_deploy/"
    remote_src: yes
    creates: "/home/{{ build_user.name }}/.config/ansible_deploy/aurutils"

- name: Check trusted gpg keys
  command: pacman-key -l
  register: pacmankeys
  changed_when: false
  check_mode: no
  no_log: true

- name: Trust the signing key for the aurutils package
  command: pacman-key --recv-keys DBE7D3DD8C81D58D0A13D0E76BC26A17B9B7018A
  when: '"DBE7D3DD8C81D58D0A13D0E76BC26A17B9B7018A" not in pacmankeys.stdout'

- name: Trust my signing key
  command: pacman-key -a -
  args:
    stdin: "{{ lookup('file', 'gpg.asc') }}"
  when: '"B848159363C2877917954BE127DE40D9B8455C1B" not in pacmankeys.stdout'
  register: my_key_imported

- name: Sign my signing key
  command: pacman-key --lsign-key "B848159363C2877917954BE127DE40D9B8455C1B"
  when: my_key_imported.changed

- name: Build the aurutils package
  become_user: "{{ build_user.name }}"
  command: makepkg -s --noconfirm
  args:
    chdir: "/home/{{ build_user.name }}/.config/ansible_deploy/aurutils"
    creates: "/home/{{ build_user.name }}/.config/ansible_deploy/aurutils/aurutils-*-any.pkg.tar.*"

- name: Install aurutils
  shell: "pacman --noconfirm -U /home/{{ build_user.name }}/.config/ansible_deploy/aurutils/aurutils-*-any.pkg.tar.*"
  args:
    creates: /usr/bin/aur

- name: Create owned directories
  file:
    name: "{{ item }}"
    state: directory
    owner: "{{ build_user.name }}"
    group: "{{ build_user.group }}"
    mode: 0755
  loop:
    - /var/cache/pacman/custom

- name: Create directories
  file:
    name: "{{ item }}"
    state: directory
    owner: root
    group: root
    mode: 0755
  loop:
    - /etc/aurutils

- name: Install configs
  copy:
    src: "files/{{ item.src }}"
    dest: "{{ item.dest }}"
    mode: 0644
    owner: root
    group: root
  loop:
    - src: aurutils.conf
      dest: /etc/pacman.d/conf.d/
    - src: pacman-custom.conf
      dest: /etc/aurutils/
    - src: makepkg.conf # TODO: Is this needed or can I use the default from devtools?
      dest: /etc/aurutils/

- name: Create custom repo db
  command: repo-add --sign /var/cache/pacman/custom/custom.db.tar
  become: true
  become_user: "{{ build_user.name }}"
  args:
    creates: /var/cache/pacman/custom/custom.db.tar

- name: Install scripts
  copy:
    src: "files/{{ item.src }}"
    dest: "{{ item.dest }}"
    mode: 0755
    owner: root
    group: root
  loop:
    - src: aurutils-purge
      dest: /usr/local/bin/aurutils-purge
    - src: aurutils-sync
      dest: /usr/local/bin/aurutils-sync
    - src: aurutils-update-devel-packages
      dest: /usr/local/bin/

- name: build aurutils inside aurutils
  become_user: "{{ build_user.name }}"
  command: aurutils-sync --no-view aurutils
  args:
    creates: "/var/cache/pacman/custom/aurutils-*-any.pkg.tar.*"
  register: buildaurutils

- name: Install aurutils
  command: pacman -Syy aurutils
  when: buildaurutils.changed

- name: chown the custom package db
  file:
    path: "{{ item }}"
    owner: "{{ build_user.name }}"
    state: file
  loop:
    - /var/cache/pacman/custom/custom.db