#!/usr/bin/env bash
# ============================================================================
#  ArxOS · VMware Workstation setup  --  installs the build tools VMware needs,
#  runs the Broadcom installer bundle, and builds its kernel modules so it
#  works after a reboot. Works on any major Linux (auto-detects the packages).
#
#  VMware Workstation Pro is now free for personal use, but Broadcom does not
#  let scripts download it, so you grab the .bundle yourself once (link below).
#  This script does everything else. Every command is commented.
# ============================================================================
set -uo pipefail

if [ -t 1 ]; then B=$'\e[1m'; DIM=$'\e[2m'; R=$'\e[0m'; G=$'\e[38;5;173m'; OK=$'\e[32m'; NO=$'\e[31m'; Y=$'\e[33m'
else B=""; DIM=""; R=""; G=""; OK=""; NO=""; Y=""; fi
head(){ printf '\n%s%s%s\n' "$B$G" "$*" "$R"; }; good(){ printf '%s✓%s %s\n' "$OK" "$R" "$*"; }
warn(){ printf '%s!%s %s\n' "$Y" "$R" "$*"; }; bad(){ printf '%s✗%s %s\n' "$NO" "$R" "$*"; }
SUDO=""; [ "$(id -u)" -ne 0 ] && SUDO="sudo"
run(){ printf '%s$ %s%s\n' "$DIM" "$*" "$R"; "$@"; }

. /etc/os-release 2>/dev/null || true
if   command -v pacman >/dev/null 2>&1; then PKG=pacman
elif command -v apt    >/dev/null 2>&1; then PKG=apt
elif command -v dnf    >/dev/null 2>&1; then PKG=dnf
elif command -v zypper >/dev/null 2>&1; then PKG=zypper
else PKG=""; fi

head "ArxOS · VMware Workstation setup   (${ID:-linux} · ${PKG:-no package manager})"

# ---- Step 1: install what VMware needs to build its kernel modules ----------
# VMware compiles two modules (vmmon, vmnet) against your kernel at install
# time, so it needs the compiler, make, and your kernel's headers.
head "Step 1 · Install the build tools and kernel headers"
case "$PKG" in
  pacman)  run $SUDO pacman -S --needed --noconfirm base-devel linux-headers gcc make ;;
  apt)     run $SUDO apt update
           run $SUDO apt install -y build-essential "linux-headers-$(uname -r)" gcc make ;;
  dnf)     run $SUDO dnf install -y kernel-devel kernel-headers gcc make "@Development Tools" ;;
  zypper)  run $SUDO zypper install -y kernel-default-devel gcc make ;;
  *)       bad "No supported package manager. Install gcc, make and your kernel headers by hand." ; exit 1 ;;
esac
good "Build tools ready."

# ---- Step 2: locate the VMware installer bundle you downloaded -------------
# You download this once from Broadcom (free personal account):
#   https://support.broadcom.com/  ->  VMware Cloud Foundation  ->  free downloads
#   -> "VMware Workstation Pro" for Linux  ->  the file ends in .bundle
head "Step 2 · Find the VMware .bundle you downloaded"
BUNDLE=""
for d in "$HOME/Downloads" "$HOME" "$(pwd)"; do
  [ -d "$d" ] || continue
  f=$(find "$d" -maxdepth 2 -iname 'VMware-Workstation*.bundle' -type f 2>/dev/null | sort | tail -1)
  [ -n "$f" ] && { BUNDLE="$f"; break; }
done
if [ -z "$BUNDLE" ]; then
  bad "No VMware-Workstation*.bundle found in ~/Downloads."
  echo "Download it (free) from: ${B}https://support.broadcom.com/${R}"
  echo "search 'VMware Workstation Pro', get the Linux .bundle, then rerun this script."
  exit 1
fi
good "Found: $BUNDLE"

# ---- Step 3: run the installer --------------------------------------------
# The bundle is a self-extracting installer; it must run as root. --required
# skips the interactive EULA clicks and installs with sensible defaults.
head "Step 3 · Install VMware Workstation"
run $SUDO chmod +x "$BUNDLE"
run $SUDO bash "$BUNDLE" --required --console || run $SUDO bash "$BUNDLE"
good "VMware installer finished."

# ---- Step 4: build + load VMware's kernel modules --------------------------
# vmware-modconfig compiles vmmon + vmnet for your current kernel and loads
# them, so networking and VMs work immediately -- and after every reboot.
head "Step 4 · Build VMware's kernel modules"
run $SUDO vmware-modconfig --console --install-all 2>/dev/null || warn "modconfig reported an issue -- see note below."
if lsmod | grep -q vmmon; then
  good "VMware modules (vmmon/vmnet) are loaded. Launch it with: vmware"
else
  warn "vmmon not loaded. On distros with Secure Boot, the modules must be signed,"
  warn "or Secure Boot disabled. If it still fails, the community 'vmware-host-modules'"
  warn "package (patched for new kernels) usually fixes it. Then rerun Step 4."
fi

# ---- KVM note --------------------------------------------------------------
if lsmod | grep -qE 'kvm_intel|kvm_amd'; then
  warn "KVM is loaded. VMware and QEMU/KVM share CPU virtualization; shut down any"
  warn "running QEMU/KVM guests before starting a VMware VM (and vice-versa)."
fi

head "Done. Open VMware, create a new VM, and point it at arxos-0.0.1.iso."
