#!/bin/bash

# Create a bootable bootc qcow2 image with GRUB bootloader
# This creates a properly bootable qcow2 from the bootc container

set -e

CONTAINER_IMAGE="git.raines.xyz/particle-os/debian-bootc:latest-with-fixes"
QCOW2_FILE="debian-bootc-bootable.qcow2"
SIZE="10G"

echo "🏗️  Creating bootable bootc qcow2 with GRUB"
echo "=========================================="
echo "Container: $CONTAINER_IMAGE"
echo "Output: $QCOW2_FILE"
echo "Size: $SIZE"
echo ""

# Clean up
rm -f "$QCOW2_FILE"

echo "📦 Exporting container contents..."
TEMP_DIR=$(mktemp -d)
CONTAINER_ID=$(podman create "$CONTAINER_IMAGE")
podman export "$CONTAINER_ID" | tar -xC "$TEMP_DIR"
podman rm "$CONTAINER_ID"

echo "💾 Creating qcow2 image..."
# Create a 10GB qcow2 image
qemu-img create -f qcow2 "$QCOW2_FILE" "$SIZE"

echo "🔧 Setting up bootable filesystem..."
# Use guestfish to create a bootable system
if command -v guestfish >/dev/null 2>&1; then
    echo "📁 Using guestfish to create bootable system..."
    guestfish -a "$QCOW2_FILE" << EOF
run
part-disk /dev/sda mbr
part-set-bootable /dev/sda 1 true
mkfs ext4 /dev/sda1
mount /dev/sda1 /
copy-in $TEMP_DIR/* /
mkdir /boot
mkdir /dev
mkdir /proc
mkdir /sys
mkdir /run
mkdir /tmp
mkdir /var
mkdir /usr
mkdir /bin
mkdir /sbin
mkdir /etc
mkdir /root
mkdir /home
write /etc/fstab "/dev/sda1 / ext4 defaults 0 1\n"
write /etc/hostname "debian-bootc\n"
write /etc/hosts "127.0.0.1 localhost\n127.0.1.1 debian-bootc\n"
write /etc/passwd "root:x:0:0:root:/root:/bin/bash\n"
write /etc/group "root:x:0:\n"
umount /
EOF
else
    echo "📁 Using manual approach..."
    # Manual approach - create raw image first, then convert
    RAW_FILE="temp-bootc.raw"
    dd if=/dev/zero of="$RAW_FILE" bs=1M count=10240 2>/dev/null
    
    # Set up loopback
    sudo losetup -f "$RAW_FILE"
    LOOP_DEV=$(sudo losetup -j "$RAW_FILE" | cut -d: -f1)
    
    # Create partition
    echo -e "n\np\n1\n\n\nt\n83\na\nw" | sudo fdisk "$LOOP_DEV" >/dev/null 2>&1
    sudo partprobe "$LOOP_DEV"
    
    # Format and mount
    sudo mkfs.ext4 -F "${LOOP_DEV}p1"
    MOUNT_DIR=$(mktemp -d)
    sudo mount "${LOOP_DEV}p1" "$MOUNT_DIR"
    
    # Copy files
    sudo cp -a "$TEMP_DIR"/* "$MOUNT_DIR"/
    sudo mkdir -p "$MOUNT_DIR"/{boot,dev,proc,sys,run,tmp,var,usr,bin,sbin,etc,root,home}
    
    # Create basic config files
    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 bootloader
    echo "🚀 Installing GRUB bootloader..."
    sudo grub-install --target=i386-pc --boot-directory="$MOUNT_DIR/boot" --force "$LOOP_DEV"
    
    # Create GRUB configuration
    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 System" {
    linux /boot/vmlinuz root=/dev/sda1 ro quiet
    initrd /boot/initrd.img
}
EOF
    
    # Unmount and cleanup
    sudo umount "$MOUNT_DIR"
    sudo losetup -d "$LOOP_DEV"
    rmdir "$MOUNT_DIR"
    
    # Convert to qcow2
    echo "🔄 Converting to qcow2..."
    qemu-img convert -f raw -O qcow2 "$RAW_FILE" "$QCOW2_FILE"
    rm -f "$RAW_FILE"
fi

echo "🧹 Cleaning up..."
rm -rf "$TEMP_DIR"

echo "✅ Created bootable qcow2: $QCOW2_FILE"
echo "📊 Image info:"
qemu-img info "$QCOW2_FILE"

echo ""
echo "🚀 To boot this image:"
echo "   qemu-system-x86_64 -m 2G -drive file=$QCOW2_FILE,format=qcow2 -netdev user,id=net0 -device e1000,netdev=net0 -nographic"



