---
- name: "Ensure {{ app_name }}'s dependencies are installed"
  package:
    name: "{{ omz_linux_dependencies }}"
    state: present
    update_cache: true
  when: ansible_system == 'Linux'

- name: Ensure Oh My Zsh src folder exists
  file:
    mode: 0755
    path: "{{ omz_destination }}"
    state: directory
  when: not install_prezto

- name: Ensure Oh My Zsh installer script is downloaded
  get_url:
    url: https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
    dest: "{{ omz_destination }}/install.sh"
    mode: 0755
    force: true
  register: omz_installer
  when: not install_prezto

- name: "Ensure older version of {{ app_name }} is uninstalled" # noqa 503
  file:
    path: "{{ omz_destination }}/oh-my-zsh"
    state: absent
  when:
    - not install_prezto
    - omz_installer.changed

- name: "Ensure {{ app_name }} is installed" # noqa 503
  environment:
    ZSH: "{{ omz_destination }}/oh-my-zsh"
    CHSH: "{{ 'yes' if (switch_to_zsh) else 'no' }}"
    RUNZSH: "no"
  command: ./install.sh
  args:
    chdir: "{{ omz_destination }}"
    creates: "{{ omz_destination }}/oh-my-zsh"
  when:
    - not install_prezto
    - omz_installer.changed

- name: Run Antigen installation tasks
  include_tasks: antigen-Linux.yml
  when: ansible_os_family not in ('Darwin', 'Debian')

- name: Ensure ~/.zshrc is setup
  become_user: "{{ user.username }}"
  template:
    src: zshrc.j2
    dest: ~/.zshrc
    mode: 0700
    backup: true
  loop: "{{ user_configs }}"
  loop_control:
    label: "{{ user.username }}"
    loop_var: user
  when:
    - not install_prezto
    - (user.system is not defined) or ((user.system is defined) and (not user.system))
