---
# Define program names for package builtin.
# This file is for ALL systems and should not include UI components.

- name: General | Facts | Packages | Shared Defaults
  set_fact:
    sshfs: sshfs
    tar: tar

- name: General | Facts | Packages | Parrot OS Fixes
  set_fact:
    ansible_pkg_mgr: "apt"
    ansible_python_interpreter: "/usr/bin/python3"
  when: ansible_distribution == "Parrot OS"

- name: General | Facts | Package | apt
  set_fact:
    locate: locate
    opensshd: openssh-server
    microcode_amd: amd64-microcode
    microcode_intel: intel-microcode
    cron: cron
    encfs: encfs
  when: ansible_pkg_mgr == "apt"

- name: General | Facts | Package | pacman
  set_fact:
    locate: mlocate
    opensshd: openssh
    microcode_amd: linux-firmware
    microcode_intel: intel-ucode
    cron: cronie
    encfs: encfs
  when: ansible_pkg_mgr == "pacman"

- name: General | Facts | Package | FreeBSD
  set_fact:
    sshfs: fusefs-sshfs
    locate: htop # Placeholder to prevent errors, locate built into FreeBSD.
    opensshd: htop # sshd comes installed on FreeBSD
    ansible_python_interpreter: "/usr/local/bin/python3.8"
    tar: htop # tar already provided in FreeBSD
    microcode_amd: htop # TODO
    microcode_intel: htop # TODO
    encfs: fusefs-encfs
  when: ansible_system == "FreeBSD"

- name: General | Facts | Package | dnf
  set_fact:
    sshfs: fuse-sshfs
    locate: mlocate
    opensshd: openssh
    microcode_amd: microcode_ctl
    microcode_intel: microcode_ctl
    cron: cronie
    encfs: fuse-encfs
  when: ansible_pkg_mgr == "dnf"

- name: General | Facts | Package | zypper
  set_fact:
    locate: plocate
    opensshd: openssh
    microcode_amd: ucode-amd
    microcode_intel: ucode-intel
    cron: cronie
    encfs: encfs
  when: ansible_pkg_mgr == "zypper"


- name: General | Facts | Package | Update Command | Helpers
  set_fact:
    update_accept_var: '$accept'
    nix_days: 14
    ansible_distribution_file: "{{ prov_dir }}/ansible_distribution.txt"
    ansible_pkg_mgr_file: "{{ prov_dir }}/ansible_pkg_mgr.txt"

- name: General | Facts | Package | Update Command | Helpers | pacman
  set_fact:
    update_accept_var: '--noconfirm'
  when: ansible_pkg_mgr == "pacman"

- name: General | Facts | Package | Update Command | Store Variables
  shell: "{{ item }}"
  loop:
    - "echo '{{ ansible_distribution }}' > {{ ansible_distribution_file }}"
    - "echo '{{ ansible_pkg_mgr }}' > {{ ansible_pkg_mgr_file }}"

- name: General | Facts | Package | Update Command
  set_fact:
    update_package_manager: |

      distribution="`cat {{ ansible_distribution_file }}`"
      pkg_mgr="`cat {{ ansible_pkg_mgr_file }}`"

      case $pkg_mgr in
        apt)
          if [[ $distribution != "ParrotOS" ]]; then
            echo -e "******* Apt *******\n*** Update Cache ***" &&
            sudo apt update &&
            echo -e "\n*** Auto Remove ***" &&
            sudo apt autoremove {{ update_accept_var }} &&
            echo -e "\n*** Clean ***" &&
            sudo apt clean {{ update_accept_var }} &&
            echo -e "Cleaned!\n\n*** Configure DPKG ***" &&
            sudo dpkg --configure -a &&
            echo -e "\n*** Fix Broken/Missing ***" &&
            sudo apt --fix-broken --fix-missing install &&
            echo -e "\n*** Upgrade ***" &&
            sudo apt dist-upgrade --allow-downgrades --fix-broken \
              --fix-missing {{ update_accept_var }} ||
            return 1
          else
            echo -e "******* Parrot *******\n*** Update Cache ***" &&
            sudo apt update &&
            echo -e "\n*** Auto Remove ***" &&
            sudo apt autoremove {{ update_accept_var }} &&
            parrot_mirrors_suck=true &&
            while [[ $parrot_mirrors_suck ]]; do
              unset parrot_mirrors_suck
              echo -e "\n*** Upgrade ***"
              sudo parrot-upgrade
              if [[ $? != 0 ]]; then
                parrot_mirrors_suck=true
                echo -e "*** Update Cache, Again ***"
                sudo apt update --fix-missing
              fi
            done ||
            return 1
          fi
          echo -e "\n******* Mission Complete!! *******"
        ;;

        pacman)
          echo "*** Pacman ***"
          if [[ "$accept" == "Y" ]]; then
            auto_accept="{{ update_accept_var }}"
          fi
          sudo pacman -Syyu $auto_accept ||
          return 1
        ;;

        pkg | pkgng)
          echo "*** FreeBSD-Update ***"
          clean=""
          [[ "{{ update_accept_var }}" == "-y" ]] ||
            echo "Are you sure you'd like to clean /var/db/freebsd-update? [y/N] "
          [[ "{{ update_accept_var }}" == "-y" ]] ||
            read -N 1 clean
          echo -e "\n"
          typeset -l clean
          if [[ "{{ update_accept_var }}" == "-y" || "$clean" == "y" ]]; then
            echo "Cleaning directory..."
            sudo sh -c "rm -rfv /var/db/freebsd-update
            mkdir -v /var/db/freebsd-update"
          fi
          sudo freebsd-update fetch install

          echo "*** Pkg ***" &&
          sudo pkg update &&
          sudo pkg clean {{ update_accept_var }} &&
          sudo pkg autoremove {{ update_accept_var }} &&
          sudo pkg upgrade {{ update_accept_var }} ||
          return 1
        ;;

        dnf)
          echo "*** DNF ***" &&
          sudo dnf clean all {{ update_accept_var }} &&
          sudo dnf autoremove {{ update_accept_var }} &&
          sudo dnf upgrade {{ update_accept_var }} ||
          return 1
        ;;

        zypper)
          echo "*** Zypper ***" &&
          sudo zypper refresh &&
          sudo zypper cleanlocks &&
          sudo zypper purge-kernels &&
          sudo zypper clean -a &&
          sudo zypper patch {{ update_accept_var }} &&
          sudo zypper update {{ update_accept_var }} &&
          sudo zypper dist-upgrade {{ update_accept_var }} &&
          # This is a neat feature, but is installing things like GIMP from the
          #  repos when it is already installed through Flatpak, so not needed.
          #sudo zypper install-new-recommends ||
          sleep 0 ||
          return 1
        ;;

        *)
          if [[ $distribution == "NixOS" ]]; then
            sudo echo "*** NixOS ***" &&
            # Clean up!
            # https://specific.solutions.limited/blog/recovering-diskspace-in-nixos

            echo -en "\n * Delete any existing garbage and snapshots older than " &&
            echo -e "{{ nix_days }} days.\n" &&
            echo "User:" &&
            nix-collect-garbage --delete-older-than '{{ nix_days }}d' &&
            echo "Root:" &&
            sudo nix-collect-garbage --delete-older-than '{{ nix_days }}d'

            ls ~/.cache/*nix* /root/.cache/*nix* 2>/dev/null 1>&2 &&
                echo -e "\n * Delete any cache.\n"
            du -hs ~/.cache/*nix* 2>/dev/null &&
                echo "User:" &&
                rm -rfv ~/.cache/*nix*
            sudo du -hs /root/.cache/*nix* 2>/dev/null &&
                echo "Root:" &&
                sudo rm -rfv /root/.cache/*nix*

            echo -e "\n * Update channels with the latest expressions.\n" &&
            echo "User:" &&
            nix-channel --update &&
            echo "Root:" &&
            sudo nix-channel --update

            echo -e "\n * Rebuild using the existing *.nix files.\n" &&
            sudo nixos-rebuild switch ||
            return 1

            echo -e "\n * Hard link duplicates, saves 25-35% of store size.\n" &&
            sudo nix-store --optimize
          else
            echo "WARNING: This package manager is not supported yet." >&2
            echo "  distribution='${distribution}', pkg_mgr='${pkg_mgr}'" >&2
          fi
        ;;
      esac
# End of update_package_manager

# Flatpaks
- name: General | Facts | Package | Update Command | Flatpak | Check
  shell: which flatpak
  register: flatpak_exec
  ignore_errors: yes

- name: General | Facts | Package | Update Command | Flatpak | Exists
  set_fact:
    update_flatpak: |
      echo "*** Flatpak ***"
      [[ -n {{ update_accept_var }} ]] && sudo flatpak repair
      sudo flatpak uninstall --unused {{ update_accept_var }} &&
      sudo flatpak update {{ update_accept_var }} ||
      return 1
  when: flatpak_exec is defined
    and flatpak_exec.failed is defined
    and not flatpak_exec.failed

- name: General | Facts | Package | Update Command | Flatpak | Missing
  set_fact:
    update_flatpak: |
      echo "*** Flatpak Not Installed ***"
  when: update_flatpak is not defined

# Firmware
- name: General | Facts | Package | Update Command | Firmware | Check
  shell: which fwupdmgr
  register: fwupdmgr_exec
  ignore_errors: yes

- name: General | Facts | Package | Update Command | Firmware | Exists
  set_fact:
    update_firmware: |
      echo "*** Firmware ***"
      sudo fwupdmgr refresh --force &&
      sudo fwupdmgr get-updates {{ update_accept_var }} &&
      sudo fwupdmgr update --no-reboot-check {{ update_accept_var }} ||
      return 1
  when: fwupdmgr_exec is defined
    and fwupdmgr_exec.failed is defined
    and not fwupdmgr_exec.failed

- name: General | Facts | Package | Update Command | Firmware | Missing
  set_fact:
    update_firmware: |
      echo "*** Firmware Updater (fwupdmgr) Not Installed ***"
  when: update_firmware is not defined