#!/usr/bin/env bash
# Build Live USB ISO from flake configuration
# Creates an uncompressed ISO suitable for Ventoy and other USB boot tools
# Usage: nix run .#build-liveusb
# Or: ./scripts/build-liveusb.sh

set -euo pipefail

REPO_ROOT="${REPO_ROOT:-$(git rev-parse --show-toplevel 2>/dev/null || pwd)}"

echo "Building Live USB ISO..."
nix build "${REPO_ROOT}#nixosConfigurations.live-usb.config.system.build.isoImage" --show-trace

if ls "${REPO_ROOT}/result/iso/"*.iso 1> /dev/null 2>&1; then
    iso_file=$(ls "${REPO_ROOT}/result/iso/"*.iso)
    echo "Build complete!"
    echo "ISO location: $iso_file"
    echo "Ready for Ventoy or dd to USB"
else
    echo "Build failed - no ISO file found"
    exit 1
fi
