---
- name: Windows | Create {{ install_path }} dir
  win_file:
    path: "{{ install_path }}"
    state: directory

- name: Windows | Check if the service wrapper exists
  win_stat:
    path: "{{ install_path }}/servicewrapper.exe"
  register: servicewrapper_info

- name: Windows | Get the service wrapper
  block:
    - name: Windows | Download the service wrapper zip file
      win_get_url:
        url: "{{ service_wrapper_link }}"
        dest: "{{ install_path }}/service-wrapper.zip"
        timeout: 60
      retries: 3

    - name: Windows | Unzip the service-wrapper zip archive
      win_unzip:
        src: "{{ install_path }}/service-wrapper.zip"
        dest: "{{ install_path }}/service-wrapper"

    - name: Windows | Move the service wrapper to the expected location
      win_copy:
        src: "{{ install_path }}/service-wrapper/service-wrapper.exe"
        dest: "{{ install_path }}/servicewrapper.exe"
        remote_src: yes

    - name: Windows | Remove service-wrapper temporary files
      win_file:
        state: absent
        path: "{{ item }}"
      with_items:
        - "{{ install_path }}/service-wrapper.zip"
        - "{{ install_path }}/service-wrapper"
  when: not servicewrapper_info.stat.exists

- name: Windows | Get the PSTools from sysinternals (used for SDN setup later on)
  block:
    - name: Windows | Remove existing PSTools
      win_file:
        state: absent
        path: "{{ item }}"
      with_items:
        - "{{ install_path }}/PSTools.zip"
        - "{{ install_path }}/PSTools"

    - name: Windows | Fetch PSTools.zip from {{ ps_tools_link }}
      win_get_url:
        url: "{{ ps_tools_link }}"
        dest: "{{ install_path }}/PSTools.zip"
      retries: 10
      delay: 5

    - name: Windows | Unzip PSTools.zip archive
      win_unzip:
        src: "{{ install_path }}/PSTools.zip"
        dest: "{{ install_path }}/PSTools"

    - name: Windows | Remove PSTools.zip archive
      win_file:
        state: absent
        path: "{{ install_path }}/PSTools.zip"
