---
- name: gnupg | Create gnupg directory
  file:
    path: '{{ xdg_dir.data_home }}/gnupg'
    state: directory
    mode: '700'

- name: gnupg | Copy config
  template:
    src: 'gnupg/{{ item }}.j2'
    dest: '{{ xdg_dir.data_home }}/gnupg/{{ item }}.conf'
    mode: '600'
  loop:
    - gpg
    - gpg-agent

- name: gnupg | Update SSH config
  when: ssh_client == 'openssh'
  block:
    - name: gnupg | Ensure ~/.ssh directory exists
      file:
        path: ~/.ssh
        mode: '700'
        state: directory

    - name: gnupg | Configure ssh to update TTY value for GPG on each run
      lineinfile:
        path: ~/.ssh/config
        mode: '644'
        create: true
        search_string: gpg-connect-agent UPDATESTARTUPTTY
        line: Match host * exec "gpg-connect-agent UPDATESTARTUPTTY /bye"
        insertbefore: BOF
