#!/usr/bin/env bash
# ============================================================================
#  ArxOS · VirtualBox setup  --  installs Oracle VirtualBox and every driver
#  it needs, on any major Linux, with the kernel module built and loaded so
#  it works after a reboot with no troubleshooting.
#
#  NOTE: VirtualBox cannot boot the ArxOS *live* ISO (a known VirtualBox +
#  archiso limitation). Use this if you want VirtualBox for other work; to run
#  ArxOS itself, use the QEMU/KVM setup script (recommended and fully tested).
#
#  Every command is commented. It only installs software; it creates no VMs.
# ============================================================================
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"; "$@"; }

# ---- detect the distro's package manager -----------------------------------
. /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 · VirtualBox setup   (${ID:-linux} · ${PKG:-no package manager})"
warn "VirtualBox will not boot the ArxOS live ISO. For ArxOS, use the QEMU/KVM script."
echo

# ---- install VirtualBox + the matching host kernel modules -----------------
# The host kernel module (vboxdrv) must match your running kernel. On Arch we
# use the DKMS package so it rebuilds automatically on every kernel update.
case "$PKG" in
  pacman)   # Arch / EndeavourOS / Manjaro / ArxOS
    run $SUDO pacman -S --needed --noconfirm virtualbox virtualbox-host-dkms
    # ensure the DKMS module is built for the running kernel, then load it
    run $SUDO dkms autoinstall 2>/dev/null || true
    ;;
  apt)      # Debian / Ubuntu / Mint / Pop!_OS
    run $SUDO apt update
    # virtualbox-dkms builds vboxdrv against your kernel; linux-headers lets it compile
    run $SUDO apt install -y virtualbox virtualbox-dkms "linux-headers-$(uname -r)"
    ;;
  dnf)      # Fedora / RHEL / Nobara  (VirtualBox lives in RPM Fusion)
    run $SUDO dnf install -y "https://mirrors.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm" || true
    run $SUDO dnf install -y VirtualBox kernel-devel kernel-headers dkms gcc make perl
    ;;
  zypper)   # openSUSE
    run $SUDO zypper install -y virtualbox virtualbox-host-source kernel-devel dkms
    ;;
  *)
    bad "No supported package manager found. Install VirtualBox + its host DKMS module by hand."
    exit 1 ;;
esac
good "VirtualBox installed."

# ---- load the kernel module now so no reboot is needed ---------------------
# vboxdrv is the driver every VirtualBox VM needs. Different distros name the
# reload helper differently, so we try each, then fall back to a plain modprobe.
run $SUDO modprobe vboxdrv 2>/dev/null \
  || run $SUDO /sbin/vboxconfig 2>/dev/null \
  || run $SUDO /usr/lib/virtualbox/vboxdrv.sh setup 2>/dev/null || true
if lsmod | grep -q vboxdrv; then good "vboxdrv kernel module is loaded."
else warn "vboxdrv not loaded yet -- reboot once, or run: sudo modprobe vboxdrv"; fi

# ---- add you to the vboxusers group (needed for USB passthrough) -----------
if getent group vboxusers >/dev/null 2>&1; then
  run $SUDO usermod -aG vboxusers "$USER"
  good "Added '$USER' to vboxusers (log out and back in once for it to apply)."
fi

# ---- KVM note: VirtualBox and QEMU/KVM cannot use the CPU at the same time -
if lsmod | grep -qE 'kvm_intel|kvm_amd'; then
  warn "KVM is loaded. VirtualBox and QEMU/KVM share the CPU's virtualization and"
  warn "cannot both run a VM at once. Shut down any running QEMU/KVM guests first."
fi

head "Done. Launch VirtualBox from your menu, or run: virtualbox"
echo "For ArxOS itself, use the QEMU/KVM script -- it is the tested path."
