- name: Delete the currently existing ~/.zshenv
  file:
    path: '{{ zshenv_file }}'
    state: absent

- name: Produce the final ~/.zshenv
  shell: "cat {{ zshenv_dir }}/*.zsh > {{ zshenv_file }}"

- name: Clean up ~/.zshenv.d
  file:
    path: '{{ zshenv_dir }}'
    state: absent

- name: Delete the currently existing ~/.zshenv
  file:
    path: '{{ zshenv_file }}'
    state: absent

- name: Produce ~/.zshenv
  shell: "cat {{ zshenv_dir }}/*.zsh > {{ zshenv_file }}"

- name: Count the number of lines that are exactly 'export PATH'
  shell: "grep '^export PATH$' {{ zshenv_file }} | wc -l "
  register: path_export_count

- when: path_export_count != 0
  block:
    - name: Strip the 'export PATH' lines
      lineinfile:
        state: absent
        line: export PATH
        path: '{{ zshenv_file }}'
    - name: Add a final 'export PATH' line
      lineinfile:
        state: present
        line: "# Export PATH\nexport PATH"
        insertafter: EOF
        path: '{{ zshenv_file }}'

- name: Clean up ~/.zshenv.d
  file:
    path: '{{ zshenv_dir }}'
    state: absent

Clean up possibly duplicate 'export PATH' lines
Clean up possibly duplicate 'export PATH' lines
