#!/bin/bash
# Shared settings, sourced by the other scripts in this directory.

VM_NAME="cachyos"
VM_DISK="/var/lib/libvirt/images/${VM_NAME}.qcow2"
VM_DISK_SIZE="64G"
VM_MEMORY_MB="8192"
VM_VCPUS="4"
VM_NETWORK="br0"

ISO_VERSION="260809"
ISO_NAME="cachyos-desktop-linux-${ISO_VERSION}.iso"
ISO_DIR="/scratch/isos"
ISO_PATH="${ISO_DIR}/${ISO_NAME}"
ISO_URL="https://mirror.cachyos.org/ISO/desktop/${ISO_VERSION}/${ISO_NAME}"

SSH_USER="abaumann"
VM_IP="192.168.1.135"
# Password used only for the initial console bootstrap (enabling sshd/ufw)
# and the very first sudo prompts over SSH, before SSH keys are installed.
# Override with: export VM_PASSWORD=...
VM_PASSWORD="${VM_PASSWORD:-123qwe}"

ML4W_DOTINST_URL="https://raw.githubusercontent.com/mylinuxforwork/dotfiles/main/hyprland-dotfiles-stable.dotinst"

# --- Target selection (VM vs. an already-themed local laptop) ---------
#
# Scripts from 06- onward can act on either the libvirt VM (over SSH) or
# directly on a local machine that already has the same ML4W dotfiles
# profile installed. TARGET picks which; default is "vm" to match the
# existing behavior of those scripts.
TARGET="vm"

# Scans "$@" for a --vm / --local flag, sets TARGET accordingly, and
# leaves the remaining arguments in the global PARSED_ARGS array for the
# caller to re-collect. Must be called directly (NOT inside a `<(...)`
# process substitution / command substitution), since those run in a
# subshell and the TARGET assignment would not survive. Usage in a
# script, after `source ./00-config.sh`:
#
#   parse_target_flag "$@"
#   set -- "${PARSED_ARGS[@]+"${PARSED_ARGS[@]}"}"
parse_target_flag() {
    PARSED_ARGS=()
    for arg in "$@"; do
        case "$arg" in
            --vm) TARGET="vm" ;;
            --local) TARGET="local" ;;
            *) PARSED_ARGS+=("$arg") ;;
        esac
    done
}

# Runs a single shell command string either over SSH on the VM or
# directly on the local machine, depending on TARGET. A heredoc attached
# to the call becomes that command's stdin either way, so `run "cat >
# file" <<'EOF' ... EOF` works in both modes.
run() {
    if [ "$TARGET" = "local" ]; then
        bash -c "$1"
    else
        ssh "${SSH_USER}@${VM_IP}" "$1"
    fi
}

# Like run, but passes "$@" through as positional params to a `bash -s
# --` invocation, for heredoc scripts that reference $1, $2, ...
# internally.
run_script() {
    if [ "$TARGET" = "local" ]; then
        bash -s -- "$@"
    else
        ssh "${SSH_USER}@${VM_IP}" bash -s -- "$@"
    fi
}
