---
# Packages specific to workstations developing Flutter apps.

## Facts ##

- name: Workstation | Linux | Software | Flutter | Facts [1/4]
  set_fact:
    flutter_url: "https://storage.googleapis.com/flutter_infra_release/releases/stable/linux/flutter_linux_3.27.2-stable.tar.xz"
    flutter_download_file: "{{ user_user.home }}/Downloads/flutter.tar.xz"
    flutter_sdk_location: "{{ user_user.home }}/SDKs/Flutter"
    flutter_report: "{{ user_user.home }}/Reports/flutter.txt"
  when: coding == true

- name: Workstation | Linux | Software | Flutter | Facts [2/4]
  set_fact:
    flutter_bin_location: "{{ flutter_sdk_location }}/flutter/bin"
  when: coding == true

- name: Workstation | Linux | Software | Flutter | Facts [3/4]
  set_fact:
    flutter: "{{ flutter_bin_location }}/flutter"
  when: coding == true

- name: Workstation | Linux | Software | Flutter | Facts [3/3]
  set_fact:
    flutter_report_commands:
      - "date > {{ flutter_report }}"
      - "{{ flutter }} --disable-analytics >> {{ flutter_report }}"
      - "date >> {{ flutter_report }}"
      - "{{ flutter }} doctor -v >> {{ flutter_report }}"
      - "date >> {{ flutter_report }}"
  when: coding == true

## Checks ##

- name: Workstation | Linux | Software | Flutter | Check SDK Exists
  stat:
    path: "{{ flutter_sdk_location }}"
  register: flutter_sdk_stat
  when: coding == true

- name: Workstation | Linux | Software | Flutter | Check Download Exists
  stat:
    path: "{{ flutter_download_file }}"
  register: flutter_download_stat
  when: coding == true

## Packages ##

- name: Workstation | Linux | Software | Flutter | Necessities
  package:
    name:
    - curl
    - git
    - unzip
    - xz-utils
    - zip
    - libglu1-mesa
    state: present
  when: coding == true

# https://docs.flutter.dev/get-started/install/linux/desktop
- name: Workstation | Linux | Software | Flutter | Linux App Dependencies
  package:
    name:
    - clang
    - cmake
    - git
    - ninja-build
    - pkg-config
    - libgtk-3-dev
    - liblzma-dev
    - libstdc++-12-dev
    state: present
  when: coding == true

# https://docs.flutter.dev/get-started/install/linux/android
- name: Workstation | Linux | Software | Flutter | Android App Dependencies
  package:
    name:
      - libc6:amd64
      - libstdc++6
      - lib32z1
      - libbz2-1.0
    state: present
  when: coding == true

## Install SDK ##

- name: Workstation | Linux | Software | Flutter | SDK
  block:

  - name: Workstation | Linux | Software | Flutter | Download SDK
    get_url:
      url: "{{ flutter_url }}"
      dest: "{{ flutter_download_file }}"
      owner: "{{ user }}"
      group: "{{ user }}"
      mode: '0664'
    when: not flutter_download_stat.stat.exists

  - name: Workstation | Linux | Software | Flutter | Create Folder
    file:
      path: "{{ flutter_sdk_location }}"
      state: directory
      owner: "{{ user }}"
      group: "{{ user }}"
      mode: '0755'

  - name: Workstation | Linux | Software | Flutter | Extract SDK
    unarchive:
      src: "{{ flutter_download_file }}"
      dest: "{{ flutter_sdk_location }}"
      owner: "{{ user }}"
      group: "{{ user }}"

  become_user: "{{ user }}"
  when: coding == true and not flutter_sdk_stat.stat.exists

- name: Workstation | Linux | Software | Flutter | Delete Archive
  file:
    path: "{{ flutter_download_file }}"
    state: absent

## Configure Environment ##

- name: Workstation | Linux | Software | Flutter | Modify PATH (.bashrc)
  blockinfile:
    path: "{{ item }}/.bashrc"
    block: |
      export PATH="$PATH:{{ flutter_bin_location }}"
    marker: '# {mark} MANAGED BY ANSIBLE | Flutter'
    state: present
    create: yes
    backup: yes
  loop:
    - "{{ user_root.home }}"
    - "{{ user_user.home }}"
  ignore_errors: yes
  when: coding == true and user_root.home != "" and user_user.home != ""

- name: Workstation | Linux | Software | Flutter | Modify PATH (.zshrc)
  blockinfile:
    path: "{{ item }}/.zshrc"
    block: |
      export PATH="$PATH:{{ flutter_bin_location }}"
    marker: '# {mark} MANAGED BY ANSIBLE | Flutter'
    state: present
    create: yes
    backup: yes
  loop:
    - "{{ user_root.home }}"
    - "{{ user_user.home }}"
  ignore_errors: yes
  when: coding == true and user_root.home != "" and user_user.home != ""

## Test SDK ##

- name: Workstation | Linux | Software | Flutter | Doctor Report
  shell: "{{ item }}"
  loop: "{{ flutter_report_commands }}"
  become_user: "{{ user }}"
  when: coding == true

## Uninstall SDK ##

- name: Workstation | Linux | Software | Flutter | Remove SDK
  file:
    path: "{{ flutter_sdk_location }}"
    state: absent
  when: not coding == true