#!/bin/bash

echo "Creating truly bootable qcow2 with proper GRUB..."

# Clean up
rm -f debian-bootc-truly-bootable.qcow2

# Create qcow2
qemu-img create -f qcow2 debian-bootc-truly-bootable.qcow2 2G

# 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 raw image
dd if=/dev/zero of=temp-bootable.raw bs=1M count=200 2>/dev/null

# Set up loopback
sudo losetup -f temp-bootable.raw
LOOP_DEV=$(sudo losetup -j temp-bootable.raw | cut -d: -f1)

# Create partition table and 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 the partition
sudo mkfs.ext4 -F "${LOOP_DEV}p1"

# Mount and copy files
MOUNT_DIR=$(mktemp -d)
sudo mount "${LOOP_DEV}p1" "$MOUNT_DIR"
sudo cp -a "$TEMP_DIR"/* "$MOUNT_DIR"/

# Create essential directories
sudo mkdir -p "$MOUNT_DIR"/{boot,dev,proc,sys,run,tmp,var,usr,bin,sbin,etc,root,home}

# Create basic system 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 with correct target
echo "Installing GRUB bootloader..."
if command -v grub-install >/dev/null 2>&1; then
    # Try different GRUB targets
    if [ -d "/usr/lib/grub/i386-pc" ]; then
        sudo grub-install --target=i386-pc --boot-directory="$MOUNT_DIR/boot" --force "$LOOP_DEV"
    elif [ -d "/usr/lib/grub/x86_64-efi" ]; then
        sudo grub-install --target=x86_64-efi --boot-directory="$MOUNT_DIR/boot" --force "$LOOP_DEV"
    else
        # Try without specifying target
        sudo grub-install --boot-directory="$MOUNT_DIR/boot" --force "$LOOP_DEV"
    fi
else
    echo "GRUB not available, creating basic boot sector..."
    # Create a basic boot sector manually
    sudo dd if=/usr/lib/syslinux/mbr/mbr.bin of="$LOOP_DEV" bs=440 count=1 2>/dev/null || true
fi

# 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" {
    set root=(hd0,1)
    linux /boot/vmlinuz root=/dev/sda1 ro quiet
    initrd /boot/initrd.img
}
EOF

# Create a basic kernel and initrd if they don't exist
if [ ! -f "$MOUNT_DIR/boot/vmlinuz" ]; then
    echo "Creating placeholder kernel..."
    sudo touch "$MOUNT_DIR/boot/vmlinuz"
fi

if [ ! -f "$MOUNT_DIR/boot/initrd.img" ]; then
    echo "Creating placeholder initrd..."
    sudo touch "$MOUNT_DIR/boot/initrd.img"
fi

# Unmount and convert
sudo umount "$MOUNT_DIR"
sudo losetup -d "$LOOP_DEV"
rmdir "$MOUNT_DIR"

# Convert to qcow2
qemu-img convert -f raw -O qcow2 temp-bootable.raw debian-bootc-truly-bootable.qcow2

# Cleanup
rm -f temp-bootable.raw
rm -rf "$TEMP_DIR"

echo "Created debian-bootc-truly-bootable.qcow2"
qemu-img info debian-bootc-truly-bootable.qcow2

echo ""
echo "Testing bootability..."
echo "To boot: qemu-system-x86_64 -m 2G -drive file=debian-bootc-truly-bootable.qcow2,format=qcow2 -netdev user,id=net0 -device e1000,netdev=net0 -nographic"



