---
- name: Acquiring home of current user
  when: ansible_facts["system"] == "Linux"
  ansible.builtin.shell:
    cmd: "echo ~{{ ansible_user }}"
  register: ansible_user_home
- name: Find  all NVM pre-built binary (sub)directories
  ansible.builtin.find:
    paths: "{{ ansible_user_home.stdout }}/downloads/archives/released/nvm"
    patterns: "^node-v[0-9]*\\."
    file_type: directory
    use_regex: true
  register: prebuilt_nodes
- name: Bootstrapping NVM
  become: true
  block:
    - name: Grabbing only path information for pre-built binary (sub)directories
      ansible.builtin.set_fact:
        prebuilt_nodepaths: "{{ prebuilt_nodes.files | map(attribute='path') }}"
    - name: Linking binaries to directories already in PATH environment variable
      ignore_errors: true
      ansible.builtin.file:
        src: "{{ item[0] }}/bin/{{ item[1] }}"
        dest: "/usr/bin/{{ item[1] }}"
        state: link
      loop: "{{ prebuilt_nodepaths | product(['node']) }}"
    - name: Linking binaries to directories already in PATH environment variable
      ignore_errors: true
      ansible.builtin.file:
        src: "{{ item[0] }}/lib/node_modules/corepack/dist/{{ item[1] }}.js"
        dest: "/usr/bin/{{ item[1] }}"
        state: link
      loop: "{{ prebuilt_nodepaths | product(['corepack']) }}"
    - name: Linking binaries to directories already in PATH environment variable
      ignore_errors: true
      ansible.builtin.file:
        src: "{{ item[0] }}/lib/node_modules/npm/bin/{{ item[1] }}-cli.js"
        dest: "/usr/bin/{{ item[1] }}"
        state: link
      loop: "{{ prebuilt_nodepaths | product(['npm','npx']) }}"
    - name: Link includes to directories already recognized by system
      ignore_errors: true
      ansible.builtin.file:
        src: "{{ item[0] }}/include/{{ item[1] }}"
        dest: "/usr/include/{{ item[1] }}"
        state: link
      loop: "{{ prebuilt_nodepaths | product(['node']) }}"
    - name: Link requisite libraries to directories already recognized by system
      ignore_errors: true
      ansible.builtin.file:
        src: "{{ item[0] }}/lib/{{ item[1] }}"
        dest: "/usr/lib/{{ item[1] }}"
        state: link
      loop: "{{ prebuilt_nodepaths | product(['node_modules']) }}"
    - name: Create man1 subdirectory for man pages
      ansible.builtin.file:
        path: /usr/share/man/man1
        recurse: true
        owner: root
        group: root
        mode: "644"
        state: directory
    - name: Link shared resources to directories already recognized by system
      ignore_errors: true
      ansible.builtin.file:
        src: "{{ item[0] }}/share/man/man1/{{ item[1] }}.1"
        dest: "/usr/share/man/man1/{{ item[1] }}.1"
        state: link
      loop: "{{ prebuilt_nodepaths | product(['node']) }}"
    - name: Create man1 subdirectory for man pages
      ansible.builtin.file:
        path: /usr/share/doc
        recurse: true
        owner: root
        group: root
        mode: "644"
        state: directory
    - name: Link shared resources to directories already recognized by system
      ignore_errors: true
      ansible.builtin.file:
        src: "{{ item[0] }}/share/doc/{{ item[1] }}"
        dest: "/usr/share/doc/{{ item[1] }}"
        state: link
      loop: "{{ prebuilt_nodepaths | product(['node']) }}"
# - name: Reboot machine for shell environment change
#   ansible.builtin.reboot:
#     msg: Rebooting machine

