###############################################################################
# Justfile - convenience commands for building and testing the image
# Run `just` (no args) to see the list. Requires `just` (brew install just).
###############################################################################

image_name      := "kamos"
default_tag     := "stable"
bib_image       := "quay.io/centos-bootc/bootc-image-builder:latest"

# Show available recipes.
default:
    @just --list

# Build the OCI image with podman.
# On an Apple Silicon Mac you MUST pass --platform=linux/amd64 because the
# upstream Bazzite NVIDIA image is x86_64 only. This will use qemu emulation
# and will be slow. CI is the much faster path.
build target_image=image_name tag=default_tag:
    podman build \
        --platform=linux/amd64 \
        --pull=newer \
        --tag {{ target_image }}:{{ tag }} \
        .

# Build a QCOW2 disk image (good for testing in VMware/UTM/QEMU).
build-qcow2 target_image=image_name tag=default_tag:
    just _build-disk {{ target_image }} {{ tag }} qcow2

# Build an ISO suitable for installation onto a real NVIDIA PC.
build-iso target_image=image_name tag=default_tag:
    just _build-disk {{ target_image }} {{ tag }} iso

# Build a raw disk image.
build-raw target_image=image_name tag=default_tag:
    just _build-disk {{ target_image }} {{ tag }} raw

# Internal: invoke bootc-image-builder.
_build-disk target_image tag type:
    mkdir -p output
    podman run \
        --rm \
        --privileged \
        --pull=newer \
        --security-opt label=type:unconfined_t \
        -v $(pwd)/output:/output \
        -v $(pwd)/disk_config:/config:ro \
        -v /var/lib/containers/storage:/var/lib/containers/storage \
        {{ bib_image }} \
        --type {{ type }} \
        --use-librepo=True \
        --local \
        localhost/{{ target_image }}:{{ tag }}

# Run the QCOW2 image in QEMU directly (fast, headless-friendly).
run-vm-qcow2:
    qemu-system-x86_64 \
        -enable-kvm \
        -M q35 \
        -cpu host \
        -smp 4 \
        -m 6G \
        -drive file=output/qcow2/disk.qcow2,if=virtio \
        -nic user,model=virtio-net-pci

# Lint shell scripts.
lint:
    shellcheck build_files/*.sh

# Format shell scripts.
format:
    shfmt -w build_files/*.sh

# Validate the Justfile itself.
check:
    just --fmt --check --unstable

# Remove build artifacts.
clean:
    rm -rf output
