#!/bin/bash

# Use bootc offline method with qcow2
# Based on https://bootc-dev.github.io/bootc/registries-and-offline.html

set -e

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

echo "🏗️  Using bootc offline method with qcow2"
echo "========================================"
echo "Container: $CONTAINER_IMAGE"
echo "Output: $QCOW2_FILE"
echo "OCI Dir: $OCI_DIR"
echo "Size: $SIZE"
echo ""

# Clean up
rm -f "$QCOW2_FILE"
rm -rf "$OCI_DIR"

echo "💾 Creating qcow2 disk image..."
qemu-img create -f qcow2 "$QCOW2_FILE" "$SIZE"

echo "📦 Copying container to OCI directory (offline method)..."
# Use skopeo to copy the container to an OCI directory as per the documentation
skopeo copy "docker://$CONTAINER_IMAGE" "oci:/tmp/$OCI_DIR"

echo "🔧 Setting up qcow2 as block device..."
# Set up the qcow2 as a loopback device
sudo losetup -f "$QCOW2_FILE"
LOOP_DEV=$(sudo losetup -j "$QCOW2_FILE" | cut -d: -f1)
echo "Loop device: $LOOP_DEV"

echo "📋 Creating partition table..."
# Create a partition table on the qcow2
echo -e "n\np\n1\n\n\nt\n83\na\nw" | sudo fdisk "$LOOP_DEV" >/dev/null 2>&1
sudo partprobe "$LOOP_DEV"

echo "📁 Formatting filesystem..."
# Format the partition
sudo mkfs.ext4 -F "${LOOP_DEV}p1"

echo "🔗 Mounting and copying container contents..."
# Mount the partition
MOUNT_DIR=$(mktemp -d)
sudo mount "${LOOP_DEV}p1" "$MOUNT_DIR"

# Extract the OCI container contents
TEMP_EXTRACT=$(mktemp -d)
tar -xf "/tmp/$OCI_DIR/blobs/sha256/$(ls /tmp/$OCI_DIR/blobs/sha256/ | grep -v manifest | head -1)" -C "$TEMP_EXTRACT"
sudo cp -a "$TEMP_EXTRACT"/* "$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"

# Install GRUB if available
if command -v grub-install >/dev/null 2>&1; then
    echo "🚀 Installing GRUB bootloader..."
    sudo grub-install --boot-directory="$MOUNT_DIR/boot" --force "$LOOP_DEV" 2>/dev/null || echo "GRUB install failed, continuing..."
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 (Offline)" {
    set root=(hd0,1)
    linux /boot/vmlinuz root=/dev/sda1 ro quiet
    initrd /boot/initrd.img
}
EOF

# Create placeholder kernel and initrd
sudo touch "$MOUNT_DIR/boot/vmlinuz"
sudo touch "$MOUNT_DIR/boot/initrd.img"

echo "🧹 Cleaning up..."
# Unmount and cleanup
sudo umount "$MOUNT_DIR"
sudo losetup -d "$LOOP_DEV"
rmdir "$MOUNT_DIR"
rm -rf "$TEMP_EXTRACT"
rm -rf "/tmp/$OCI_DIR"

echo "✅ Bootc offline installation completed!"
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"



