---
-
  name: "Creating VMWare Horizon installation directories"
  ansible.builtin.file:
    mode: '0644'
    path: "{{ item }}"
    state: directory
  with_items:
    - "{{ tmp_dir }}"
    - "{{ tmp_dir }}/packages"

-
  name: "Upgrading all apt packages"
  ansible.builtin.apt:
    force_apt_get: true
    upgrade: dist

-
  name: "Downloading VMWare Horizon Client for Linux"
  ansible.builtin.get_url:
    mode: '0755'
    url: "{{ horizon_url }}"
    dest: "/tmp/{{ horizon_archive }}"

-
  name: "Extracting VMWare Horizon Packages for Linux"
  ansible.builtin.unarchive:
    src: "/tmp/{{ horizon_archive }}/"
    dest: "{{ tmp_dir }}"

-
  name: "Finding which directory was extracted for VMWare Horizon Installers"
  ansible.builtin.find:
    paths: "{{ tmp_dir }}"
    file_type: directory
    excludes: "packages"
  register: package_archive_dir

-
  name: "Setting Extracted VMWare Horizon Directory Variable"
  set_fact: package_extracted_dir="{{ package_archive_dir.files | map(attribute='path') | join('') }}"

-
  name: "Showing Extracted VMWare Horizon Directory"
  ansible.builtin.debug:
    msg: "{{ package_extracted_dir }}"

-
  name: "Finding VMWare Horizon Packages (tar.gz) Archives"
  ansible.builtin.find:
    paths: "{{ package_extracted_dir }}/armhf/"
    patterns: '*.tar.gz'
  register: package_archives

-
  name: "Extracting VMWare Horizon Packages for Linux"
  ansible.builtin.unarchive:
    src: "{{ item.path }}"
    dest: "{{ tmp_dir }}/packages"
  with_items: "{{ package_archives.files }}"

-
  name: "Finding VMWare Horizon .armhf folders"
  ansible.builtin.find:
    paths: "{{ tmp_dir }}/packages"
    patterns: '*.armhf'
    file_type: directory
    excludes: "*SessionSDK*,*ClientSDK*,*-sdk-*"
  register: extracted_package_archives

-
  name: "Setting Variable for the Package Dir for VMWare Horizon package files"
  ansible.builtin.set_fact: packages_extracted_dirs="{{ extracted_package_archives.files | map(attribute='path') | list  }}"

-
  name: "Displaying Extracted VMWare Horizon binary directories"
  ansible.builtin.debug:
    msg: "{{ packages_extracted_dirs }}"

-
  name: "Registering VMWare USB Files"
  ansible.builtin.set_fact:
    usb_path: "{{ packages_extracted_dirs|
                   select('regex', my_regex)|
                   first|
                   regex_replace(my_regex, my_replace) }}"
  vars:
    my_regex: '(.*USB.*)$'
    my_replace: '\1'

-
  name: "Registering VMWare Client Files"
  ansible.builtin.set_fact:
    client_path: "{{ packages_extracted_dirs|
                   select('regex', my_regex)|
                   first|
                   regex_replace(my_regex, my_replace) }}"
  vars:
    my_regex: '(.*Client.*)$'
    my_replace: '\1'

-
  name: "Registering VMWare PCOIP Files"
  ansible.builtin.set_fact:
    pcoip_path: "{{ packages_extracted_dirs|
                   select('regex', my_regex)|
                   first|
                   regex_replace(my_regex, my_replace) }}"
  vars:
    my_regex: '(.*PCoIP.*)$'
    my_replace: '\1'

-
  name: "Displaying Extracted VMWare Horizon binary directories"
  ansible.builtin.debug:
    msg: "{{ item }}"
  with_items:
    - "{{ pcoip_path }}"
    - "{{ usb_path }}"
    - "{{ client_path }}"

-
  name: "Copying VMWare Horizon Client Libraries, Binaries, Documents and Locales"
  ansible.posix.synchronize:
    dest: "{{ item.dest }}"
    src: "{{ item.path }}"
    recursive: true
  with_items:
    -
      path: "{{ client_path }}/usr/bin/"
      dest: "/usr/bin/"
    -
      path: "{{ client_path }}/usr/lib/"
      dest: "/usr/lib/"
    -
      path: "{{ client_path }}/usr/share/doc/"
      dest: "/usr/share/doc/"
    -
      path: "{{ client_path }}/usr/share/locale/"
      dest: "/usr/share/locale/"

-
  name: "Copying VMWare Horizon PCoIP Libraries, Binaries, Documents and Locales"
  ansible.posix.synchronize:
    dest: "{{ item.dest }}"
    src: "{{ item.path }}"
    recursive: true
  with_items:
    -
      path: "{{ pcoip_path }}/usr/lib/vmware/view/client/"
      dest: "/usr/bin/"
    -
      path: "{{ pcoip_path }}/usr/lib/"
      dest: "/usr/lib/"

-
  name: "Copying VMWare Horizon USB Libraries, Binaries, Documents and Locales"
  ansible.posix.synchronize:
    dest: "{{ item.dest }}"
    src: "{{ item.path }}"
    recursive: true
  with_items:
    -
      path: "{{ usb_path }}/bin/"
      dest: "/usr/bin/"
    -
      path: "{{ usb_path }}/lib/"
      dest: "/usr/lib/"
    -
      path: "{{ usb_path }}/init.d/"
      dest: "/etc/init.d/"

-
  name: "Creating Symbolic links for VMWare Horizon USB Arbritrator"
  ansible.builtin.file:
    src: /usr/bin/vmware-usbarbitrator
    dest: /usr/lib/vmware/view/usb/vmware-usbarbitrator
    state: link

-
  name: "Creating Symbolic links for VMWare Horizon USB Loader"
  ansible.builtin.file:
    src: /usr/bin/vmware-view-usbdloader
    dest: /usr/lib/vmware/view/usb/vmware-view-usbdloader
    state: link

-
  name: "Creating Symbolic links for VMWare Horizon USB Arbritrator rc5 init.d scripts"
  ansible.builtin.file:
    src: /etc/init.d/vmware-USBArbitrator
    dest: /etc/rc5.d/S50vmware-USBArbitrator
    state: link

-
  name: "Creating Symbolic links for VMWare Horizon USB Arbritrator rc6 init.d scripts"
  ansible.builtin.file:
    src: /etc/init.d/vmware-USBArbitrator
    dest: /etc/rc6.d/K08vmware-USBArbitrator
    state: link

-
  name: "Copying Generic Horizon View Icon"
  ansible.builtin.copy:
    mode: '0644'
    dest: "{{ item.dest }}"
    src: "{{ item.path }}"
  with_items:
    -
      path: "vmware-view.png"
      dest: "/usr/share/icons/vmware-view.png"

-
  name: "Creating Horizon View General Shortcut"
  ansible.builtin.copy:
    content: |
      [Desktop Entry]
      Name=Horizon Client
      GenericName=Horizon Client
      Comment=Horizon Client
      Exec=/usr/bin/vmware-view
      Icon=/usr/share/icons/vmware-view.png
      Categories=Office;
      Terminal=false
      Type=Application
    dest: "/usr/share/applications/vmware-view.desktop"
    force: false
    mode: '0644'
    
-
  name: "Starting vmware-USBArbitrator"
  ansible.builtin.service:
    name: vmware-USBArbitrator
    state: started
    enabled: true

-
  name: "Deleting Leftovers"
  ansible.builtin.file:
    path: "{{ item }}"
    state: absent
  with_items:
    - "{{ tmp_dir }}"

-
  name: "Adding ARMHF as VMWare Horizon Only runs on ARMHF, i386 or x64"
  ansible.builtin.command: dpkg --add-architecture armhf

-
  name: "Ensuring apt cache is updated"
  ansible.builtin.apt:
    cache_valid_time: 3600
    update_cache: true  

-
  name: "Upgrade all apt packages"
  ansible.builtin.apt:
    force_apt_get: true
    update_cache: true
    upgrade: dist

-
  name: "Installing armhf dependencies for VMWare Horizon"
  ansible.builtin.apt:
    name: "{{ horizon_view_depenencies }}"
    update_cache: true
    state: present

- 
  name: "Adding ARMHF as VMWare Horizon Only runs on ARMHF, i386 or x64"
  ansible.builtin.file:
    src: /lib/ld-linux-armhf.so.3
    dest: /lib/ld-linux.so.3
    state: link

-
  name: "Pinning LibuDev for VMWare"
  ansible.builtin.dpkg_selections:
    name: "{{ item }}"
    selection: hold
  loop:
    - libgcc1:armhf
    - libc6:armhf
    - libatk-bridge2.0-0:armhf
    - libpangocairo-1.0-0:armhf
    - libgdk-pixbuf2.0-0:armhf
    - libgtk-3-0:armhf
    - libudev0:armhf
    - libxss1:armhf  
    - libatkmm-1.6-1v5:armhf
    - libcairomm-1.0-1v5:armhf
    - libxkbfile1:armhf
    - libxtst6:armhf
    - packagekit-gtk3-module:armhf
    - libpulse0:armhf
    - libgtkmm-3.0-1v5:armhf
    - libmagic-dev:armhf
    - uuid-dev:armhf