---
- name: "Ensure {{ app_name }} is installed"
  community.general.npm:
    name: appium
    global: true
    state: latest

# Appium Desktop Installation
- name: Ensure Megabyte Labs configuration directory exists
  file:
    mode: 0700
    path: ~/.config/megabytelabs
    state: directory

- name: "Check if {{ app_name }} Desktop has configuration stored in ~/.config/megabytelabs/appiumdesktop"
  stat:
    path: ~/.config/megabytelabs/appiumdesktop
  register: appiumdesktop_config

- name: "Detect previously installed {{ app_name }} Desktop version"
  command: cat appiumdesktop
  args:
    chdir: ~/.config/megabytelabs
  changed_when: false
  register: current_appiumdesktop_version
  when: appiumdesktop_config.stat.exists

- name: "Detect the latest {{ app_name }} Desktop version"
  uri:
    url: https://api.github.com/repos/appium/appium-desktop/releases/latest
  register: appiumdesktop_latest_release_tag

- name: "Determine whether or not the latest version of {{ app_name }} Desktop's AppImage is already installed"
  set_fact:
    install_appiumdesktop: "{{ (current_appiumdesktop_version.skipped | default('false')) or \
      ((not current_appiumdesktop_version.skipped | default('false')) and \
      (current_appiumdesktop_version.stdout != appiumdesktop_latest_release_tag.json.tag_name | replace('v',''))) }}"

- name: "Acquire the latest {{ app_name }} Desktop AppImage"
  get_url:
    url: "https://github.com/appium/appium-desktop/releases/download/{{ appiumdesktop_latest_release_tag.json.tag_name }}/\
      Appium-Server-GUI-linux-{{ appiumdesktop_latest_release_tag.json.tag_name | replace('v','') }}.AppImage"
    dest: "{{ appiumdesktop_src_path }}/Appium-Server-GUI.AppImage"
    mode: 0755
  when: install_appiumdesktop

- name: Run user configuration tasks
  include_tasks: user-Linux.yml
  vars:
    appimage_name: Appium-Server-GUI.AppImage
  loop: "{{ user_configs }}"
  loop_control:
    label: "{{ user.username }}"
    loop_var: user
  when:
    - install_appiumdesktop
    - (user.system is not defined) or ((user.system is defined) and (not user.system))

- name: "Ensure {{ app_name }} Desktop AppImage is removed from /usr/local/bin"
  file:
    path: "{{ appiumdesktop_src_path }}/Appium-Server-GUI.AppImage"
    state: absent
  when: install_appiumdesktop

- name: "Save meta information about the version of {{ app_name }} Desktop that was installed"
  copy:
    dest: ~/.config/megabytelabs/appiumdesktop
    mode: 0600
    content: |
      {{ appiumdesktop_latest_release_tag.json.tag_name | replace('v','') }}
  when: install_appiumdesktop

# Appium Inspector Installation
- name: "Check if Appium Inspector has configuration stored in ~/.config/megabytelabs/appium_inspector"
  stat:
    path: ~/.config/megabytelabs/appium_inspector
  register: appium_inspector_config

- name: "Detect previously installed Appium Inspector version"
  command: cat appium_inspector
  args:
    chdir: ~/.config/megabytelabs
  changed_when: false
  register: current_appium_inspector_version
  when: appium_inspector_config.stat.exists

- name: "Detect the latest Appium Inspector version"
  uri:
    url: https://api.github.com/repos/appium/appium-inspector/releases/latest
  register: appium_inspector_latest_release_tag

- name: "Determine whether or not the latest version of Appium Inspector's AppImage is already installed"
  set_fact:
    install_appium_inspector: "{{ (current_appium_inspector_version.skipped | default('false')) or \
      ((not current_appium_inspector_version.skipped | default('false')) and \
      (current_appium_inspector_version.stdout != appium_inspector_latest_release_tag.json.tag_name | replace('v',''))) }}"

- name: "Acquire the latest Appium Inspector AppImage"
  get_url:
    url: "https://github.com/appium/appium-inspector/releases/download/{{ appium_inspector_latest_release_tag.json.tag_name }}/\
      Appium-Inspector-linux-{{ appium_inspector_latest_release_tag.json.tag_name | replace('v','') }}.AppImage"
    dest: "{{ appiumdesktop_src_path }}/Appium-Inspector.AppImage"
    mode: 0755
  when: install_appium_inspector

- name: Run user configuration tasks
  include_tasks: user-Linux.yml
  vars:
    appimage_name: Appium-Inspector.AppImage
  loop: "{{ user_configs }}"
  loop_control:
    label: "{{ user.username }}"
    loop_var: user
  when:
    - install_appium_inspector
    - (user.system is not defined) or ((user.system is defined) and (not user.system))

- name: "Ensure Appium Inspector AppImage is removed from /usr/local/bin"
  file:
    path: "{{ appiumdesktop_src_path }}/Appium-Inspector.AppImage"
    state: absent
  when: install_appium_inspector

- name: "Save meta information about the version of Appium Inspector that was installed"
  copy:
    dest: ~/.config/megabytelabs/appium_inspector
    mode: 0600
    content: |
      {{ appium_inspector_latest_release_tag.json.tag_name | replace('v','') }}
  when: install_appium_inspector
