---
#########
# Greenfoot und BlueJ. 

- name: Determine installed bluej version
  shell: "dpkg -l bluej 2> /dev/null | grep ii | awk '{print $3}'"
  register: command_output
- set_fact:
    bluej_installed_version: "{{ command_output.stdout }}"

- name: Determine installed greenfoot version
  shell: "dpkg -l greenfoot 2> /dev/null | grep ii | awk '{print $3}'"
  register: command_output
- set_fact:
    greenfoot_installed_version: "{{ command_output.stdout }}"

- name: BlueJ URI
  ansible.builtin.debug:
    msg: "{{ mirror_javadev }}/bluej-{{ bluej_target_version }}.deb" 
  
- name: Install bluej if necessary
  ansible.builtin.apt:
    deb: "{{ mirror_javadev }}/bluej-{{ bluej_target_version }}.deb" 
  when: (mirror_javadev is defined and mirror_javadev | length > 0) and
        (bluej_target_version is defined and bluej_target_version | length > 0 ) and
        (bluej_installed_version != bluej_target_version )

- name: Install greenfoot if necessary
  ansible.builtin.apt:
    deb: "{{ mirror_javadev }}/greenfoot-{{ greenfoot_target_version }}.deb" 
  when: (mirror_javadev is defined and mirror_javadev | length > 0) and
        (greenfoot_target_version is defined and greenfoot_target_version | length > 0 ) and
        (greenfoot_installed_version != greenfoot_target_version )

########
# FreeCAD AppImage
#
- name: Make sure target dir exists
  ansible.builtin.file:
    path: /opt/appimages/freecad/
    state: directory
    recurse: yes
    owner: root
    group: root
    mode: '0755'

- name: Download FreeCAD AppImage from Cache
  ansible.builtin.get_url:
    url: "{{ mirror_appimage }}/FreeCAD.AppImage"
    dest: /opt/appimages/freecad/FreeCAD.AppImage
    mode: '0755'
  environment:
    http_proxy: ''

- name: Copy desktop freecad starter file to target
  copy: 
    src: files/qg.appimage.FreeCAD.desktop   
    dest: /usr/share/applications/qg.appimage.FreeCAD.desktop
    mode: '0644'
    owner: root
    group: root

########
# Arduino2 AppImage
#
- name: Make sure target dir exists
  ansible.builtin.file:
    path: /opt/appimages/arduino2/
    state: directory
    recurse: yes
    owner: root
    group: root
    mode: '0755'

- name: Make sure lib dir for template exists
  ansible.builtin.file:
    path: /var/lib/arduino2
    state: directory
    recurse: yes
    owner: root
    group: root
    mode: '0755'

- name: Download Arduino2 AppImage from Cache
  ansible.builtin.get_url:
    url: "{{ mirror_appimage }}/ArduinoIDE2.AppImage"
    dest: /opt/appimages/arduino2/ArduinoIDE2.AppImage
    mode: '0755'
  environment:
    http_proxy: ''

- name: Copy desktop arduino2 starter file to target
  copy: 
    src: files/qg.appimage.ArduinoIDE2.desktop   
    dest: /usr/share/applications/qg.appimage.ArduinoIDE2.desktop
    mode: '0644'
    owner: root
    group: root

- name: Copy arduino Appimage start Script to target
  copy: 
    src: files/start-arduino2-appimage  
    dest: /opt/appimages/arduino2/start-arduino2-appimage
    mode: '0755'
    owner: root
    group: root

- name: Delete arduino15 template
  ansible.builtin.file:
    state: absent
    path: /var/lib/arduino2/template

- name: Extract up to date arduino15 template to target
  ansible.builtin.unarchive:
    src: "{{ mirror_appimage }}/ArduinoIDE2-template.zip"
    dest: /var/lib/arduino2/
    remote_src: yes
  environment:
    http_proxy: ''

- name: Make sure cachedir exists on laptops too
  ansible.builtin.file:
    path: /var/cache/user
    state: directory
    recurse: yes
    owner: root
    group: root
    mode: '0777'

########
# Prusa Slicer AppImage
#
- name: Make sure target dir exists
  ansible.builtin.file:
    path: /opt/appimages/prusaslicer/
    state: directory
    recurse: yes
    owner: root
    group: root
    mode: '0755'

- name: Download PrusaSlicer AppImage from Cache
  ansible.builtin.get_url:
    url: "{{ mirror_appimage }}/PrusaSlicer.AppImage"
    dest: /opt/appimages/prusaslicer/PrusaSlicer.AppImage
    mode: '0755'
  environment:
    http_proxy: ''

- name: Copy desktop PrusaSlicer starter file to target
  copy: 
    src: files/qg.appimage.PrusaSlicer.desktop   
    dest: /usr/share/applications/qg.appimage.PrusaSlicer.desktop
    mode: '0644'
    owner: root

- name: Copy desktop Prusaslicer icon file to target
  copy: 
    src: files/prusaslicer.png   
    dest: /usr/share/icons/prusaslicer.png
    mode: '0644'
    owner: root
    group: root
    group: root


########
# Logseq AppImage
#
- name: Make sure target dir for logseq exists
  ansible.builtin.file:
    path: /opt/appimages/logseq/
    state: directory
    recurse: yes
    owner: root
    group: root
    mode: '0755'

- name: Download logseq AppImage from Cache
  ansible.builtin.get_url:
    url: "{{ mirror_appimage }}/Logseq.AppImage"
    dest: /opt/appimages/logseq/Logseq.AppImage
    mode: '0755'
  environment:
    http_proxy: ''

- name: Copy desktop logseq starter file to target
  copy: 
    src: files/qg.appimage.Logseq.desktop   
    dest: /usr/share/applications/qg.appimage.Logseq.desktop
    mode: '0644'
    owner: root
    group: root

- name: Copy desktop logseq icon file to target
  copy: 
    src: files/logseq.png   
    dest: /usr/share/icons/logseq.png
    mode: '0644'
    owner: root
    group: root

########
# Digital als tarpack
#
- name: Unpack digital.zip to target
  ansible.builtin.unarchive:
    src: "{{ tarpack_digital }}"
    dest: /
    remote_src: yes
  when: tarpack_digital is defined and tarpack_digital | length > 0
  environment:
    http_proxy: ''


#######
# Scratch
- name: Install scratch
  ansible.builtin.apt:
    deb: "{{ mirror_debs }}/{{ scratch_install_name }}"
  environment:
    http_proxy: ''

