#!/bin/sh
# ForeverDB: is this Linux PC ready for WoW Forever? (https://foreverdb.net/guide/wow-forever-on-linux)
#
#   curl -fsSL https://foreverdb.net/downloads/linux-check.sh | sh
#
# It only looks: it reads your system's details and prints a report. It installs nothing, changes nothing, needs no sudo
# and sends nothing anywhere. Every check is also in the guide as a command you can run yourself.
# OK = fine, CHECK = worth a look, PROBLEM = fix this first.

ok() { printf '  OK       %s\n' "$*"; }
chk() { printf '  CHECK    %s\n' "$*"; }
bad() { printf '  PROBLEM  %s\n' "$*"; }
have() { command -v "$1" >/dev/null 2>&1; }
head1() { sed -n '1p'; }

printf '\nForeverDB Linux check for WoW Forever\n'
printf '======================================\n'

# ---------------------------------------------------------------- the system
printf '\nSystem\n'
if [ -r /etc/os-release ]; then
  . /etc/os-release
  ok "Distro: ${PRETTY_NAME:-${NAME:-unknown}}"
else
  chk "Distro: unknown (no /etc/os-release)"
fi
[ "$(uname -m)" = "x86_64" ] && ok "Processor type: x86-64" || bad "Processor type: $(uname -m). WoW Forever needs an x86-64 PC (Wine and Proton run Windows x86-64 programs)."
KERNEL=$(uname -r)
KMAJ=$(printf '%s' "$KERNEL" | cut -d. -f1); KMIN=$(printf '%s' "$KERNEL" | cut -d. -f2 | tr -cd '0-9')
if [ -e /dev/ntsync ]; then
  ok "Kernel $KERNEL with NTSYNC (Wine's fastest way to sync threads)"
elif [ "${KMAJ:-0}" -gt 6 ] 2>/dev/null || { [ "${KMAJ:-0}" -eq 6 ] && [ "${KMIN:-0}" -ge 14 ]; } 2>/dev/null; then
  chk "Kernel $KERNEL is new enough for NTSYNC, but /dev/ntsync isn't there: the ntsync module may not be loaded. The game still runs (Proton falls back to fsync)."
else
  chk "Kernel $KERNEL is older than 6.14, so no NTSYNC. The game still runs (Proton uses fsync); a newer kernel can smooth out busy scenes."
fi
[ -n "${XDG_SESSION_TYPE:-}" ] && ok "Desktop session: $XDG_SESSION_TYPE" || chk "Desktop session: unknown (run this from your desktop's terminal)"

# ---------------------------------------------------------------- processor and memory
printf '\nProcessor and memory\n'
CPU=$( (have lscpu && lscpu | sed -n 's/^Model name:[[:space:]]*//p' | head1) || sed -n 's/^model name[[:space:]]*:[[:space:]]*//p' /proc/cpuinfo | head1)
THREADS=$(nproc 2>/dev/null || echo '?')
CPU=$(printf '%s' "$CPU" | sed 's/[[:space:]]*$//')
ok "Processor: ${CPU:-unknown}, $THREADS threads"
MEM_KB=$(sed -n 's/^MemTotal:[[:space:]]*\([0-9]*\).*/\1/p' /proc/meminfo 2>/dev/null)
if [ -n "$MEM_KB" ]; then
  MEM_GB=$(( (MEM_KB + 524288) / 1048576 ))
  if [ "$MEM_GB" -ge 16 ]; then ok "Memory: $MEM_GB GB"
  elif [ "$MEM_GB" -ge 8 ]; then chk "Memory: $MEM_GB GB. It runs, but 16 GB leaves room for Battle.net, Discord and a browser."
  else bad "Memory: $MEM_GB GB. Expect stutter in cities; 16 GB is the comfortable amount."; fi
fi

# ---------------------------------------------------------------- graphics
printf '\nGraphics\n'
if have lspci; then
  lspci -nnk 2>/dev/null | awk '/VGA compatible|3D controller|Display controller/{gpu=$0; sub(/^[^:]*:[^:]*: /,"",gpu); print "  GPU      " gpu} /Kernel driver in use/{sub(/^[[:space:]]*Kernel driver in use: /,""); print "           driver in use: " $0}'
else
  chk "lspci isn't installed (package pciutils), so the graphics card can't be named here"
fi
if have nvidia-smi; then
  NV=$(nvidia-smi --query-gpu=name,driver_version --format=csv,noheader 2>/dev/null | head1)
  [ -n "$NV" ] && ok "NVIDIA driver: $NV"
fi
if lsmod 2>/dev/null | grep -q '^nouveau'; then
  chk "The open nouveau driver is loaded for an NVIDIA card. For WoW, install NVIDIA's own driver (your distro's driver manager has it)."
fi
if lsmod 2>/dev/null | grep -q '^radeon ' && ! lsmod 2>/dev/null | grep -q '^amdgpu'; then
  bad "An old AMD card is on the radeon driver, which has no Vulkan. It needs the amdgpu driver (see the guide)."
fi
if have vulkaninfo; then
  VK=$(vulkaninfo --summary 2>/dev/null)
  if [ -z "$VK" ]; then
    bad "vulkaninfo found no working Vulkan driver. Wine and Proton turn DirectX into Vulkan, so this has to work first."
  else
    printf '%s\n' "$VK" | awk -F'= ' '/deviceName/{n=$2} /apiVersion/{a=$2} /driverName/{d=$2} /deviceType/{t=$2} /driverInfo/{i=$2; if (n !~ /llvmpipe|lavapipe|SwiftShader/) print n "|" a "|" d "|" t "|" i}' | while IFS='|' read -r name api drv type info; do
      maj=$(printf '%s' "$api" | cut -d. -f1 | tr -cd '0-9'); min=$(printf '%s' "$api" | cut -d. -f2 | tr -cd '0-9')
      if [ "${maj:-0}" -gt 1 ] 2>/dev/null || { [ "${maj:-0}" -eq 1 ] && [ "${min:-0}" -ge 3 ]; } 2>/dev/null; then
        ok "Vulkan $api on $name ($drv $info)"
      else
        bad "Vulkan $api on $name: DXVK and VKD3D-Proton need Vulkan 1.3. Update your graphics driver (Mesa or NVIDIA)."
      fi
    done
    printf '%s\n' "$VK" | grep -q 'deviceName' || bad "vulkaninfo printed no graphics device"
    if printf '%s\n' "$VK" | grep -q 'deviceName' && ! printf '%s\n' "$VK" | grep 'deviceName' | grep -vqE 'llvmpipe|lavapipe|SwiftShader'; then
      bad "Only a software Vulkan device (llvmpipe) was found: your real graphics driver's Vulkan part is missing (Mesa's vulkan drivers, or NVIDIA's)."
    fi
  fi
else
  chk "vulkaninfo isn't installed: install vulkan-tools (Debian, Ubuntu, Fedora) or vulkan-tools (Arch) and run this again"
fi

# ---------------------------------------------------------------- storage
printf '\nStorage (keep at least 100 GB free for the game and its patches)\n'
DIR="$HOME/Games"; [ -d "$DIR" ] || DIR="$HOME"
FS=$(stat -f -c %T "$DIR" 2>/dev/null)
FREE=$(df -Pk "$DIR" 2>/dev/null | awk 'NR==2{print int($4/1048576)}')
case "$FS" in
  ext2/ext3|ext4|btrfs|xfs|f2fs|bcachefs) ok "$DIR is on $FS" ;;
  fuseblk|ntfs|ntfs3|msdos|vfat|exfat) bad "$DIR is on $FS (a Windows drive). Wine prefixes break there: put the game on a Linux drive (ext4 or btrfs)." ;;
  "") chk "Couldn't read the filesystem of $DIR" ;;
  *) chk "$DIR is on $FS" ;;
esac
if [ -n "$FREE" ]; then
  if [ "$FREE" -ge 100 ]; then ok "$FREE GB free in $DIR"; else bad "$FREE GB free in $DIR: keep at least 100 GB free for the game and its patches"; fi
fi
DEV=$(df -P "$DIR" 2>/dev/null | awk 'NR==2{print $1}')
ROTA=""
have lsblk && case "$DEV" in /dev/*) ROTA=$(lsblk -dno ROTA "$DEV" 2>/dev/null | head1 | tr -d ' ') ;; esac
if [ -z "$ROTA" ]; then
  BASE=$(printf '%s' "$DEV" | sed -e 's#^/dev/##' -E -e 's/p?[0-9]+$//')
  [ -n "$BASE" ] && [ -r "/sys/block/$BASE/queue/rotational" ] && ROTA=$(cat "/sys/block/$BASE/queue/rotational")
fi
case "$ROTA" in
  0) ok "It's a solid-state drive" ;;
  1) chk "It's a spinning hard drive: the game runs, but loading screens will be slow. An SSD makes a big difference." ;;
esac

# ---------------------------------------------------------------- the tools
printf '\nGame tools\n'
LUTRIS=""
have lutris && LUTRIS="installed"
have flatpak && flatpak info net.lutris.Lutris >/dev/null 2>&1 && LUTRIS="${LUTRIS:+$LUTRIS, }Flatpak"
[ -n "$LUTRIS" ] && ok "Lutris: $LUTRIS" || chk "Lutris isn't installed (the guide uses it to install Battle.net)"
RUNNERS=$(ls -d "$HOME/.local/share/lutris/runners/wine/"*[Pp]roton* "$HOME/.var/app/net.lutris.Lutris/data/lutris/runners/wine/"*[Pp]roton* \
  "$HOME/.local/share/Steam/compatibilitytools.d/"* "$HOME/.steam/root/compatibilitytools.d/"* "$HOME/.var/app/com.valvesoftware.Steam/data/Steam/compatibilitytools.d/"* 2>/dev/null | xargs -n1 basename 2>/dev/null | sort -u | tr '\n' ' ')
[ -n "$RUNNERS" ] && ok "Proton builds found: $RUNNERS" || chk "No GE-Proton found yet: Lutris downloads one for you, or use ProtonUp-Qt"
{ have steam || { have flatpak && flatpak info com.valvesoftware.Steam >/dev/null 2>&1; }; } && ok "Steam is installed" || true
have gamemoded || have gamemoderun && ok "GameMode is installed" || chk "GameMode isn't installed (optional: it asks the CPU for full speed while you play)"
have mangohud && ok "MangoHud is installed" || chk "MangoHud isn't installed (optional: an FPS and frame time overlay)"

printf '\nDone. What each line means, and how to fix a PROBLEM: https://foreverdb.net/guide/wow-forever-on-linux\n\n'
