#!/usr/bin/env bash
# Gacor25 — cek kesiapan Linux untuk gaming (25 pengecekan)
# https://gacor25.kalilinux.info/
#
# Hanya MEMBACA informasi sistem. Tidak butuh sudo, tidak mengubah apa pun.
# Perintah perbaikan hanya DITAMPILKAN, tidak dijalankan.
#
# Pemakaian:  bash gacor25.sh [--no-color] [--help]
# Lisensi: MIT

G25_VERSION="1.0.0"

case "${1:-}" in
  -h|--help)
    echo "Gacor25 $G25_VERSION — cek kesiapan Linux untuk gaming (25 pengecekan)"
    echo "Pemakaian: bash gacor25.sh [--no-color]"
    echo "Hanya membaca sistem. Tidak butuh sudo. Perintah perbaikan hanya ditampilkan."
    exit 0 ;;
  -v|--version) echo "$G25_VERSION"; exit 0 ;;
esac

if [ -t 1 ] && [ "${1:-}" != "--no-color" ] && [ -z "${NO_COLOR:-}" ]; then
  G=$'\e[32m'; Y=$'\e[33m'; R=$'\e[31m'; B=$'\e[1;33m'; D=$'\e[2m'; N=$'\e[0m'
else
  G=""; Y=""; R=""; B=""; D=""; N=""
fi

PASS=0; WARN=0; FAIL=0
have() { command -v "$1" >/dev/null 2>&1; }

line() { # status no label value [fix]
  local st="$1" no="$2" label="$3" val="$4" fix="${5:-}" mark col dots
  case "$st" in
    ok)   mark="✔"; col="$G"; PASS=$((PASS+1)) ;;
    warn) mark="!"; col="$Y"; WARN=$((WARN+1)) ;;
    *)    mark="✘"; col="$R"; FAIL=$((FAIL+1)) ;;
  esac
  dots=$(printf '%*s' $((32 - ${#label})) '' | tr ' ' '.')
  printf ' %s%s%s %02d  %s %s %s\n' "$col" "$mark" "$N" "$no" "$label" "$dots" "$val"
  [ -n "$fix" ] && [ "$st" != "ok" ] && printf '     %s→ %s%s\n' "$D" "$fix" "$N"
  return 0
}
section() { printf '%s[%s]%s\n' "$B" "$1" "$N"; }

# ---------------------------------------------------------------- distro
ID=""; ID_LIKE=""; PRETTY="Linux"
if [ -r /etc/os-release ]; then
  ID=$(. /etc/os-release; echo "${ID:-}")
  ID_LIKE=$(. /etc/os-release; echo "${ID_LIKE:-}")
  PRETTY=$(. /etc/os-release; echo "${PRETTY_NAME:-Linux}")
fi
FAM="other"
case " ${ID:-} ${ID_LIKE:-} " in
  *" arch "*|*" manjaro "*|*" endeavouros "*) FAM="pacman" ;;
  *" fedora "*|*" rhel "*|*" centos "*|*" nobara "*) FAM="dnf" ;;
  *" debian "*|*" ubuntu "*|*" kali "*|*" linuxmint "*|*" pop "*) FAM="apt" ;;
esac
pkg() { # suggested install command per family
  case "$FAM" in
    apt) echo "sudo apt install $1" ;;
    dnf) echo "sudo dnf install $2" ;;
    pacman) echo "sudo pacman -S $3" ;;
    *) echo "pasang paket: $1" ;;
  esac
}

GPU=""; GPU_VENDOR=""
if have lspci; then
  GPU=$(lspci 2>/dev/null | grep -Ei 'vga|3d controller|display controller' | head -n1 | sed -E 's/^[0-9a-fA-F:.]+ [^:]+: //')
  case "$GPU" in
    *NVIDIA*|*nVidia*) GPU_VENDOR="nvidia" ;;
    *AMD*|*ATI*|*Radeon*) GPU_VENDOR="amd" ;;
    *Intel*) GPU_VENDOR="intel" ;;
  esac
fi

printf '\n%sGACOR25%s %s· cek kesiapan Linux untuk gaming · v%s%s\n' "$B" "$N" "$D" "$G25_VERSION" "$N"
printf '%s%s · kernel %s%s\n\n' "$D" "$PRETTY" "$(uname -r)" "$N"

# ---------------------------------------------------------------- 1-5 Grafis
section "Grafis"
if [ -n "$GPU" ]; then
  line ok 1 "GPU terdeteksi" "$(echo "$GPU" | cut -c1-40)"
elif have lspci; then
  line fail 1 "GPU terdeteksi" "tidak ditemukan" "cek apakah ini mesin virtual atau server tanpa GPU"
else
  line warn 1 "GPU terdeteksi" "lspci tidak ada" "$(pkg pciutils pciutils pciutils)"
fi

DRV=""
if have lspci && [ -n "$GPU" ]; then
  DRV=$(lspci -k 2>/dev/null | grep -A3 -Ei 'vga|3d controller|display controller' | grep -m1 'Kernel driver in use' | awk -F': ' '{print $2}')
fi
if [ -n "$DRV" ]; then
  if [ "$DRV" = "nouveau" ]; then
    line warn 2 "Driver kernel aktif" "nouveau" "untuk gaming pakai driver NVIDIA resmi"
  else
    line ok 2 "Driver kernel aktif" "$DRV"
  fi
else
  line fail 2 "Driver kernel aktif" "tidak ada" "pasang driver grafis untuk GPU kamu"
fi

ICD_DIRS="/usr/share/vulkan/icd.d /etc/vulkan/icd.d"
ICDS=$(ls $ICD_DIRS 2>/dev/null | grep -c '\.json$')
if have vulkaninfo; then
  VK=$(vulkaninfo --summary 2>/dev/null | grep -m1 -i 'apiVersion' | grep -oE '[0-9]+\.[0-9]+(\.[0-9]+)?' | head -n1)
  if [ -n "$VK" ]; then line ok 3 "Vulkan tersedia" "$VK"
  else line fail 3 "Vulkan tersedia" "tidak jalan" "$(pkg mesa-vulkan-drivers mesa-vulkan-drivers vulkan-radeon)"; fi
elif [ "${ICDS:-0}" -gt 0 ]; then
  line warn 3 "Vulkan tersedia" "driver ada, vulkan-tools belum" "$(pkg vulkan-tools vulkan-tools vulkan-tools)"
else
  line fail 3 "Vulkan tersedia" "tidak ditemukan" "$(pkg mesa-vulkan-drivers mesa-vulkan-drivers vulkan-radeon)"
fi

VK32=0
ls $ICD_DIRS 2>/dev/null | grep -qE 'i686|i386' && VK32=1
for f in /usr/lib/i386-linux-gnu/libGLX_nvidia.so.0 /usr/lib32/libGLX_nvidia.so.0 /usr/lib/libGLX_nvidia.so.0; do
  [ -e "$f" ] && [ "$GPU_VENDOR" = "nvidia" ] && VK32=1
done
if [ "$VK32" = 1 ]; then line ok 4 "Vulkan 32-bit" "terpasang"
else line fail 4 "Vulkan 32-bit" "tidak ditemukan" "$(pkg mesa-vulkan-drivers:i386 mesa-vulkan-drivers.i686 lib32-vulkan-radeon)"; fi

if have glxinfo && [ -n "${DISPLAY:-}${WAYLAND_DISPLAY:-}" ]; then
  REN=$(glxinfo -B 2>/dev/null | grep -m1 'OpenGL renderer' | sed 's/.*: //')
  case "$REN" in
    *llvmpipe*|*softpipe*|"") line fail 5 "Render bukan llvmpipe" "${REN:-tidak terbaca}" "driver grafis belum aktif, game akan sangat lambat" ;;
    *) line ok 5 "Render bukan llvmpipe" "$(echo "$REN" | cut -c1-40)" ;;
  esac
elif [ -z "${DISPLAY:-}${WAYLAND_DISPLAY:-}" ]; then
  line warn 5 "Render bukan llvmpipe" "tidak ada sesi grafis" "jalankan dari terminal di desktop"
else
  line warn 5 "Render bukan llvmpipe" "glxinfo tidak ada" "$(pkg mesa-utils mesa-demos mesa-utils)"
fi

# ---------------------------------------------------------------- 6-10 Steam
section "Steam dan library"
case "$FAM" in
  apt)
    if dpkg --print-foreign-architectures 2>/dev/null | grep -q i386; then line ok 6 "Arsitektur 32-bit (i386)" "aktif"
    else line fail 6 "Arsitektur 32-bit (i386)" "belum aktif" "sudo dpkg --add-architecture i386 && sudo apt update"; fi ;;
  pacman)
    if grep -qE '^\[multilib\]' /etc/pacman.conf 2>/dev/null; then line ok 6 "Repo multilib" "aktif"
    else line fail 6 "Repo multilib" "belum aktif" "buka komentar [multilib] di /etc/pacman.conf lalu sudo pacman -Syu"; fi ;;
  dnf) line ok 6 "Dukungan 32-bit" "bawaan dnf" ;;
  *) line warn 6 "Dukungan 32-bit" "distro tidak dikenal" ;;
esac

STEAM=""
have steam && STEAM="terpasang"
if [ -z "$STEAM" ] && have flatpak && flatpak list --app 2>/dev/null | grep -q com.valvesoftware.Steam; then STEAM="flatpak"; fi
if [ -n "$STEAM" ]; then line ok 7 "Steam" "$STEAM"
else line warn 7 "Steam" "belum ada" "$(pkg steam-installer steam steam)"; fi

PROTON=""
for d in "$HOME/.steam/steam/steamapps/common" "$HOME/.local/share/Steam/steamapps/common" \
         "$HOME/.var/app/com.valvesoftware.Steam/.local/share/Steam/steamapps/common" \
         "$HOME/.steam/root/compatibilitytools.d" "$HOME/.local/share/Steam/compatibilitytools.d"; do
  ls -d "$d"/*Proton* "$d"/GE-Proton* >/dev/null 2>&1 && PROTON="ada" && break
done
if [ -n "$PROTON" ]; then line ok 8 "Proton" "tersedia"
else line warn 8 "Proton" "belum ada" "Steam > Settings > Compatibility > aktifkan Steam Play"; fi

if have flatpak; then line ok 9 "Flatpak" "$(flatpak --version 2>/dev/null | awk '{print $2}')"
else line warn 9 "Flatpak" "belum ada" "$(pkg flatpak flatpak flatpak)"; fi

if have wine; then line ok 10 "Wine (opsional)" "$(wine --version 2>/dev/null | head -n1)"
else line warn 10 "Wine (opsional)" "belum ada" "$(pkg wine wine wine)"; fi

# ---------------------------------------------------------------- 11-15 Performa
section "Performa"
if have gamemoderun; then line ok 11 "GameMode" "terpasang"
else line warn 11 "GameMode" "belum ada" "$(pkg gamemode gamemode gamemode)"; fi

GOV=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor 2>/dev/null)
case "$GOV" in
  performance|schedutil|ondemand) line ok 12 "CPU governor" "$GOV" ;;
  "") line warn 12 "CPU governor" "tidak terbaca" "wajar di mesin virtual" ;;
  *) line warn 12 "CPU governor" "$GOV" "pakai GameMode saat main: gamemoderun %command%" ;;
esac

MEMKB=$(awk '/^MemTotal:/{print $2}' /proc/meminfo 2>/dev/null)
MEMGB=$(( ${MEMKB:-0} / 1024 / 1024 ))
if [ "${MEMKB:-0}" -ge 7500000 ]; then line ok 13 "RAM" "${MEMGB} GB"
elif [ "${MEMKB:-0}" -ge 3500000 ]; then line warn 13 "RAM" "${MEMGB} GB" "8 GB ke atas lebih nyaman untuk game modern"
else line fail 13 "RAM" "${MEMGB} GB" "RAM terlalu kecil untuk kebanyakan game 3D"; fi

if [ "$(wc -l < /proc/swaps 2>/dev/null)" -gt 1 ]; then line ok 14 "Swap / zram" "aktif"
else line warn 14 "Swap / zram" "tidak ada" "$(pkg zram-tools zram-generator-defaults zram-generator)"; fi

MMC=$(cat /proc/sys/vm/max_map_count 2>/dev/null)
if [ "${MMC:-0}" -ge 1048576 ]; then line ok 15 "vm.max_map_count" "$MMC"
else line warn 15 "vm.max_map_count" "${MMC:-?}" "echo vm.max_map_count=1048576 | sudo tee /etc/sysctl.d/80-gamecompat.conf && sudo sysctl --system"; fi

# ---------------------------------------------------------------- 16-20 Sistem
section "Sistem"
KV=$(uname -r | grep -oE '^[0-9]+\.[0-9]+')
KMAJ=${KV%%.*}; KMIN=${KV#*.}
if [ "${KMAJ:-0}" -gt 6 ] || { [ "${KMAJ:-0}" -eq 6 ] && [ "${KMIN:-0}" -ge 1 ]; }; then line ok 16 "Versi kernel" "$(uname -r)"
else line warn 16 "Versi kernel" "$(uname -r)" "kernel 6.1 ke atas punya dukungan GPU dan controller lebih baik"; fi

case "${XDG_SESSION_TYPE:-}" in
  wayland|x11) line ok 17 "Sesi grafis" "$XDG_SESSION_TYPE" ;;
  *) line warn 17 "Sesi grafis" "${XDG_SESSION_TYPE:-tidak ada}" "jalankan dari terminal di desktop" ;;
esac

FREEKB=$(df -Pk "$HOME" 2>/dev/null | awk 'NR==2{print $4}')
FREEGB=$(( ${FREEKB:-0} / 1024 / 1024 ))
if [ -z "$FREEKB" ]; then line warn 18 "Ruang kosong di home" "tidak terbaca" "folder home tidak ditemukan"
elif [ "$FREEGB" -ge 50 ]; then line ok 18 "Ruang kosong di home" "${FREEGB} GB"
elif [ "$FREEGB" -ge 20 ]; then line warn 18 "Ruang kosong di home" "${FREEGB} GB" "game modern sering butuh 50 GB lebih"
else line fail 18 "Ruang kosong di home" "${FREEGB} GB" "kosongkan ruang disk dulu"; fi

if pgrep -x pipewire >/dev/null 2>&1; then line ok 19 "Audio" "PipeWire"
elif pgrep -x pulseaudio >/dev/null 2>&1; then line ok 19 "Audio" "PulseAudio"
else line warn 19 "Audio" "tidak berjalan" "jalankan dari sesi desktop, atau pasang pipewire"; fi

if ls /usr/lib/udev/rules.d/*steam* /lib/udev/rules.d/*steam* /etc/udev/rules.d/*steam* >/dev/null 2>&1; then
  line ok 20 "Aturan udev controller" "terpasang"
else
  line warn 20 "Aturan udev controller" "belum ada" "$(pkg steam-devices steam-devices steam)"
fi

# ---------------------------------------------------------------- 21-25 Keamanan
section "Keamanan dan akun"
if [ "$(id -u)" -ne 0 ]; then line ok 21 "Tidak jalan sebagai root" "user $(id -un)"
else line fail 21 "Tidak jalan sebagai root" "root" "Steam dan banyak game menolak jalan sebagai root; pakai user biasa"; fi

FW=""
if have systemctl; then
  for s in ufw firewalld nftables; do systemctl is-active --quiet "$s" 2>/dev/null && FW="$s" && break; done
fi
if [ -n "$FW" ]; then line ok 22 "Firewall" "$FW aktif"
else line warn 22 "Firewall" "tidak terdeteksi" "$(pkg ufw firewalld ufw)"; fi

UPD=""
case "$FAM" in
  apt) UPD=$(apt list --upgradable 2>/dev/null | grep -c upgradable) ;;
  dnf) have timeout && UPD=$(timeout 30 dnf -q check-update 2>/dev/null | grep -cE '^[A-Za-z0-9]') ;;
  pacman) have checkupdates && UPD=$(timeout 30 checkupdates 2>/dev/null | wc -l) ;;
esac
if [ -z "$UPD" ]; then line warn 23 "Pembaruan tertunda" "tidak bisa dicek"
elif [ "$UPD" -eq 0 ]; then line ok 23 "Pembaruan tertunda" "tidak ada"
else
  case "$FAM" in apt) F="sudo apt update && sudo apt full-upgrade" ;; dnf) F="sudo dnf upgrade" ;; *) F="sudo pacman -Syu" ;; esac
  line warn 23 "Pembaruan tertunda" "$UPD paket" "$F"
fi

SYNC=""
have timedatectl && SYNC=$(timedatectl show -p NTPSynchronized --value 2>/dev/null)
if [ "$SYNC" = "yes" ]; then line ok 24 "Jam tersinkron" "ya"
else line warn 24 "Jam tersinkron" "${SYNC:-tidak terbaca}" "sudo timedatectl set-ntp true"; fi

if [ ! -d /sys/firmware/efi ]; then
  line ok 25 "Secure Boot" "tidak dipakai (BIOS)"
else
  SB=""
  if have mokutil; then SB=$(mokutil --sb-state 2>/dev/null | head -n1)
  else
    f=$(ls /sys/firmware/efi/efivars/SecureBoot-* 2>/dev/null | head -n1)
    [ -n "$f" ] && [ "$(od -An -t u1 -j4 -N1 "$f" 2>/dev/null | tr -d ' ')" = "1" ] && SB="SecureBoot enabled"
  fi
  case "$SB" in
    *enabled*)
      if [ "$GPU_VENDOR" = "nvidia" ]; then line warn 25 "Secure Boot" "aktif" "modul NVIDIA harus ditandatangani (MOK), atau matikan Secure Boot"
      else line ok 25 "Secure Boot" "aktif"; fi ;;
    *) line ok 25 "Secure Boot" "tidak aktif" ;;
  esac
fi

# ---------------------------------------------------------------- nilai
if [ "$PASS" -ge 21 ]; then LBL="GACOR"; elif [ "$PASS" -ge 15 ]; then LBL="LUMAYAN"; else LBL="BELUM GACOR"; fi
printf '\n%sNilai: %d/25  %s%s  %s(%d gagal, %d peringatan)%s\n' "$B" "$PASS" "$LBL" "$N" "$D" "$FAIL" "$WARN" "$N"
printf '%sPerbaiki yang bertanda ✘ dulu, lalu jalankan lagi: bash gacor25.sh%s\n\n' "$D" "$N"
