---
- name: Setup VMware Workstation facts
  set_fact:
    workstation_extra_packages: "{{ os_workstation_extra_packages }}"
  when:
    - workstation_extra_packages is undefined
    - os_workstation_extra_packages is defined

- name: Create temporary directory
  file:
    path: "{{ workstation_tempdir }}"
    state: directory
    mode: 0755

- name: Check for existing installation
  stat:
    path: "/usr/bin/vmware"
  register: file_stats

- name: Download workstation # noqa 303
  command: |
    curl -LA "Mozilla/5.0 (X11; Linux x86_64; rv:82.0) Gecko/20210101 Firefox/82.0" "{{ workstation_download_url }}" \
      -o "{{ workstation_tempdir }}/tryworkstation-linux-64.sh"
  args:
    creates: "{{ workstation_tempdir }}/tryworkstation-linux-64.sh"
  when: not file_stats.stat.exists

- name: Ensure Workstation installer is executable
  file:
    path: "{{ workstation_tempdir }}/tryworkstation-linux-64.sh"
    mode: 0755
  when: not file_stats.stat.exists

- name: Run installer
  become: true
  command: >
    {{ workstation_tempdir }}/tryworkstation-linux-64.sh --eulas-agreed --console
    --required {{ '--set-setting vmware-workstation serialNumber ' + workstation_license
    if workstation_license is defined else '' }}
  args:
    creates: /usr/bin/vmware
  when: not file_stats.stat.exists
  tags:
    - skip_ansible_lint

- name: Install packages needed by VMWare Packer build
  become: true
  package:
    name: "{{ item }}"
    state: present
  loop: "{{ workstation_extra_packages }}"
  when: workstation_extra_packages is defined

- name: Stat for systemd
  stat:
    path: /etc/systemd/system
  register: systemd_dir

- name: Install systemd services
  copy:
    src: "{{ item }}"
    dest: /etc/systemd/system/
    owner: root
    mode: 0644
  loop:
    - vmware.service
    - vmware-usbarbitrator.service
    - vmware-workstation-server.service
  notify: reload systemd configurations
  when: systemd_dir.stat.exists

- name: Clone Unlocker (for running Mac OS X on VMWare)
  git:
    depth: "1"
    dest: "{{ unlocker_temp_dir }}"
    force: true
    repo: "{{ unlocker_git }}"
    version: master

- name: Update python to python3 in lnx-install.sh
  lineinfile:
    path: "{{ unlocker_temp_dir }}/lnx-install.sh"
    regex: "python gettools.py"
    line: python3 gettools.py

- name: Patch VMWare for Mac OS X
  command: bash lnx-install.sh
  args:
    chdir: "{{ unlocker_temp_dir }}"
    creates: /usr/lib/vmware/isoimages/darwin.iso

- name: Remove the temporary Unlocker directory
  file:
    path: "{{ unlocker_temp_dir }}"
    state: absent
