#!/bin/bash

# omarchy:summary=Remove Steam and all of its game libraries, configs, and caches.
# omarchy:requires-sudo=true

set -e

echo "Removing Steam..."
omarchy-lifecycle-dispatch pre-remove steam

# Closing its window leaves Steam running in the tray, where it would keep
# running and write data back after the rm.
uid=$(id -u)
steam_procs='steam|steamwebhelper'
if pgrep -u "$uid" -x "$steam_procs" >/dev/null; then
  pkill -u "$uid" -x "$steam_procs" || true
  for ((i = 0; i < 50; i++)); do
    pgrep -u "$uid" -x "$steam_procs" >/dev/null || break
    sleep 0.2
  done
  if pgrep -u "$uid" -x "$steam_procs" >/dev/null; then
    pkill -KILL -u "$uid" -x "$steam_procs" || true
  fi
fi

omarchy-pkg-drop steam

# Keep the default locations in case XDG settings changed after Steam was installed.
steam_dirs=("$HOME/.steam" "$HOME/.local/share/Steam" "$HOME/.config/steam" "$HOME/.cache/steam")
[[ ${XDG_DATA_HOME:-} == /* ]] && steam_dirs+=("$XDG_DATA_HOME/Steam")
[[ ${XDG_CONFIG_HOME:-} == /* ]] && steam_dirs+=("$XDG_CONFIG_HOME/steam")
[[ ${XDG_CACHE_HOME:-} == /* ]] && steam_dirs+=("$XDG_CACHE_HOME/steam")
rm -rf -- "${steam_dirs[@]}"

echo ""
echo "Steam and its data have been removed."
