#!/usr/bin/bash

function setup_custom_videos {
    # Apply our branding for Steam
    if [[ -f /usr/bin/bazzite-steam-brand ]]; then
        /usr/bin/bazzite-steam-brand
    fi
}

function short_session_recover {
    mkdir -p "$HOME/.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 "/usr/share/gamescope-session-plus/bootstrap_steam.tar.gz" ]]; then
        tar xf /usr/share/gamescope-session-plus/bootstrap_steam.tar.gz -C "$HOME/.local/share/Steam"
        setup_custom_videos
    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

# When set to 1, a toggle will show up in the steamui to control whether dynamic refresh rate is applied to the steamui
export STEAM_GAMESCOPE_DYNAMIC_REFRESH_IN_STEAM_SUPPORTED=0

# 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

# Allow formatting external drives
export STEAM_ALLOW_DRIVE_ADOPT=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

# HDMI-CEC support
export STEAM_ENABLE_CEC=0

# 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 XCURSOR_THEME=steam
export XCURSOR_SCALE=256

if command -v kwriteconfig6 >/dev/null; then
  # Chromium (and therefore Steam) ignore XCursor and use on the GTK config
  kwriteconfig6 --file gtk-3.0/settings.ini  --group Settings --key gtk-cursor-theme-name steam
fi

# HHD Support + Legion Go Dual DInput Support
export SDL_GAMECONTROLLERCONFIG=$(cat <<-END
060000000d0f00009601000000000000,Steam Controller (HHD),a:b0,b:b1,x:b2,y:b3,back:b6,guide:b8,start:b7,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:h0.1,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,paddle1:b13,paddle2:b12,paddle3:b15,paddle4:b14,misc2:b11,misc3:b16,misc4:b17,crc:ea35,
060035ea0d0f00009601000001000000,Steam Controller (HHD),a:b0,b:b1,x:b2,y:b3,back:b6,guide:b8,start:b7,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:h0.1,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,misc2:b11,
060000000d0f00009601000002000000,Steam Controller (HHD),a:b0,b:b1,x:b2,y:b3,back:b6,guide:b8,start:b7,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:h0.1,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,paddle1:b13,paddle2:b12,misc2:b11,
060035ea0d0f00009601000003000000,Steam Controller (HHD),a:b0,b:b1,x:b2,y:b3,back:b6,guide:b8,start:b7,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:h0.1,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,paddle1:b13,paddle2:b12,paddle3:b15,paddle4:b14,misc2:b11,
030022faef170000ed61000010010000,Legion Go Dual DInput,crc:fa22,platform:Linux,a:b0,b:b1,x:b2,y:b3,leftx:a0,lefty:a1,lefttrigger:b4,righttrigger:b5,back:b7,start:b6,steam:2,
$SDL_GAMECONTROLLERCONFIG
END
)

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 "/usr/share/gamescope-session-plus/bootstrap_steam.tar.gz" ]] && ! grep -q "set_bootstrap=1" "$STEAM_BOOTSTRAP_CONFIG"; then
    mkdir -p "$HOME/.local/share/Steam"
    tar xf /usr/share/gamescope-session-plus/bootstrap_steam.tar.gz -C "$HOME/.local/share/Steam"
    setup_custom_videos
    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
