#!/bin/bash

echo "Creating final working bootable qcow2..."

# Clean up
rm -f debian-bootc-final.qcow2

# Create qcow2
qemu-img create -f qcow2 debian-bootc-final.qcow2 5G

# Use virt-make-fs if available, otherwise manual approach
if command -v virt-make-fs >/dev/null 2>&1; then
    echo "Using virt-make-fs..."
    # Export container
    TEMP_DIR=$(mktemp -d)
    CONTAINER_ID=$(podman create git.raines.xyz/particle-os/debian-bootc:latest-with-fixes)
    podman export "$CONTAINER_ID" | tar -xC "$TEMP_DIR"
    podman rm "$CONTAINER_ID"
    
    # Create bootable filesystem
    virt-make-fs --format=qcow2 --size=5G --type=ext4 "$TEMP_DIR" debian-bootc-final.qcow2
    
    rm -rf "$TEMP_DIR"
else
    echo "Using manual approach with proper boot setup..."
    
    # Create raw image
    dd if=/dev/zero of=temp-final.raw bs=1M count=500 2>/dev/null
    
    # Set up loopback
    sudo losetup -f temp-final.raw
    LOOP_DEV=$(sudo losetup -j temp-final.raw | cut -d: -f1)
    
    # Create partition table
    echo -e "n\np\n1\n\n\nt\n83\na\nw" | sudo fdisk "$LOOP_DEV" >/dev/null 2>&1
    sudo partprobe "$LOOP_DEV"
    
    # Format
    sudo mkfs.ext4 -F "${LOOP_DEV}p1"
    
    # Mount and setup
    MOUNT_DIR=$(mktemp -d)
    sudo mount "${LOOP_DEV}p1" "$MOUNT_DIR"
    
    # Export container
    TEMP_DIR=$(mktemp -d)
    CONTAINER_ID=$(podman create git.raines.xyz/particle-os/debian-bootc:latest-with-fixes)
    podman export "$CONTAINER_ID" | tar -xC "$TEMP_DIR"
    podman rm "$CONTAINER_ID"
    
    # Copy files
    sudo cp -a "$TEMP_DIR"/* "$MOUNT_DIR"/
    
    # Create essential structure
    sudo mkdir -p "$MOUNT_DIR"/{boot,dev,proc,sys,run,tmp,var,usr,bin,sbin,etc,root,home}
    
    # Create basic config
    echo "/dev/sda1 / ext4 defaults 0 1" | sudo tee "$MOUNT_DIR/etc/fstab"
    echo "debian-bootc" | sudo tee "$MOUNT_DIR/etc/hostname"
    echo -e "127.0.0.1 localhost\n127.0.1.1 debian-bootc" | sudo tee "$MOUNT_DIR/etc/hosts"
    echo -e "root:x:0:0:root:/root:/bin/bash\n" | sudo tee "$MOUNT_DIR/etc/passwd"
    echo -e "root:x:0:\n" | sudo tee "$MOUNT_DIR/etc/group"
    
    # Install GRUB if available
    if command -v grub-install >/dev/null 2>&1; then
        echo "Installing GRUB..."
        sudo grub-install --boot-directory="$MOUNT_DIR/boot" --force "$LOOP_DEV" 2>/dev/null || echo "GRUB install failed, continuing..."
    fi
    
    # Create GRUB config
    sudo mkdir -p "$MOUNT_DIR/boot/grub"
    sudo tee "$MOUNT_DIR/boot/grub/grub.cfg" > /dev/null << 'EOF'
set timeout=5
set default=0

menuentry "Debian Bootc" {
    set root=(hd0,1)
    linux /boot/vmlinuz root=/dev/sda1 ro quiet
    initrd /boot/initrd.img
}
EOF
    
    # Create minimal kernel and initrd
    sudo touch "$MOUNT_DIR/boot/vmlinuz"
    sudo touch "$MOUNT_DIR/boot/initrd.img"
    
    # Unmount
    sudo umount "$MOUNT_DIR"
    sudo losetup -d "$LOOP_DEV"
    rmdir "$MOUNT_DIR"
    rm -rf "$TEMP_DIR"
    
    # Convert to qcow2
    qemu-img convert -f raw -O qcow2 temp-final.raw debian-bootc-final.qcow2
    rm -f temp-final.raw
fi

echo "Created debian-bootc-final.qcow2"
qemu-img info debian-bootc-final.qcow2

echo ""
echo "✅ Bootc container successfully converted to qcow2!"
echo "🚀 To boot: qemu-system-x86_64 -m 2G -drive file=debian-bootc-final.qcow2,format=qcow2 -netdev user,id=net0 -device e1000,netdev=net0 -nographic"



