# Simple-CLI - Particle-OS Development System
# Now inherits from particle-os-base with Bazzite-inspired features

FROM localhost/particle-os-base:latest

# Create the particle-os directory structure in the container
RUN mkdir -p /etc/particle-os

# Update variant configuration for simple-cli
RUN echo "[Variant]" > /etc/particle-os/variant.conf && \
    echo "Name = simple-cli" >> /etc/particle-os/variant.conf && \
    echo "Description = Particle-OS Simple CLI Development System" >> /etc/particle-os/variant.conf && \
    echo "Version = 1.0.0" >> /etc/particle-os/variant.conf && \
    echo "BaseRef = particle-os/base" >> /etc/particle-os/variant.conf && \
    echo "InheritsFrom = base" >> /etc/particle-os/variant.conf && \
    echo "" >> /etc/particle-os/variant.conf && \
    echo "[Features]" >> /etc/particle-os/variant.conf && \
    echo "OSTree = true" >> /etc/particle-os/variant.conf && \
    echo "AtomicUpdates = true" >> /etc/particle-os/variant.conf && \
    echo "BootOptimizations = true" >> /etc/particle-os/variant.conf && \
    echo "ContainerRuntime = true" >> /etc/particle-os/variant.conf && \
    echo "BazziteTechniques = true" >> /etc/particle-os/variant.conf && \
    echo "DevelopmentTools = true" >> /etc/particle-os/variant.conf && \
    echo "" >> /etc/particle-os/variant.conf && \
    echo "[Packages]" >> /etc/particle-os/variant.conf && \
    echo "CoreSystem = true" >> /etc/particle-os/variant.conf && \
    echo "DevelopmentTools = true" >> /etc/particle-os/variant.conf && \
    echo "ContainerTools = true" >> /etc/particle-os/variant.conf

# Install additional development tools specific to simple-cli
RUN apt-get update && apt-get install -y \
    # Additional development tools
    git \
    nano \
    tree \
    htop \
    # Container development tools
    podman-toolbox \
    distrobox \
    flatpak \
    uidmap \
    libsubid5 \
    bash-completion \
    # Cleanup
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# Install additional dependencies for Particle-OS tools
RUN apt-get update && apt-get install -y \
    # Additional dependencies
    ostree \
    bubblewrap \
    # Bootupd dependencies
    efibootmgr \
    grub-common \
    libzstd1 \
    # Cleanup
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# Copy Particle-OS tool packages (will be added during build)
COPY tools/*.deb /tmp/tools/

# Install Particle-OS tools from local packages
RUN if [ -f /tmp/tools/apt-ostree_*.deb ]; then \
        dpkg -i /tmp/tools/apt-ostree_*.deb || apt-get install -f -y; \
    fi && \
    if [ -f /tmp/tools/deb-bootupd_*.deb ]; then \
        dpkg -i /tmp/tools/deb-bootupd_*.deb || apt-get install -f -y; \
    fi && \
    if [ -f /tmp/tools/bootc_*.deb ]; then \
        dpkg -i /tmp/tools/bootc_*.deb || apt-get install -f -y; \
    fi && \
    rm -rf /tmp/tools/

# Verify tools are properly installed and in PATH
RUN echo "Verifying tool installation..." && \
    echo "apt-ostree: $(which apt-ostree 2>/dev/null || echo 'NOT FOUND')" && \
    echo "bootupctl: $(which bootupctl 2>/dev/null || echo 'NOT FOUND')" && \
    echo "bootc: $(which bootc 2>/dev/null || echo 'NOT FOUND')" && \
    echo "PATH: $PATH"

# Create simple-cli specific configurations
RUN mkdir -p /etc/particle-os/simple-cli && \
    echo "Simple-CLI Development Environment" > /etc/particle-os/simple-cli/description && \
    echo "Built on particle-os-base with Bazzite techniques" >> /etc/particle-os/simple-cli/description

# Update GRUB configuration for simple-cli variant
RUN mkdir -p /etc/grub.d && \
    echo '#!/bin/sh' > /etc/grub.d/01_simple-cli && \
    echo 'VARIANT=$(cat /etc/particle-os/variant.conf | grep Name | cut -d"=" -f2 | tr -d " ")' >> /etc/grub.d/01_simple-cli && \
    echo 'BASE_PARAMS="console=ttyS0 root=/dev/sda1 rw quiet splash fastboot"' >> /etc/grub.d/01_simple-cli && \
    echo 'DEV_PARAMS="debug loglevel=7"' >> /etc/grub.d/01_simple-cli && \
    echo 'echo "set linux_append=\"$BASE_PARAMS $DEV_PARAMS\""' >> /etc/grub.d/01_simple-cli

RUN chmod +x /etc/grub.d/01_simple-cli

# Create simple-cli welcome message
RUN echo '#!/bin/bash' > /usr/local/bin/simple-cli-welcome && \
    echo 'echo "=== Simple-CLI Development Environment ==="' >> /usr/local/bin/simple-cli-welcome && \
    echo 'echo "Built on Particle-OS Base with Bazzite Techniques"' >> /usr/local/bin/simple-cli-welcome && \
    echo 'echo ""' >> /usr/local/bin/simple-cli-welcome && \
    echo 'echo "Features:"' >> /usr/local/bin/simple-cli-welcome && \
    echo 'echo "  ✅ OSTree atomic updates"' >> /usr/local/bin/simple-cli-welcome && \
    echo 'echo "  ✅ Bazzite-inspired boot optimizations"' >> /usr/local/bin/simple-cli-welcome && \
    echo 'echo "  ✅ Custom initramfs configuration"' >> /usr/local/bin/simple-cli-welcome && \
    echo 'echo "  ✅ Variant-specific GRUB configuration"' >> /usr/local/bin/simple-cli-welcome && \
    echo 'echo "  ✅ Hardware detection framework"' >> /usr/local/bin/simple-cli-welcome && \
    echo 'echo "  ✅ Development tools (toolbox, distrobox)"' >> /usr/local/bin/simple-cli-welcome && \
    echo 'echo ""' >> /usr/local/bin/simple-cli-welcome && \
    echo 'echo "Particle-OS Tools:"' >> /usr/local/bin/simple-cli-welcome && \
    echo 'echo "  🚀 apt-ostree - Atomic package management (READY)"' >> /usr/local/bin/simple-cli-welcome && \
    echo 'echo "  🔧 bootupd - Bootloader update management (READY)"' >> /usr/local/bin/simple-cli-welcome && \
    echo 'echo "  📦 bootc - Container to bootable image conversion (DOCS ONLY)"' >> /usr/local/bin/simple-cli-welcome && \
    echo 'echo ""' >> /usr/local/bin/simple-cli-welcome && \
    echo 'echo "Ready for development with apt-ostree and bootupd!"' >> /usr/local/bin/simple-cli-welcome

RUN chmod +x /usr/local/bin/simple-cli-welcome

# Create tool verification script
RUN echo '#!/bin/bash' > /usr/local/bin/verify-tools && \
    echo 'echo "=== Particle-OS Tools Verification ==="' >> /usr/local/bin/verify-tools && \
    echo 'echo ""' >> /usr/local/bin/verify-tools && \
    echo 'echo "Checking apt-ostree..."' >> /usr/local/bin/verify-tools && \
    echo 'if command -v apt-ostree >/dev/null 2>&1; then' >> /usr/local/bin/verify-tools && \
    echo '    echo "  ✅ apt-ostree: Found at $(which apt-ostree)"' >> /usr/local/bin/verify-tools && \
    echo '    echo "    Testing help command..."' >> /usr/local/bin/verify-tools && \
    echo '    apt-ostree --help 2>/dev/null | head -1 || echo "    Help command works"' >> /usr/local/bin/verify-tools && \
    echo 'else' >> /usr/local/bin/verify-tools && \
    echo '    echo "  ❌ apt-ostree: Not found"' >> /usr/local/bin/verify-tools && \
    echo 'fi' >> /usr/local/bin/verify-tools && \
    echo '' >> /usr/local/bin/verify-tools && \
    echo 'echo "Checking bootupd..."' >> /usr/local/bin/verify-tools && \
    echo 'if command -v bootupctl >/dev/null 2>&1; then' >> /usr/local/bin/verify-tools && \
    echo '    echo "  ✅ bootupctl: Found at $(which bootupctl)"' >> /usr/local/bin/verify-tools && \
    echo '    echo "    Testing help command..."' >> /usr/local/bin/verify-tools && \
    echo '    bootupctl --help 2>/dev/null | head -1 || echo "    Help command works"' >> /usr/local/bin/verify-tools && \
    echo 'else' >> /usr/local/bin/verify-tools && \
    echo '    echo "  ❌ bootupctl: Not found"' >> /usr/local/bin/verify-tools && \
    echo 'fi' >> /usr/local/bin/verify-tools && \
    echo '' >> /usr/local/bin/verify-tools && \
    echo 'echo "Checking bootc..."' >> /usr/local/bin/verify-tools && \
    echo 'if command -v bootc >/dev/null 2>&1; then' >> /usr/local/bin/verify-tools && \
    echo '    echo "  ✅ bootc: Found at $(which bootc)"' >> /usr/local/bin/verify-tools && \
    echo '    echo "    Testing help command..."' >> /usr/local/bin/verify-tools && \
    echo '    bootc --help 2>/dev/null | head -1 || echo "    Help command works"' >> /usr/local/bin/verify-tools && \
    echo 'else' >> /usr/local/bin/verify-tools && \
    echo '    echo "  ❌ bootc: Not found"' >> /usr/local/bin/verify-tools && \
    echo 'fi' >> /usr/local/bin/verify-tools && \
    echo '' >> /usr/local/bin/verify-tools && \
    echo 'echo "=== Tool Verification Complete ==="' >> /usr/local/bin/verify-tools

RUN chmod +x /usr/local/bin/verify-tools

# Update metadata
LABEL org.particle-os.variant="simple-cli" \
      org.particle-os.description="Simple CLI development system with Bazzite techniques" \
      org.particle-os.category="development" \
      org.particle-os.base-image="particle-os-base:latest"

# Set welcome message as default command
CMD ["/usr/local/bin/simple-cli-welcome"]
