#!/bin/bash
# delinuxco-helper-scripts: version 0.1.4

set -euo pipefail  # Better error handling (e: exit on error, u: error on unset variables, o pipefail: catch errors in pipes)

# Configuration
PACKAGES=(qemu-full virt-manager vde2 dnsmasq iproute2 edk2-ovmf)
LOG_PREFIX="[Virt-Installer]"

# Function for styled logging
log() {
    echo -e "${LOG_PREFIX} $1"
}

# Check if running on Arch Linux (checks for pacman)
if ! command -v pacman &> /dev/null; then
    log "Error: This script is designed for Arch Linux (pacman not found)."
    exit 1
fi

log "Starting installation process..."

# 1. Install Packages
log "Installing packages: ${PACKAGES[*]}..."
sudo pacman -S --noconfirm "${PACKAGES[@]}"

# 2. User Permissions
log "Adding user '$USER' to the libvirt group..."
sudo usermod -G libvirt -a "$USER"

# 3. Service Management
log "Enabling and starting libvirtd service..."
sudo systemctl enable --now libvirtd.service

# 4. Network Configuration
log "Configuring default virtual network..."
# We use '|| true' here because if the network is already started, 
# virsh might return an error code which would trigger 'set -e' to exit the script.
sudo virsh net-start default || log "Network 'default' already running."
sudo virsh net-autostart default

log "Installation finished successfully!"
log "ACTION REQUIRED: Please reboot or log out/in to apply group changes."

exit 0


