#!/usr/bin/bash

# SCRIPT VERSION
VER=22
VER_FILE="/etc/bazzite/flatpak_manager_version"
VER_RAN=$(cat $VER_FILE)
IMAGE_INFO="/usr/share/ublue-os/image-info.json"
IMAGE_FLAVOR=$(jq -r '."image-flavor"' < $IMAGE_INFO)
BASE_IMAGE_NAME=$(jq -r '."base-image-name"' < $IMAGE_INFO)

# IMAGE IDENTIFIERS
KNOWN_IMAGE_FLAVOR_FILE="/etc/bazzite/flatpak_manager_image_flavor"
KNOWN_IMAGE_FLAVOR=$(cat $KNOWN_IMAGE_FLAVOR_FILE)

# Run script if updated
if [[ -f $VER_FILE && $VER = $VER_RAN ]]; then
  if [[ -f $KNOWN_IMAGE_FLAVOR_FILE ]]; then
    if [[ $IMAGE_FLAVOR = $KNOWN_IMAGE_FLAVOR ]]; then
      echo "Flatpak manager v$VER has already ran. Exiting..."
      exit 0
    fi
  fi
fi

# Opt out of and remove Fedora's flatpak repo
if grep -qz 'fedora' <<< $(flatpak remotes); then
  /usr/lib/fedora-third-party/fedora-third-party-opt-out
  /usr/bin/fedora-third-party disable
  flatpak remote-delete fedora --force
fi

# Ensure Flathub is enabled
flatpak remote-add --if-not-exists --system flathub /usr/etc/flatpak/remotes.d/flathub.flatpakrepo
flatpak remote-modify --system --enable flathub

# Installed flatpaks
FLATPAK_LIST=$(flatpak list --columns=application)

# Flatpak list files
INSTALL_LIST_FILE="/usr/share/ublue-os/bazzite/flatpak/install"
REMOVE_LIST_FILE="/usr/share/ublue-os/bazzite/flatpak/remove"

# Flatpak theming support
flatpak override \
  --filesystem=xdg-config/gtk-4.0:ro \
  --filesystem=xdg-config/gtk-3.0:ro \
  --filesystem=xdg-data/icons:ro

# XInput for Firefox
flatpak override \
  --system \
  --env=MOZ_USE_XINPUT2=1 \
  org.mozilla.firefox

# Fix printing on LibreOffice
flatpak override \
  --system \
  --socket=cups \
  --socket=session-bus \
  org.libreoffice.LibreOffice

# Allow MangoHUD config for Flatpaks
flatpak override \
  --filesystem=xdg-config/MangoHud:ro \
  --filesystem=xdg-config/vkBasalt:ro

# Fix permissions for XIV Launcher
flatpak override \
  --device=dri \
  dev.goats.xivlauncher

# Params needed for HW accelerated video on Nvidia (Ignoring hybrid systems)
if [[ $IMAGE_FLAVOR =~ "nvidia" ]] && [ $(grep -o "\-display" <<< $(lshw -C display) | wc -l) -le 1 ] && grep -q "vendor: NVIDIA Corporation" <<< $(lshw -C display); then
  flatpak override \
    --system \
    --filesystem=host-os \
    --env=LIBVA_DRIVER_NAME=nvidia \
    --env=LIBVA_DRIVERS_PATH=/run/host/usr/lib64/dri \
    --env=LIBVA_MESSAGING_LEVEL=1 \
    --env=MOZ_DISABLE_RDD_SANDBOX=1 \
    --env=NVD_BACKEND=direct \
    org.mozilla.firefox
else
  # Undo if user was previously using a Nvidia image and is no longer
  flatpak override \
    --system \
    --nofilesystem=host-os \
    --unset-env=LIBVA_DRIVER_NAME \
    --unset-env=LIBVA_DRIVERS_PATH \
    --unset-env=LIBVA_MESSAGING_LEVEL \
    --unset-env=MOZ_DISABLE_RDD_SANDBOX \
    --unset-env=NVD_BACKEND \
    org.mozilla.firefox
fi

# Set up Firefox default configuration
mkdir -p /var/lib/flatpak/extension/org.mozilla.firefox.systemconfig/x86_64/stable/defaults/pref
rm -f /var/lib/flatpak/extension/org.mozilla.firefox.systemconfig/x86_64/stable/defaults/pref/*bazzite*.js
/usr/bin/cp -rf /usr/share/ublue-os/firefox-config/* /var/lib/flatpak/extension/org.mozilla.firefox.systemconfig/x86_64/stable/defaults/pref/

# Install flatpaks in list
if [[ -f $INSTALL_LIST_FILE ]]; then
  if [[ -n $FLATPAK_LIST ]]; then
    INSTALL_LIST=$(echo $FLATPAK_LIST | grep -vf - $INSTALL_LIST_FILE)
  else
    INSTALL_LIST=$(cat $INSTALL_LIST_FILE)
  fi
  if [[ -n $INSTALL_LIST ]]; then
    if ! flatpak install --system --noninteractive flathub ${INSTALL_LIST[@]}; then
      # exit on error
      exit 1
    fi
  fi
fi

# Remove flatpaks in list once
if [[ ! -f $VER_FILE && -f $REMOVE_LIST_FILE ]]; then
  REMOVE_LIST=$(echo $FLATPAK_LIST | grep -f - $REMOVE_LIST_FILE)
  if [[ -n $REMOVE_LIST ]]; then
    flatpak remove --system --noninteractive ${REMOVE_LIST[@]}
  fi
fi

mkdir -p /etc/bazzite
echo $VER > $VER_FILE
echo $IMAGE_FLAVOR > $KNOWN_IMAGE_FLAVOR_FILE
