#!/usr/bin/bash

IMAGE_INFO="/usr/share/ublue-os/image-info.json"
IMAGE_NAME=$(jq -r '."image-name"' < $IMAGE_INFO)
BASE_IMAGE_NAME=$(jq -r '."base-image-name"' < $IMAGE_INFO)
FEDORA_VERSION=$(jq -r '."fedora-version"' < $IMAGE_INFO)

# INIT
BAZZITE_CONFIG_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/bazzite"
mkdir -p "$BAZZITE_CONFIG_DIR"

# SCRIPT VERSION
USER_SETUP_VER=30
USER_SETUP_VER_FILE="$BAZZITE_CONFIG_DIR/version"
USER_SETUP_FEDORA_VER_FILE="$BAZZITE_CONFIG_DIR/fedora_version"
USER_SETUP_IMAGE_VER_FILE=$BAZZITE_CONFIG_DIR/image_name""

if [[ -f "$HOME/.bazzite-configured" ]]; then
  mv -f "$HOME/.bazzite-configured" "$USER_SETUP_VER_FILE"
fi

if [[ -f "$HOME/.bazzite-configured-fedora-version" ]]; then
  mv -f "$HOME/.bazzite-configured-fedora-version" "$USER_SETUP_FEDORA_VER_FILE"
fi

USER_SETUP_VER_RAN=$(cat $USER_SETUP_VER_FILE)
USER_SETUP_FEDORA_VER_RAN=$(cat $USER_SETUP_FEDORA_VER_FILE)
USER_SETUP_IMAGE_VER_RAN=$(cat $USER_SETUP_IMAGE_VER_FILE)

# Run script if updated
if [[ -f $USER_SETUP_VER_FILE && -f $USER_SETUP_FEDORA_VER_RAN && -f $USER_SETUP_FEDORA_VER_FILE ]]; then
  if [[ $USER_SETUP_VER = "$USER_SETUP_VER_RAN" && $FEDORA_VERSION = "$USER_SETUP_FEDORA_VER_RAN" && $BASE_IMAGE_NAME = "$USER_SETUP_IMAGE_VER_RAN" ]]; then
    echo "User setup v$USER_SETUP_VER has already run. Exiting..."
    exit 0
  fi
fi

# Enable Flathub
flatpak remote-add --if-not-exists --user flathub /usr/etc/flatpak/remotes.d/flathub.flatpakrepo
flatpak remote-modify --user --enable flathub

# Handle privileged tasks
pkexec /usr/libexec/bazzite-privileged-user-setup "$USER"

# Refresh fontconfig
if [[ -f "$HOME/.config/fontconfig" ]]; then
  rm -rf "$HOME/.config/fontconfig"
fi

# Set up optional Gradience themes
GRADIENCE_THEME_DIR="/usr/etc/skel/.var/app/com.github.GradienceTeam.Gradience/config/presets/user"
GRADIENCE_DIR="$HOME/.var/app/com.github.GradienceTeam.Gradience/config/presets/user"
mkdir -p "$GRADIENCE_DIR"
if [[ ! -f "$GRADIENCE_DIR/vapor.json" ]]; then
  cp "$GRADIENCE_THEME_DIR/vapor.json" "$GRADIENCE_DIR/vapor.json"
fi
if [[ ! -f "$GRADIENCE_DIR/vgui2.json" ]]; then
  cp "$GRADIENCE_THEME_DIR/vgui2.json" "$GRADIENCE_DIR/vgui2.json"
fi

PTYXIS_THEME_DIR="/usr/etc/skel/.local/share/org.gnome.Ptyxis/palettes"
PTYXIS_DIR="$HOME/.local/share/org.gnome.Ptyxis/palettes"
mkdir -p "$PTYXIS_DIR"
if [[ ! -f "$PTYXIS_DIR/vapor.palette" ]]; then
  cp "$PTYXIS_THEME_DIR/vapor.palette" "$PTYXIS_DIR/vapor.palette"
fi
if [[ ! -f "$PTYXIS_DIR/vgui2.palette" ]]; then
  cp "$PTYXIS_THEME_DIR/vgui2.palette" "$PTYXIS_DIR/vgui2.palette"
fi

# Initialize Ptyxis config
# This is intentionally run on both images to ensure a user rebasing from Silverblue to Kinoite
# doesn't get their Ptyxis config wiped out.
echo 'Configuring Ptyxis'
if [[ ! -f "$BAZZITE_CONFIG_DIR/ptyxis-initialized" ]]; then
  if [[ $BASE_IMAGE_NAME =~ "kinoite" ]]; then
    dconf load / < /etc/dconf/db/local.d/02-bazzite-kde
  fi
  touch "$BAZZITE_CONFIG_DIR/ptyxis-initialized"
fi

if [[ $BASE_IMAGE_NAME =~ "kinoite" ]]; then
  echo 'Running setup for Kinoite'

  echo 'Enabling System76-Scheduler KWin script'
  kwriteconfig5 --file kwinrc --group Plugins --key kwin-system76-scheduler-integrationEnabled true

  if [[ $IMAGE_NAME =~ "deck" || $IMAGE_NAME =~ "ally" || $IMAGE_NAME =~ "framegame" ]]; then
    echo 'Running setup for Kinoite on Steam Deck'
    if [[ ! -f "$HOME/Desktop/Return.desktop" ]]; then
      echo 'Re-creating return to gamemode shortcut'
      mkdir -p $HOME/Desktop/
      cp "/etc/skel/Desktop/Return.desktop" "$HOME/Desktop/Return.desktop"
    fi
  fi
else
  echo 'Running setup for Silverblue'

  if [[ $IMAGE_NAME =~ "deck" || $IMAGE_NAME =~ "ally" || $IMAGE_NAME =~ "framegame" ]]; then
    echo 'Running setup for Silverblue on Steam Deck'

    echo 'Setting up templates'
    mkdir -p "$HOME/Templates"
    if [[ ! -f "$HOME/Templates/vkBasalt.conf" ]]; then
      ln -s "/etc/vkBasalt.conf" "$HOME/Templates/vkBasalt.conf"
    fi
    if [[ ! -f "$HOME/Templates/dxvk.conf" ]]; then
      ln -s "/etc/dxvk-example.conf" "$HOME/Templates/dxvk.conf"
    fi

    echo 'Tweaking GNOME indexer'
    if [[ ! -f "$HOME/.steam/.trackerignore" ]]; then
      mkdir -p "$HOME/.steam"
      touch "$HOME/.steam/.trackerignore"
    fi
    if [[ ! -f "${XDG_DATA_HOME:-$HOME/.local/share}/Steam/.trackerignore" ]]; then
      mkdir -p "${XDG_DATA_HOME:-$HOME/.local/share}/Steam"
      touch "${XDG_DATA_HOME:-$HOME/.local/share}/Steam/.trackerignore"
    fi
  fi
fi

# Deck Build Setup
if [[ $IMAGE_NAME =~ "deck" || $IMAGE_NAME =~ "ally" || $IMAGE_NAME =~ "framegame" ]]; then
  # HHD Setup
  if /usr/libexec/hardware/hhd-supported-hardware; then
    echo 'Enabling HHD'
    pkexec /usr/libexec/bazzite-enable-hhd "$USER"
  fi

  # SDGyroDSU Setup
  if /usr/libexec/hardware/valve-hardware; then
    systemctl --user enable --now sdgyrodsu.service
  else
    systemctl --user disable --now sdgyrodsu.service
  fi

  # Legion Rotation Fix
  AUTOSTART_FOLDER=${XDG_CONFIG_HOME:-$HOME/.config}

  # Remove old legion-only file
  if [[ -f "$AUTOSTART_FOLDER/autostart/bazzite-handle-legion-go-rotation.desktop" ]]; then
    rm -f "$AUTOSTART_FOLDER/autostart/bazzite-handle-legion-go-rotation.desktop"
  fi

  if /usr/libexec/hardware/rotation-fix-hardware && [[ $BASE_IMAGE_NAME =~ "kinoite" ]]; then
    if [[ ! -f "$AUTOSTART_FOLDER/autostart/bazzite-rotation-fix.desktop" ]]; then
      echo 'Adding rotation fix'
      mkdir -p "$AUTOSTART_FOLDER/autostart"
      printf "[Desktop Entry]\nExec=/usr/libexec/bazzite-rotation-fix\nIcon=dialog-scripts\nName=bazzite-rotation-fix\nType=Application\nX-KDE-AutostartScript=true\n" > "$AUTOSTART_FOLDER/autostart/bazzite-rotation-fix.desktop"
    fi
  elif [[ -f "$AUTOSTART_FOLDER/autostart/bazzite-rotation-fix.desktop" ]]; then
    echo 'Non-supported or GNOME detected, removing rotaion fix'
    rm -f "$AUTOSTART_FOLDER/autostart/bazzite-rotation-fix.desktop"
  fi
fi

# Prevent future executions
echo "Writing state file"
echo $USER_SETUP_VER > $USER_SETUP_VER_FILE
echo $FEDORA_VERSION > $USER_SETUP_FEDORA_VER_FILE
echo $BASE_IMAGE_NAME > $USER_SETUP_IMAGE_VER_FILE
