#!/usr/bin/env just --justfile

# Simple CLI - particle-os build automation
# Based on Aurora's Justfile pattern

# Default recipe to display help
default:
    @just --list

# Build the container image
build:
    #!/usr/bin/env bash
    echo "Building Simple CLI container image..."
    podman build -f Containerfile -t localhost/simple-cli:latest .
    echo "Build complete: localhost/simple-cli:latest"

# Build the container image with Particle-OS tools
build-with-tools:
    #!/usr/bin/env bash
    echo "Building Simple CLI container image with Particle-OS tools..."
    echo "This will include: apt-ostree, deb-bootupd, and bootc"
    podman build -f Containerfile -t localhost/simple-cli:latest .
    echo "Build complete: localhost/simple-cli:latest"
    echo ""
    echo "Testing tools..."
    podman run --rm localhost/simple-cli:latest verify-tools

# Build and push to registry
build-push registry="localhost":
    #!/usr/bin/env bash
    echo "Building and pushing to {{registry}}..."
    podman build -f Containerfile -t {{registry}}/simple-cli:latest .
    podman push {{registry}}/simple-cli:latest
    echo "Pushed to {{registry}}/simple-cli:latest"

# Generate bootable image using bootc-image-builder
generate-bootable:
    #!/usr/bin/env bash
    echo "Generating bootable image..."
    mkdir -p output
    podman run --rm --privileged \
        -v $PWD/output:/output \
        -v /var/lib/containers/storage:/var/lib/containers/storage \
        quay.io/centos-bootc/bootc-image-builder:latest \
        --type iso,raw \
        --output /output \
        localhost/simple-cli:latest
    echo "Bootable image generated in output/"

# Generate bootable image with Particle-OS tools
generate-bootable-with-tools:
    #!/usr/bin/env bash
    echo "Generating bootable image with Particle-OS tools..."
    echo "This will create a disk image with apt-ostree, deb-bootupd, and bootc integrated"
    mkdir -p output
    podman run --rm --privileged \
        -v $PWD/output:/output \
        -v /var/lib/containers/storage:/var/lib/containers/storage \
        quay.io/centos-bootc/bootc-image-builder:latest \
        --type iso,raw \
        --output /output \
        localhost/simple-cli:latest
    echo ""
    echo "Bootable image with Particle-OS tools generated in output/"
    echo "Files created:"
    ls -la output/

# Test the container
test:
    #!/usr/bin/env bash
    echo "Testing Simple CLI container..."
    podman run --rm localhost/simple-cli:latest \
        bash -c "echo 'System info:' && uname -a && echo 'Packages:' && dpkg -l | wc -l"
    echo "Test complete"

# Test Particle-OS tools integration
test-tools:
    #!/usr/bin/env bash
    echo "Testing Particle-OS tools integration..."
    echo ""
    echo "=== Tool Verification ==="
    podman run --rm localhost/simple-cli:latest verify-tools
    echo ""
    echo "=== Basic Functionality Test ==="
    podman run --rm localhost/simple-cli:latest bash -c "
        echo 'Testing apt-ostree...' && apt-ostree --help | head -5
        echo ''
        echo 'Testing bootupctl...' && bootupctl --help | head -5  
        echo ''
        echo 'Testing bootc...' && bootc --help | head -5
    "
    echo "Tools test complete"

# Clean build artifacts
clean:
    #!/usr/bin/env bash
    echo "Cleaning build artifacts..."
    rm -rf output/
    podman image prune -f
    podman system prune -f
    echo "Cleanup complete"

# Install ujust recipes for end users
install-ujust:
    #!/usr/bin/env bash
    sudo mkdir -p /usr/share/particle-os/just
    sudo cp -r usr/share/particle-os/just/* /usr/share/particle-os/just/
    echo "ujust recipes installed system-wide"

# Validate the recipe
validate:
    #!/usr/bin/env bash
    echo "Validating recipe.yml..."
    if command -v bluebuild >/dev/null 2>&1; then
        bluebuild validate recipe.yml
    else
        echo "bluebuild not installed, skipping validation"
    fi

# Full build pipeline
pipeline: build test generate-bootable
    @echo "Full build pipeline completed"

# Full build pipeline with Particle-OS tools
pipeline-with-tools: build-with-tools test-tools generate-bootable-with-tools
    @echo "Full build pipeline with Particle-OS tools completed"
    @echo ""
    @echo "🎉 Your Particle-OS tools are now integrated and ready!"
    @echo "📦 apt-ostree: Atomic package management"
    @echo "🔧 bootupd: Bootloader update management"  
    @echo "📦 bootc: Container to bootable image conversion"
    @echo ""
    @echo "Next steps:"
    @echo "1. Test the bootable image in QEMU"
    @echo "2. Verify all tools work correctly"
    @echo "3. Create your Particle-OS variants"

# Development environment setup
setup-dev:
    #!/usr/bin/env bash
    echo "Setting up development environment..."
    sudo apt update
    sudo apt install -y podman just podman-toolbox distrobox flatpak uidmap libsubid5 bash-completion
    echo "Development environment ready"

# Create development toolbox
create-dev-toolbox:
    #!/usr/bin/env bash
    echo "Creating development toolbox..."
    toolbox create dev
    echo "Development toolbox created. Enter with: toolbox enter dev"

# Enter development environment (toolbox)
dev-toolbox:
    #!/usr/bin/env bash
    echo "Entering development toolbox..."
    toolbox enter dev

# Create development distrobox
create-dev-distrobox:
    #!/usr/bin/env bash
    echo "Creating development distrobox..."
    distrobox create dev --image debian:trixie
    echo "Development distrobox created. Enter with: distrobox enter dev"

# Enter development environment (distrobox)
dev-distrobox:
    #!/usr/bin/env bash
    echo "Entering development distrobox..."
    distrobox enter dev

# Test toolbox functionality
test-toolbox:
    #!/usr/bin/env bash
    echo "Testing toolbox functionality..."
    toolbox --help
    echo "Toolbox test complete"

# Test distrobox functionality
test-distrobox:
    #!/usr/bin/env bash
    echo "Testing distrobox functionality..."
    distrobox --help
    echo "Distrobox test complete"

# List all development environments
list-envs:
    #!/usr/bin/env bash
    echo "=== Toolbox Environments ==="
    toolbox list 2>/dev/null || echo "No toolbox environments found"
    echo ""
    echo "=== Distrobox Environments ==="
    distrobox list 2>/dev/null || echo "No distrobox environments found"

# Clean up development environments
cleanup-envs:
    #!/usr/bin/env bash
    echo "Cleaning up development environments..."
    echo "Removing toolbox environments..."
    toolbox list | grep -v "toolbox" | xargs -I {} toolbox rm {} 2>/dev/null || true
    echo "Removing distrobox environments..."
    distrobox list | grep -v "distrobox" | xargs -I {} distrobox rm {} 2>/dev/null || true
    echo "Cleanup complete"

# Show system information
info:
    #!/usr/bin/env bash
    echo "=== Simple CLI System Information ==="
    echo "Container image: localhost/simple-cli:latest"
    echo "Base: Debian Trixie"
    echo "Architecture: particle-os with OSTree"
    echo "Bootloader: bootupd"
    echo "Kernel: linux-image-amd64"
    echo ""
    echo "=== Development Tools ==="
    echo "Toolbox: Available (podman-toolbox)"
    echo "Distrobox: Available (distrobox)"
    echo "Container runtime: Podman"
    echo "================================"

# Test boot performance
test-boot:
    #!/usr/bin/env bash
    echo "Testing boot performance..."
    echo "Creating bootable image..."
    cd .. && ./deb-bootc-image-builder/build.sh simple-cli:latest simple-cli-boot-test.qcow2
    echo "Boot test image created: simple-cli-boot-test.qcow2"

# Boot performance optimization (legacy)
optimize-boot-legacy:
    #!/usr/bin/env bash
    echo "Implementing boot performance optimizations..."
    echo "1. Enabling verbose GRUB logging..."
    echo "2. Enabling verbose kernel logging..."
    echo "3. Enabling verbose init system logging..."
    echo "4. Measuring current boot times..."
    echo "Boot optimization complete"

# Enable verbose boot logging (legacy)
enable-verbose-boot-legacy:
    #!/usr/bin/env bash
    echo "Enabling verbose boot logging for performance analysis..."
    
    # GRUB verbose logging
    echo "GRUB_CMDLINE_LINUX_DEFAULT=\"console=ttyS0 root=/dev/sda1 rw quiet splash fastboot\"" >> /etc/default/grub
    echo "GRUB_CMDLINE_LINUX=\"console=ttyS0 root=/dev/sda1 rw quiet splash fastboot\"" >> /etc/default/grub
    
    # Kernel verbose logging
    echo "kernel.printk = 7 4 1 7" >> /etc/sysctl.conf
    
    # Systemd verbose logging
    echo "LogLevel=debug" >> /etc/systemd/system.conf
    
    echo "Verbose boot logging enabled"

# Measure boot performance
measure-boot:
    #!/usr/bin/env bash
    echo "Measuring boot performance..."
    echo "Current boot time measurement tools:"
    systemd-analyze time
    systemd-analyze blame
    systemd-analyze critical-chain
    echo "Boot performance measurement complete"

# Boot performance analysis and optimization
analyze-boot:
    #!/usr/bin/env bash
    echo "Analyzing boot performance..."
    ./scripts/analyze-boot.sh analyze

enable-verbose-boot:
    #!/usr/bin/env bash
    echo "Enabling verbose boot logging..."
    ./scripts/analyze-boot.sh verbose

optimize-boot:
    #!/usr/bin/env bash
    echo "Optimizing boot performance..."
    ./scripts/analyze-boot.sh optimize

full-boot-optimization:
    #!/usr/bin/env bash
    echo "Running full boot optimization..."
    ./scripts/analyze-boot.sh all

# Quick boot performance check
quick-boot-check:
    #!/usr/bin/env bash
    echo "Quick boot performance check..."
    if command -v systemd-analyze >/dev/null 2>&1; then
        echo "⏱️  Boot time:"
        systemd-analyze time
        echo "🐌 Slowest service:"
        systemd-analyze blame | head -1
    else
        echo "❌ systemd-analyze not available"
    fi

# OSTree deployment setup
create-ostree-deployment:
    #!/usr/bin/env bash
    echo "🌱 Creating OSTree deployment for Simple-CLI..."
    echo "This script must be run INSIDE the container:"
    echo ""
    echo "1. Enter the container:"
    echo "   podman run -it localhost/simple-cli:latest /bin/bash"
    echo ""
    echo "2. Run the deployment script:"
    echo "   ./scripts/create-ostree-deployment.sh"
    echo ""
    echo "3. Exit and rebuild the bootable image"
