#!/usr/bin/env 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)
SYS_ID="$(cat /sys/devices/virtual/dmi/id/product_name)"

# SCRIPT VERSION
USER_SETUP_VER=10
USER_SETUP_VER_FILE="$HOME/.bazzite-configured"
USER_SETUP_VER_RAN=$(cat $USER_SETUP_VER_FILE)
USER_SETUP_FEDORA_VER_FILE="$HOME/.bazzite-configured-fedora-version"
USER_SETUP_FEDORA_VER_RAN=$(cat $USER_SETUP_FEDORA_VER_FILE)

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

# Enable Flathub
if grep -qz 'fedora' <<< $(flatpak remotes); then
  flatpak remote-delete --user fedora --force
fi
flatpak remote-add --if-not-exists --user flathub /etc/flatpak/remotes.d/flathub.flatpakrepo
flatpak remote-modify --user --enable flathub

# Handle privileged tasks
pkexec /usr/bin/bazzite-privileged-user-setup

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'

    echo 'Creating Desktop shortcuts'
    mkdir -p $HOME/Desktop/
    if [[ ! -f "$HOME/Desktop/steam.desktop" ]]; then
      cp /usr/share/applications/steam.desktop $HOME/Desktop/steam.desktop
    fi
    if [[ ! -f "$HOME/Desktop/Return.desktop" ]]; then
      cp /etc/skel.d/Desktop/Return.desktop $HOME/Desktop/Return.desktop
    fi
    if [[ ! -f "$HOME/Desktop/net.lutris.Lutris.desktop" ]]; then
      cp /usr/share/applications/net.lutris.Lutris.desktop $HOME/Desktop/net.lutris.Lutris.desktop
    fi
    if [[ ! -f "$HOME/Desktop/yafti.desktop" ]]; then
      cp /usr/share/applications/yafti.desktop $HOME/Desktop/yafti.desktop
    fi
  fi
else
  echo 'Running setup for Silverblue'

  echo 'Enabling GNOME extensions'
  gnome-extensions enable s76-scheduler@mattjakeman.com
  gnome-extensions enable gsconnect@andyholmes.github.io
  gnome-extensions disable background-logo@fedorahosted.org

  echo 'Adjusting Nautilus settings'
  gsettings set org.gnome.nautilus.preferences show-create-link true

  echo 'Installing Gradience presets'
  mkdir -p "$HOME/.var/app/com.github.GradienceTeam.Gradience/config/presets/user/"
  if [[ ! -f "$HOME/.var/app/com.github.GradienceTeam.Gradience/config/presets/user/vapor.json" ]]; then
    rm -f $HOME/.var/app/com.github.GradienceTeam.Gradience/config/presets/user/vapor.json
    cp /usr/share/ublue-os/bazzite/themes/vapor.json $HOME/.var/app/com.github.GradienceTeam.Gradience/config/presets/user/vapor.json
  fi
  if [[ ! -f "$HOME/.var/app/com.github.GradienceTeam.Gradience/config/presets/user/vgui2.json" ]]; then
    rm -f $HOME/.var/app/com.github.GradienceTeam.Gradience/config/presets/user/vgui2.json
    cp /usr/share/ublue-os/bazzite/themes/vgui2.json $HOME/.var/app/com.github.GradienceTeam.Gradience/config/presets/user/vgui2.json
  fi

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

    if [ ${FEDORA_MAJOR_VERSION} -gt 38 ]; then
      echo 'Enabling Bazzite Logo Menu fork'
      gnome-extensions enable logomenu@aryan_k
    else
      echo 'Enabling Bazzite Tofu Menu fork'
      gnome-extensions enable tofumenu@tofu
    fi

    echo 'Disabling GNOME keyboard'
    gsettings set org.gnome.desktop.interface toolkit-accessibility false
    gnome-extensions enable block-caribou-36@lxylxy123456.ercli.dev

    echo 'Enabling Appindicator Support'
    gnome-extenisons enable appindicatorsupport@rgcjonas.gmail.com

    echo 'Setting mouse input profile'
    gsettings set org.gnome.desktop.peripherals.mouse accel-profile "flat"

    echo 'Adjusting trackpad settings'
    gsettings set org.gnome.desktop.peripherals.touchpad send-events "enabled"

    echo 'Setting default power settings'
    gsettings set org.gnome.desktop.session idle-delay 0
    gsettings set org.gnome.settings-daemon.plugins.power ambient-enabled true
    gsettings set org.gnome.settings-daemon.plugins.power idle-dim true
    gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-battery-timeout 300
    gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-timeout 900
    gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-battery-type "suspend"
    gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type "suspend"
    gsettings set org.gnome.settings-daemon.plugins.power power-button-action "suspend"

    echo 'Setting up 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'
    mkdir -p $HOME/.steam
    touch $HOME/.steam/.trackerignore
    mkdir -p $HOME/.local/share/Steam
    touch $HOME/.local/share/Steam/.trackerignore
  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
