#! /bin/bash

function short_session_recover {
    mkdir -p ~/.local/share/Steam
    # remove some caches and stateful things known to cause Steam to fail to start if corrupt
    rm -rf --one-file-system ~/.local/share/Steam/config/widevine
    # extract the steam bootstrap to potentially fix the issue the next boot
    if [[ -f "/etc/first-boot/bootstraplinux_ubuntu12_32.tar.xz" ]]; then
        tar xf /etc/first-boot/bootstraplinux_ubuntu12_32.tar.xz -C ~/.local/share/Steam
    fi
    # change session to desktop as fallback
    steamos-session-select desktop
}

function post_gamescope_start {
	# Run steam-tweaks if exists
	if command -v steam-tweaks > /dev/null; then
		steam-tweaks
	fi
}

# Show VRR controls in Steam
export STEAM_GAMESCOPE_VRR_SUPPORTED=1

# Enable Mangoapp
export STEAM_MANGOAPP_PRESETS_SUPPORTED=1
export STEAM_USE_MANGOAPP=1

export STEAM_USE_DYNAMIC_VRS=1

# Set up first time bootstrap status
export STEAM_BOOTSTRAP_CONFIG="${XDG_CONFIG_HOME:-$HOME/.config}/gamescope/bootstrap.cfg"

# Make path to Steam bootstrap config file.
mkdir -p "$(dirname "$STEAM_BOOTSTRAP_CONFIG")"
touch "$STEAM_BOOTSTRAP_CONFIG"

# Support for gamescope tearing with GAMESCOPE_ALLOW_TEARING atom
export STEAM_GAMESCOPE_HAS_TEARING_SUPPORT=1

# Enable tearing controls in steam
export STEAM_GAMESCOPE_TEARING_SUPPORTED=1

export STEAM_GAMESCOPE_HDR_SUPPORTED=1

# Workaround for steam getting killed immediatly during reboot
export STEAMOS_STEAM_REBOOT_SENTINEL="/tmp/steamos-reboot-sentinel"
export REBOOT_SENTINEL=$STEAMOS_STEAM_REBOOT_SENTINEL # for common gamescope-session-plus

# Workaround for steam getting killed immediatly during shutdown
# Same idea as reboot sentinel above
export STEAMOS_STEAM_SHUTDOWN_SENTINEL="/tmp/steamos-shutdown-sentinel"
export SHUTDOWN_SENTINEL=$STEAMOS_STEAM_SHUTDOWN_SENTINEL # for common gamescope-session-plus

# Enable volume key management via steam for this session
export STEAM_ENABLE_VOLUME_HANDLER=1

# Have SteamRT's xdg-open send http:// and https:// URLs to Steam
export SRT_URLOPEN_PREFER_STEAM=1

# Disable automatic audio device switching in steam, now handled by wireplumber
export STEAM_DISABLE_AUDIO_DEVICE_SWITCHING=1

# Enable support for xwayland isolation per-game in Steam
export STEAM_MULTIPLE_XWAYLANDS=1

# We have the Mesa integration for the fifo-based dynamic fps-limiter
export STEAM_GAMESCOPE_DYNAMIC_FPSLIMITER=1

# We have NIS support
export STEAM_GAMESCOPE_NIS_SUPPORTED=1

# Let steam know it can unmount drives without superuser privileges
export STEAM_ALLOW_DRIVE_UNMOUNT=1

# We no longer need to set GAMESCOPE_EXTERNAL_OVERLAY from steam, mangoapp now does it itself
export STEAM_DISABLE_MANGOAPP_ATOM_WORKAROUND=1

# Enable horizontal mangoapp bar
export STEAM_MANGOAPP_HORIZONTAL_SUPPORTED=1

# Scaling support
export STEAM_GAMESCOPE_FANCY_SCALING_SUPPORT=1

# Color management support
export STEAM_GAMESCOPE_COLOR_MANAGED=1
export STEAM_GAMESCOPE_VIRTUAL_WHITE=1

# Set input method modules for Qt/GTK that will show the Steam keyboard
export QT_IM_MODULE=steam
export GTK_IM_MODULE=Steam

# Use SteamOS background if exists
if [ -f "/usr/share/steamos/steamos.png" ] ; then
  export STEAM_UPDATEUI_PNG_BACKGROUND=/usr/share/steamos/steamos.png
fi

export CURSOR_FILE="${HOME}/.local/share/Steam/tenfoot/resource/images/cursors/arrow.png"

export CLIENTCMD="steam -gamepadui -steamos3 -steampal -steamdeck"

# Workaround for Steam login issue while Steam client change propagates out of Beta
touch "${HOME}"/.steam/root/config/SteamAppData.vdf || true

if [[ -f "/etc/first-boot/bootstraplinux_ubuntu12_32.tar.xz" ]] && ! grep -q "set_bootstrap=1" "$STEAM_BOOTSTRAP_CONFIG"; then
    mkdir -p ~/.local/share/Steam
    tar xf /etc/first-boot/bootstraplinux_ubuntu12_32.tar.xz -C ~/.local/share/Steam
    echo "set_bootstrap=1" >> "$STEAM_BOOTSTRAP_CONFIG"
fi

# If we have steam_notif_daemon binary start it
if command -v steam_notif_daemon > /dev/null; then
    steam_notif_daemon &
fi
