#!/usr/bin/env sh
# Webstead for Linux — starts the Roblox you already have.
#
# It looks for the usual ways Roblox runs on Linux and starts the first one it
# finds, passing on any arguments you give it. It never asks for a password and
# never downloads anything.
#
#   chmod +x webstead.sh
#   ./webstead.sh              start Roblox
#   ./webstead.sh --info       show what was found
#   ./webstead.sh --flags      write a FastFlags file (GrapeJuice/Wine)
#   ./webstead.sh --restore    put the FastFlags back
#
# Version 1.0.0

set -eu

ISSUE=""
FLAGS="balanced"
EXTRA=""
MODE="play"

for arg in "$@"; do
  case "$arg" in
    --info) MODE="info" ;;
    --flags) MODE="flags" ;;
    --restore) MODE="restore" ;;
    --performance) FLAGS="performance" ;;
    --quality) FLAGS="quality" ;;
    --balanced) FLAGS="balanced" ;;
    --help|-h)
      echo "webstead.sh [--info|--flags|--restore] [--performance|--balanced|--quality] [-- extra args]"
      exit 0
      ;;
    --) shift || true ;;
    *) EXTRA="$EXTRA $arg" ;;
  esac
done

say() {
  printf '  %s\n' "$1"
}

# Where a Wine-based Roblox keeps its settings, if there is one.
WHO="${USER:-$(id -un)}"
settings_file() {
  for prefix in "$HOME/.local/share/grapejuice/wineprefix" \
                "$HOME/.wine" \
                "$HOME/.var/app/org.vinegarhq.Sober/data/sober/prefix"; do
    if [ -d "$prefix/drive_c" ]; then
      printf '%s/drive_c/users/%s/AppData/Local/Roblox/ClientSettings/ClientAppSettings.json' \
        "$prefix" "$WHO"
      return 0
    fi
  done
  return 1
}

preset() {
  case "$1" in
    performance)
      cat <<'JSON'
{
  "DFIntTaskSchedulerTargetFps": 240,
  "FIntRenderShadowIntensity": 0,
  "DFIntTextureQualityOverride": 1,
  "FFlagDebugGraphicsPreferD3D11": true
}
JSON
      ;;
    quality)
      cat <<'JSON'
{
  "DFIntTaskSchedulerTargetFps": 120,
  "FIntRenderShadowIntensity": 1,
  "DFIntTextureQualityOverride": 3,
  "FFlagDebugGraphicsPreferD3D11": true
}
JSON
      ;;
    *)
      cat <<'JSON'
{
  "DFIntTaskSchedulerTargetFps": 120,
  "FIntRenderShadowIntensity": 0.5,
  "DFIntTextureQualityOverride": 2,
  "FFlagDebugGraphicsPreferD3D11": true
}
JSON
      ;;
  esac
}

write_flags() {
  file="$(settings_file || true)"
  if [ -z "$file" ]; then
    say "No Wine-based Roblox settings folder was found."
    say "Sober and GrapeJuice each keep their own; open the one you use and drop ClientAppSettings.json there."
    return 0
  fi
  mkdir -p "$(dirname "$file")"
  if [ -f "$file" ] && [ ! -f "$file.backup" ]; then
    cp "$file" "$file.backup"
    say "Your old settings were saved as ClientAppSettings.json.backup"
  fi
  preset "$FLAGS" > "$file"
  say "FastFlags written: $FLAGS"
  say "$file"
}

restore_flags() {
  file="$(settings_file || true)"
  if [ -z "$file" ]; then
    say "No Wine-based Roblox settings folder was found."
    return 0
  fi
  if [ -f "$file.backup" ]; then
    mv "$file.backup" "$file"
    say "Your own settings are back."
  elif [ -f "$file" ]; then
    rm "$file"
    say "The FastFlags file was removed, so Roblox uses its defaults."
  else
    say "Nothing to restore."
  fi
}

show_info() {
  say "Webstead 1.0.0"
  found="none"
  if command -v sober >/dev/null 2>&1; then found="sober"; fi
  if [ "$found" = "none" ] && command -v flatpak >/dev/null 2>&1; then
    if flatpak info org.vinegarhq.Sober >/dev/null 2>&1; then found="flatpak org.vinegarhq.Sober"; fi
  fi
  if [ "$found" = "none" ] && command -v grapejuice >/dev/null 2>&1; then found="grapejuice"; fi
  say "Roblox runner: $found"
  if file="$(settings_file || true)"; then
    say "Settings:      $file"
  fi
}

start_roblox() {
  if command -v sober >/dev/null 2>&1; then
    say "Starting Sober"
    exec sober $EXTRA
  fi
  if command -v flatpak >/dev/null 2>&1 && flatpak info org.vinegarhq.Sober >/dev/null 2>&1; then
    say "Starting Sober (Flatpak)"
    exec flatpak run org.vinegarhq.Sober $EXTRA
  fi
  if command -v grapejuice >/dev/null 2>&1; then
    say "Starting GrapeJuice"
    exec grapejuice launch $EXTRA
  fi
  say "No Roblox runner was found on this Linux machine."
  say "Install Sober (sober.vinegarhq.org) or GrapeJuice, then run this again."
  say "This launcher only starts a copy of Roblox you install yourself."
}

case "$MODE" in
  info) show_info ;;
  flags) write_flags ;;
  restore) restore_flags ;;
  *) start_roblox ;;
esac
