#!/usr/bin/env bash

# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2023-2025 <Nitrux Latinoamericana S.C. <hello@nxos.org>>


# -- Exit on errors.

set -eu


# -- Variables.

TOOL_NAME="Calamares Startup"


# -- Environment variables.

export QT_QUICK_CONTROLS_STYLE=org.kde.breeze


# -- Print informative messages to stderr.

puts_info() {
	if [ -n "$1" ]; then
		printf "%s: \e[34mInfo:\e[0m %s\n" "$TOOL_NAME" "$*" >&2
	fi
}


# -- Print success messages to stderr.

puts_success() {
	if [ -n "$1" ]; then
		printf "%s: \e[32mSuccess:\e[0m %s\n" "$TOOL_NAME" "$*" >&2
	fi
}


# -- Print error messages to stderr.

puts_error() {
	if [ -n "$1" ]; then
		printf "%s: \e[31mError:\e[0m %b\n" "$TOOL_NAME" "$*" >&2
	fi
}


# -- Unmount swap before starting Calamres; otherwise, Calamares can't partition disks when running in a VM. I do not know why swap is automatically mounted *ONLY* in Live sessions in VMs.

if swapon --show | grep -q '/dev/'; then
    puts_info "SWAP partition is currently mounted. Unmounting..."
    swapoff -a
    puts_success "SWAP partition has been unmounted."
else
    puts_error "SWAP partition is not mounted."
fi


# -- Launch Calamares.

puts_info "Starting Calamares..."

calamares -D6
