---
- name: Ensure Megabyte Labs configuration directory exists
  ansible.windows.win_file:
    path: "{{ item }}"
    state: directory
  loop:
    - '%USERPROFILE%\.config'
    - '%USERPROFILE%\.config\megabytelabs'

- name: "Ensure {{ slackterm_goroot }} directory exists"
  ansible.windows.win_file:
    path: "{{ slackterm_goroot }}"
    state: directory

- name: "Check if {{ app_name }} has configuration stored in '%USERPROFILE%/.config/megabytelabs/slackterm'"
  ansible.windows.win_stat:
    path: '%USERPROFILE%\.config\megabytelabs\slackterm'
  register: slackterm_config

- name: "Detect previously installed {{ app_name }} version"
  ansible.windows.win_shell: type slackterm
  args:
    chdir: '%USERPROFILE%\.config\megabytelabs'
  changed_when: false
  register: current_slackterm_version
  when: slackterm_config.stat.exists

- name: "Detect the latest {{ app_name }} version"
  ansible.windows.win_uri:
    url: https://api.github.com/repos/erroneousboat/slack-term/releases/latest
    return_content: true
  register: slackterm_latest_release_tag

- name: "Determine whether or not the latest version of {{ app_name }} is already installed"
  set_fact:
    install_slackterm: "{{ (current_slackterm_version.skipped | default(false)) or \
      ((not current_slackterm_version.skipped | default(false)) and \
      (current_slackterm_version.stdout | trim != slackterm_latest_release_tag.json.tag_name | replace('v',''))) }}"

- name: "Check if {{ app_name }}'s source is present"
  ansible.windows.win_stat:
    path: "{{ slackterm_src_dir }}\\.git"
  register: slackterm_stat

- name: "Ensure {{ app_name }}'s source is cloned"
  ansible.windows.win_command: git clone https://github.com/erroneousboat/slack-term slackterm
  args:
    chdir: "{{ slackterm_src_dir | win_dirname }}"
  when: not slackterm_stat.stat.exists

- name: "Ensure {{ app_name }}'s source is up-to-date"
  ansible.windows.win_command: git pull
  args:
    chdir: "{{ slackterm_src_dir }}"
  when:
    - slackterm_stat.stat.exists
    - install_slackterm
  register: slackterm_pull
  changed_when: not (slackterm_pull.rc == 0 and ('Already up to date' in slackterm_pull.stdout))

- name: "Ensure {{ app_name }} is installed" # noqa 503
  ansible.windows.win_command: go install .
  args:
    chdir: "{{ slackterm_src_dir }}"
  environment:
    GOPATH: "{{ slackterm_goroot }}"
  when: install_slackterm

- name: "Ensure {{ app_name }} installation is added to PATH"
  ansible.windows.win_path:
    elements: "{{ slackterm_goroot }}\\bin"
    state: present

- name: Run user configuration tasks
  include_tasks: user-Windows.yml
  loop: "{{ user_configs }}"
  loop_control:
    label: "{{ user.username }}"
    loop_var: user
  when:
    - (user.slack_api_key is defined and user.slack_api_key)
    - (user.system is not defined) or ((user.system is defined) and (not user.system))

- name: "Save meta information about the version of {{ app_name }} that was installed"
  ansible.windows.win_copy:
    dest: '%USERPROFILE%\.config\megabytelabs\slackterm'
    content: |
      {{ slackterm_latest_release_tag.json.tag_name | replace('v','') }}
  when: install_slackterm
