#!/usr/bin/env bash
# bash -c "$(wget -qLO- https://github.com/therepos/proxmox/raw/main/apps/setup-vm.sh?$(date +%s))"
# Purpose: Orchestrate Ubuntu VM setup (delegates to standalone installers)
# =============================================================================
# Usage (auto-detects side; run on host first, then inside the VM):
#   Host:  menu -> 1) Create VM (create-vm.sh)  2) Mount share (setup-virtiofs.sh)
#          Passing args (e.g. 'setup') skips the menu and delegates straight to
#          setup-virtiofs.sh, preserving the previous host behaviour.
#   VM:    BASE steps (fixed order): webmin -> disk -> docker -> nvidia -> virtiofs
#          APPS (order-free, env VM_APPS; none by default): each 'foo' -> setup-foo.sh
#          Kasm is not installed here anymore; use setup-kasm.sh directly.
#
# Note: NVIDIA auto-skips without GPU; a fresh driver reboots (exit 10) and
#   setup resumes automatically. VM_APPS is persisted resume-safe across reboot.
# Config (env): VMID, VIRTIOFS_TAG, MOUNT_POINT, HOST_SHARE_PATH, CHOWN_ENABLE,
#   CHOWN_PATH, CHOWN_UID, CHOWN_GID, SHUTDOWN_TIMEOUT, KASM_VERSION, KASM_PORT,
#   NVIDIA_DRIVER_VERSION, VM_APPS.
# =============================================================================

set -euo pipefail

# --- Helpers -----------------------------------------------------------------
# >>> admonition
if [[ -n "${FORCE_COLOR:-}" || -t 1 ]]; then
  _CK=$'\033[1;32m'; _CI=$'\033[1;36m'; _CW=$'\033[1;33m'; _CE=$'\033[1;31m'; _C0=$'\033[0m'
else
  _CK=''; _CI=''; _CW=''; _CE=''; _C0=''
fi
ok()   { printf '%s[ OK ]%s %s\n' "$_CK" "$_C0" "$*"; }
info() { printf '%s[INFO]%s %s\n' "$_CI" "$_C0" "$*"; }
warn() { printf '%s[WARN]%s %s\n' "$_CW" "$_C0" "$*" >&2; }
fail() { printf '%s[FAIL]%s %s\n' "$_CE" "$_C0" "$*" >&2; exit 1; }
banner() { local l='================================================='; printf '\n%s\n  %s\n%s\n' "$l" "$*" "$l"; }
# Status line: value red when missing/failed, yellow while starting, green when OK.
row() {
  local c=''
  case "${2,,}" in
    not|*"not "*|*not-*|*missing*|*fail*|*error*|*inactive*|*disconnected*) c="$_CE" ;;
    *activating*|*reconnecting*|*starting*) c="$_CW" ;;
    *installed*|*active*|*connected*|*"logged in"*|*running*|*enabled*|*mounted*|*loaded*) c="$_CK" ;;
  esac
  printf '  %-16s%s%s%s\n' "$1" "$c" "$2" "${c:+$_C0}"
}
# <<< admonition

# Interactive numeric prompt that works under: bash -c "$(wget ...)"
asknum() { # asknum "prompt" min max default
    local p="$1" min="$2" max="$3" def="$4" in
    while true; do
        if [[ -r /dev/tty ]]; then
            read -rp "$p [$min-$max] (default: $def): " in </dev/tty || in="$def"
        else
            read -rp "$p [$min-$max] (default: $def): " in || in="$def"
        fi
        in="${in:-$def}"
        [[ "$in" =~ ^[0-9]+$ ]] || { echo "Enter a number."; continue; }
        (( in>=min && in<=max )) && { echo "$in"; return; }
    done
}

[[ $EUID -eq 0 ]] || fail "This script must be run as root (or via sudo)."

is_proxmox_host() {
    [[ -f /etc/pve/.version ]] || command -v pveversion &>/dev/null
}

GITHUB_BASE="https://github.com/therepos/proxmox/raw/main/apps"

# Fetch and execute a standalone installer from the repo. Keeps the standalone
# scripts the single source of truth. The ?$(date +%s) cache-buster forces the
# always-latest copy from 'main'. Args after the script name are passed through
# (e.g. run_remote setup-gpu.sh driver -y). Returns the remote script's own exit
# code so callers can act on it (notably exit 10 = "reboot required").
run_remote() {
    local script="$1"; shift
    local url
    url="${GITHUB_BASE}/${script}?$(date +%s)"
    local body
    body="$(wget -qLO- "$url")" || fail "Failed to download ${script} from ${GITHUB_BASE}."
    [[ -n "$body" ]] || fail "Downloaded ${script} but it was empty (bad URL / missing script?)."
    bash -c "$body" "$script" "$@"
}

# ============================================================================
# HOST SIDE — thin router. Real work lives in the standalones (create-vm.sh
# creates the VM + GPU passthrough; setup-virtiofs.sh maps + attaches the share).
# ============================================================================
if is_proxmox_host; then
    # Preserve prior behaviour: any explicit args go straight to setup-virtiofs.sh
    # (e.g. 'setup-vm.sh setup' / 'status' / 'remove') without showing the menu.
    if [[ $# -gt 0 ]]; then
        run_remote setup-virtiofs.sh "$@"
        exit $?
    fi

    # --- Memory adjustment ---------------------------------------------------
    # Changing memory needs the VM off. Two hard-won rules are enforced here:
    #   * Ballooning is incompatible with PCI passthrough (guest pages must stay
    #     pinned), so any VM with hostpci gets balloon forced to 0.
    #   * ALWAYS shut down gracefully. A hard `qm stop` can leave a passed-through
    #     GPU in a state where the VM starts but hangs before networking, with a
    #     black console and no obvious cause. Recovery is a full host reboot.
    adjust_memory() {
        banner "Adjust VM RAM"
        echo ""
        printf "  %-6s %-20s %-10s %s\n" "VMID" "NAME" "STATUS" "MEMORY"
        qm list | awk 'NR>1 {printf "  %-6s %-20s %-10s %s MB\n", $1, $2, $3, $4}'
        echo ""

        local ids first last vmid
        mapfile -t ids < <(qm list | awk 'NR>1 {print $1}')
        (( ${#ids[@]} )) || { warn "No VMs found."; return; }
        first="${ids[0]}"; last="${ids[-1]}"

        vmid="$(asknum '  VMID to modify' "$first" "$last" "$first")"
        qm status "$vmid" &>/dev/null || { warn "VM $vmid does not exist."; return; }

        local cur_mem cur_balloon has_gpu host_total_mb
        cur_mem="$(qm config "$vmid" | awk -F': ' '/^memory:/{print $2}')"
        cur_balloon="$(qm config "$vmid" | awk -F': ' '/^balloon:/{print $2}')"
        has_gpu="$(qm config "$vmid" | grep -c '^hostpci' || true)"
        host_total_mb="$(free -m | awk '/^Mem:/{print $2}')"

        echo ""
        echo "  Current memory : ${cur_mem} MB"
        echo "  Ballooning     : ${cur_balloon:-not set}"
        echo "  PCI passthrough: $( ((has_gpu)) && echo "yes (${has_gpu} device(s))" || echo no )"
        echo "  Host total RAM : ${host_total_mb} MB"
        echo ""
        echo "  Leave at least ~4096 MB for the Proxmox host itself."
        echo ""

        local new_gb max_gb
        max_gb=$(( host_total_mb / 1024 ))
        new_gb="$(asknum '  New memory in GB' 1 "$max_gb" $(( cur_mem / 1024 )) )"
        local new_mem=$(( new_gb * 1024 ))

        if (( new_mem + 4096 > host_total_mb )); then
            warn "That leaves under 4GB for the host. Choose a smaller value."
            return
        fi
        if [[ "$new_mem" == "$cur_mem" ]]; then
            info "Memory is already ${new_gb} GB. Nothing to do."
            return
        fi

        local was_running=0
        [[ "$(qm status "$vmid" | awk '{print $2}')" == "running" ]] && was_running=1

        if (( was_running )); then
            echo ""
            warn "VM ${vmid} must be shut down. Anything running on it goes offline."
            local c
            read -rp "  Continue? [y/N]: " c </dev/tty
            [[ "$c" =~ ^[Yy]$ ]] || { info "Cancelled."; return; }

            info "Shutting down gracefully (up to 180s)..."
            qm shutdown "$vmid" --timeout 180 &>/dev/null || true

            local waited=0
            while [[ "$(qm status "$vmid" | awk '{print $2}')" == "running" ]]; do
                sleep 5; waited=$((waited + 5))
                if (( waited >= 180 )); then
                    warn "VM did not shut down in time."
                    if ((has_gpu)); then
                        # Deliberately not offering a hard stop here: with a GPU
                        # attached that is exactly what wedges the device.
                        fail "Not forcing a stop on a passthrough VM. Shut it down from inside the guest, then re-run."
                    fi
                    read -rp "  Force stop? [y/N]: " c </dev/tty
                    [[ "$c" =~ ^[Yy]$ ]] || { info "Cancelled."; return; }
                    qm stop "$vmid"
                    break
                fi
            done
            ok "VM stopped."
        fi

        qm set "$vmid" --memory "$new_mem" >/dev/null || fail "Could not set memory."
        ok "Memory set to ${new_gb} GB."

        if ((has_gpu)) && [[ "${cur_balloon:-0}" != "0" ]]; then
            qm set "$vmid" --balloon 0 >/dev/null || true
            ok "Ballooning disabled (required for PCI passthrough)."
        fi

        if (( was_running )); then
            info "Starting VM..."
            qm start "$vmid" || fail "VM failed to start. Check: qm config $vmid"
            echo ""
            info "Booting. Services may take a few minutes to come back."
            ((has_gpu)) && info "If it does not respond, a host reboot clears a wedged GPU."
        else
            info "VM left powered off (it was not running)."
        fi
        echo ""
    }

    banner "Proxmox Host Setup"
    echo ""
    echo "  1) Create VM        (new Ubuntu VM + GPU passthrough)"
    echo "  2) Mount share      (VirtIO-FS only)"
    echo "  3) Adjust VM RAM    (resize VM RAM)"
    echo "  0) Exit"
    choice="$(asknum 'Choose' 0 3 1)"
    case "$choice" in
        0) ok "Bye."; exit 0 ;;
        1) run_remote create-vm.sh; exit $? ;;
        2) run_remote setup-virtiofs.sh "$@"; exit $? ;;
        3) adjust_memory; exit 0 ;;
    esac
    exit 0
fi

# ============================================================================
# VM SIDE
# ============================================================================
STATE_DIR="/var/lib/setup-vm"
COMPLETED_FILE="${STATE_DIR}/completed"   # one completed step name per line
APPS_FILE="${STATE_DIR}/apps"             # persisted VM_APPS selection (resume-safe)
VERIFY_NVIDIA_FLAG="${STATE_DIR}/verify-nvidia"
LOG_FILE="/var/log/setup-vm.log"
mkdir -p "$STATE_DIR"

# Base phase: fixed order. The reboot lives here (NVIDIA).
BASE_STEPS=(webmin disk docker nvidia virtiofs)

# App phase: order-free, opt-in via VM_APPS (none by default). Kasm is no longer
# installed here — install it manually with setup-kasm.sh if wanted. To opt an
# app back in: VM_APPS="kasm" bash -c "$(wget ... setup-vm.sh ...)".
VM_APPS="${VM_APPS:-}"

# --- Resume hook (re-runs this orchestrator after a reboot) ------------------
RESUME_SERVICE="/etc/systemd/system/setup-vm-resume.service"
RESUME_SCRIPT="${STATE_DIR}/resume.sh"

install_resume_hook() {
    cat > "$RESUME_SCRIPT" <<'INNEREOF'
#!/usr/bin/env bash
# setup-vm.sh tees its own output to the log, so we don't duplicate it here.
INNEREOF

    # Persist env overrides the standalones consume so they survive the reboot.
    [[ -n "${KASM_VERSION:-}" ]]          && echo "export KASM_VERSION=\"${KASM_VERSION}\"" >> "$RESUME_SCRIPT"
    [[ -n "${KASM_PORT:-}" ]]             && echo "export KASM_PORT=\"${KASM_PORT}\"" >> "$RESUME_SCRIPT"
    [[ -n "${NVIDIA_DRIVER_VERSION:-}" ]] && echo "export NVIDIA_DRIVER_VERSION=\"${NVIDIA_DRIVER_VERSION}\"" >> "$RESUME_SCRIPT"

    cat >> "$RESUME_SCRIPT" <<INNEREOF
bash -c "\$(wget -qLO- '${GITHUB_BASE}/setup-vm.sh?\$(date +%s)')"
INNEREOF
    chmod +x "$RESUME_SCRIPT"

    cat > "$RESUME_SERVICE" <<EOF
[Unit]
Description=setup-vm resume after reboot
After=network-online.target docker.service
Wants=network-online.target

[Service]
Type=oneshot
ExecStart=/bin/bash $RESUME_SCRIPT
RemainAfterExit=no

[Install]
WantedBy=multi-user.target
EOF

    systemctl daemon-reload
    systemctl enable setup-vm-resume.service
    ok "Resume hook installed (will run automatically after reboot)."
}

remove_resume_hook() {
    if [[ -f "$RESUME_SERVICE" ]]; then
        systemctl disable setup-vm-resume.service 2>/dev/null || true
        rm -f "$RESUME_SERVICE" "$RESUME_SCRIPT"
        systemctl daemon-reload
    fi
}

# --- Completed-step tracking -------------------------------------------------
step_completed() { [[ -f "$COMPLETED_FILE" ]] && grep -qxF "$1" "$COMPLETED_FILE"; }
mark_completed() { step_completed "$1" || echo "$1" >> "$COMPLETED_FILE"; }

# --- Base steps that keep a lightweight orchestrator-side guard --------------
# (Their standalones aren't fully self-skipping, so we avoid re-fetching/running
#  them when the thing is already present. Other steps rely on the standalone's
#  own idempotency.)
step_webmin() {
    if dpkg -l webmin &>/dev/null 2>&1; then
        ok "Webmin is already installed. Skipping."
        return 0
    fi
    run_remote setup-webmin.sh
}

step_docker() {
    if command -v docker &>/dev/null; then
        ok "Docker is already installed. Skipping."
        return 0
    fi
    run_remote setup-docker.sh
    # setup-docker.sh doesn't manage user groups; do it here so the invoking
    # user can run docker without sudo.
    local real_user="${SUDO_USER:-root}"
    if [[ "$real_user" != "root" ]]; then
        usermod -aG docker "$real_user" 2>/dev/null || true
        ok "User '${real_user}' added to docker group."
    fi
}

# --- Step dispatch + titles --------------------------------------------------
# Returns the action's exit code (10 = reboot required).
dispatch_step() {
    case "$1" in
        webmin)     step_webmin ;;
        docker)     step_docker ;;
        disk)       run_remote setup-disk.sh expand ;;
        nvidia)     run_remote setup-gpu.sh driver -y ;;
        virtiofs)   run_remote setup-virtiofs.sh mount ;;
        *)          run_remote "setup-${1}.sh" ;;   # convention: <app> -> setup-<app>.sh
    esac
}

step_title() {
    case "$1" in
        webmin)     echo "Webmin" ;;
        docker)     echo "Docker" ;;
        disk)       echo "Disk Expand" ;;
        nvidia)     echo "NVIDIA Driver" ;;
        virtiofs)   echo "VirtIO-FS Mount" ;;
        kasm)       echo "Kasm Workspaces" ;;
        *)          echo "$1" ;;
    esac
}

# --- The resumable plan loop -------------------------------------------------
run_plan() {
    local plan=("$@")
    local total=${#plan[@]} idx=0 step rc
    for step in "${plan[@]}"; do
        idx=$((idx+1))
        if step_completed "$step"; then
            ok "[${idx}/${total}] $(step_title "$step"): already done — skipping."
            continue
        fi

        echo ""
        echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        echo "  STEP ${idx}/${total}: $(step_title "$step")"
        echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        echo ""

        rc=0
        dispatch_step "$step" || rc=$?

        if [[ $rc -eq 10 ]]; then
            # Step installed something that needs a reboot (e.g. NVIDIA module).
            # Mark it completed so we don't re-run (and re-reboot) it on resume.
            mark_completed "$step"
            [[ "$step" == "nvidia" ]] && touch "$VERIFY_NVIDIA_FLAG"
            install_resume_hook
            echo ""
            echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
            echo "  REBOOTING (required by '$(step_title "$step")')..."
            echo "  Setup resumes automatically after reboot."
            echo "  Progress is logged to /var/log/setup-vm.log"
            echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
            echo ""
            sleep 3
            reboot
            exit 0  # unreachable
        elif [[ $rc -ne 0 ]]; then
            # Disarm the resume hook first, otherwise a failure that happened
            # after an earlier reboot would re-run this on every boot forever.
            remove_resume_hook
            warn "[$(step_title "$step")] failed (exit ${rc})."
            echo ""
            echo "  Completed so far: $(tr '\n' ' ' < "$COMPLETED_FILE" 2>/dev/null)"
            echo "  Log:              ${LOG_FILE}"
            echo ""
            echo "  Re-run this script to continue from '$(step_title "$step")',"
            echo "  or choose 'Reset setup state' to start over."
            echo ""
            exit "$rc"
        fi

        mark_completed "$step"
    done
}

# --- Friendly one-line summary of everything Full setup will run -------------
full_summary() {
    local apps_disp="" a
    for a in $VM_APPS; do apps_disp+=", ${a^}"; done   # capitalize each app name
    echo "Webmin, Disk, Docker, NVIDIA, VirtIO-FS${apps_disp}"
}

# --- Status (menu option) ----------------------------------------------------
vm_status() {
    banner "Status"
    row "Webmin"    "$(dpkg -l webmin &>/dev/null 2>&1 && echo installed || echo missing)"
    row "Docker"    "$(command -v docker >/dev/null && echo installed || echo missing)"
    row "NVIDIA"    "$( (command -v nvidia-smi >/dev/null && nvidia-smi &>/dev/null) && echo loaded || echo 'not loaded')"
    row "Kasm"      "$([[ -d /opt/kasm/current ]] && echo installed || echo missing)"
    row "Root disk" "$(df -h / 2>/dev/null | awk 'NR==2{print $3"/"$2" used ("$5")"}')"
    banner "/mnt/sec  $(mountpoint -q /mnt/sec 2>/dev/null && echo 'mounted (virtiofs)' || echo 'not mounted')"
    echo ""
}

# --- Access info -------------------------------------------------------------
# Computes everything LIVE (current IP, ports from config) so it is never stale.
# Used both by the menu and the completion summary.
print_access_block() {
    local ip webmin_port kasm_port
    ip="$(hostname -I 2>/dev/null | awk '{print $1}')"
    [[ -n "$ip" ]] || ip="<this-vm-ip>"
    webmin_port="$(awk -F= '/^port=/{print $2; exit}' /etc/webmin/miniserv.conf 2>/dev/null)"
    [[ -n "$webmin_port" ]] || webmin_port=10000
    kasm_port="$(docker port kasm_proxy 2>/dev/null | awk -F: 'NR==1{print $2}')"
    [[ -n "$kasm_port" ]] || kasm_port="${KASM_PORT:-443}"

    echo "  IP address   ${ip}"
    echo ""
    if dpkg -l webmin &>/dev/null 2>&1; then
        echo "  Webmin       https://${ip}:${webmin_port}"
        echo "               login: your system root credentials"
        echo ""
    fi
    if [[ -d /opt/kasm/current ]]; then
        echo "  Kasm         https://${ip}:${kasm_port}"
        echo "               user: admin@kasm.local   (password: as you set it)"
        echo ""
    fi
    echo "  VirtIO-FS    $(mountpoint -q /mnt/sec 2>/dev/null && echo '/mnt/sec (mounted)' || echo '/mnt/sec (not mounted)')"
    echo ""
    echo "  Full log     ${LOG_FILE}"
}

access_info() {
    banner "Access info"
    print_access_block
    echo "================================================="
    echo ""
}

# ============================================================================
# DISPATCH (VM side)
# ============================================================================
# Log the whole VM-side run — both the initial run and the post-reboot resume —
# to a single persistent file. (The resume hook no longer tees, to avoid dupes.)
# Decide colour from the real terminal first, force it on for delegated child
# scripts so colour survives the tee pipe, and strip ANSI on the way to the log
# so the log file stays clean text.
[[ -t 1 ]] && export FORCE_COLOR=1
exec > >(tee >(sed -u 's/\x1b\[[0-9;]*m//g' >> "$LOG_FILE")) 2>&1

banner "Ubuntu VM Setup"
echo ""

# Resume vs fresh: the COMPLETED_FILE exists only once a full setup has started,
# so its presence means "in progress" (e.g. resuming after the NVIDIA reboot).
# A genuine post-reboot resume is headless and must not prompt. A previous run
# that FAILED also leaves COMPLETED_FILE behind, but disarms the resume hook —
# so the hook, not the file, is what distinguishes the two. Without this, a
# failed run locks the user out of the menu permanently.
RESUMING=0
if [[ -f "$COMPLETED_FILE" ]] && systemctl is-enabled setup-vm-resume.service &>/dev/null; then
    RESUMING=1
fi

INTERRUPTED=0
[[ -f "$COMPLETED_FILE" && $RESUMING -eq 0 ]] && INTERRUPTED=1

reset_state() {
    remove_resume_hook
    rm -rf "$STATE_DIR"
    mkdir -p "$STATE_DIR"
    ok "Setup state cleared. The next Full setup starts from the beginning."
    echo ""
}

if [[ $RESUMING -eq 0 ]]; then
    if [[ $INTERRUPTED -eq 1 ]]; then
        echo ""
        warn "A previous setup did not finish."
        echo "  Done so far: $(tr '\n' ' ' < "$COMPLETED_FILE" 2>/dev/null)"
        echo "  'Full setup' resumes from where it stopped."
        echo "  'Reset setup state' starts over from scratch."
        echo ""
    fi

    # Fresh run: interactive menu loop (skipped entirely on resume so the
    # headless post-reboot service never blocks on input). Mount/Status/Access
    # info return to the menu; only Full setup and Exit end the loop.
    while true; do
        echo "  1) Full setup    ($(full_summary))"
        echo "  2) Mount share   (VirtIO-FS only)"
        echo "  3) Disk          (check usage / expand)"
        echo "  4) Status"
        echo "  5) Access info"
        echo "  6) Reset setup state"
        echo "  0) Exit"
        choice="$(asknum 'Choose' 0 6 0)"
        case "$choice" in
            0) ok "Bye."; exit 0 ;;
            1) echo ""; break ;;                         # proceed to full setup
            2) run_remote setup-virtiofs.sh mount; echo "" ;;
            3) run_remote setup-disk.sh; echo "" ;;
            4) vm_status ;;
            5) access_info ;;
            6) reset_state; INTERRUPTED=0 ;;
        esac
    done

    # Lock in the chosen app list, but only on a genuinely fresh run — a resume
    # after failure must keep its completed-step list, or it starts over.
    if [[ $INTERRUPTED -eq 0 ]]; then
        echo "$VM_APPS" > "$APPS_FILE"
        : > "$COMPLETED_FILE"
    else
        [[ -f "$APPS_FILE" ]] && VM_APPS="$(cat "$APPS_FILE")"
        info "Resuming from the last completed step."
    fi
else
    info "Resuming setup after reboot..."
    # Use the app list chosen at the start of this run, not the current env.
    [[ -f "$APPS_FILE" ]] && VM_APPS="$(cat "$APPS_FILE")"
fi

# Post-reboot NVIDIA verification (runs once, only after an NVIDIA reboot).
# This is also the loop-breaker: nvidia is already marked completed, so it won't
# re-run; here we just confirm the kernel module actually loaded.
if [[ -f "$VERIFY_NVIDIA_FLAG" ]]; then
    info "Verifying NVIDIA driver after reboot..."
    if nvidia-smi &>/dev/null; then
        ok "NVIDIA driver is loaded!"
        nvidia-smi
        rm -f "$VERIFY_NVIDIA_FLAG"
    else
        rm -f "$VERIFY_NVIDIA_FLAG"
        fail "NVIDIA driver failed to load after reboot. Check dmesg for errors."
    fi
fi

# Run base phase (fixed order) then app phase (order-free). Completed steps are
# skipped, so this is safe to re-enter after the reboot.
run_plan "${BASE_STEPS[@]}" $VM_APPS

# --- Done --------------------------------------------------------------------
remove_resume_hook

banner "VM Setup Complete!"
echo ""
print_access_block
echo ""
echo "  A self-signed certificate warning is expected for the web UIs."
echo ""

# Cleanup state (the log at $LOG_FILE is kept — it lives outside STATE_DIR).
rm -rf "$STATE_DIR"
ok "Setup state cleaned up. All done!"
echo ""
