---
- name: Acquiring home of current user
  when: ansible_facts["system"] == "Linux"
  ansible.builtin.shell:
    cmd: "echo ~{{ ansible_user }}"
  register: ansible_user_home
- name: Linking binaries to directories already in PATH environment variable
  become: true
  ignore_errors: true
  ansible.builtin.file:
    src: "{{ ansible_user_home.stdout }}/downloads/archives/released/surge/{{ item }}"
    dest: "/usr/bin/{{ item }}"
    state: link
  loop:
    - surge
- name: Starting to spin up the Surge service
  when: surge.service.enabled and not surge.containerized
  block:
    - name: Installing accompanying complementary Surge system service
      become: true
      ansible.builtin.command:
        cmd: surge service install
    - name: Creating corresponding SystemD service unit
      become: true
      ansible.builtin.copy:
        src: systemd/system/surge.service
        dest: /etc/systemd/system/surge.service
        owner: root
        group: root
        force: true
        backup: true
    - name: Acquiring API token for remote Surge service control
      become: true
      ansible.builtin.command:
        cmd: surge token
      register: surge_token
    - name: Pausing to inquire about how to proceed
      ansible.builtin.pause:
        prompt: "Type \"fetch\" to get the Surge API token, or \"show\" to see it for manual copying instead"
        echo: true
      register: data_method
    - name: Presenting Surge API token to Control Node
      when: data_method.user_input == "show"
      ansible.builtin.debug:
        msg: "Make sure to store the following API token for Surge:\n {{ surge_token.stdout }}"
    - name: Giving opportunity to manually copy Surge API token
      when: data_method.user_input == "show"
      ansible.builtin.pause:
    - name: Creating temporary file on managed node that stores Surge API token
      become: true
      when: data_method.user_input == "fetch"
      ansible.builtin.copy:
        content: "{{ surge_token.stdout }}"
        dest: /tmp/surge.secret
        owner: "{{ ansible_user }}"
        group: "{{ ansible_user }}"
        mode: "644"
      register: surge_token
    - name: Placing Surge API token into file on control node
      when: data_method.user_input == "fetch"
      ansible.builtin.fetch:
        src: "{{ surge_token.dest }}"
        dest: "./.tmp/{{ inventory_hostname }}-surge/surge.secret"
        flat: true
    - name: Placing Surge API token into file on control node
      when: data_method.user_input == "fetch"
      ansible.builtin.fetch:
        src: "{{ surge_token.dest }}"
        dest: "./roles/init-server/files/{{ inventory_hostname }}-surge/surge.secret"
        flat: true
    - name: Informing control node of acquired files
      when: data_method.user_input == "fetch"
      ansible.builtin.debug:
        msg: "The Surge API token file have been duplicated to './.tmp/{{ inventory_hostname }}-surge/surge.secret' at the control node."
    - name: Giving control node user time to read the aforementiioned message
      when: data_method.user_input == "fetch"
      ansible.builtin.pause:
        seconds: 30
    - name: Starting and enabling Surge SystemD service unit
      become: true
      ansible.builtin.systemd_service:
        name: surge
        scope: system
        state: started
        enabled: true
        daemon_reload: true