#!/bin/bash

# Build a complete Debian bootc qcow2 image
# This script creates a complete bootc system and installs it to a qcow2 disk

set -e

# Configuration
IMAGE_NAME="debian-bootc-complete"
QCOW2_FILE="debian-bootc-complete.qcow2"
SIZE="10G"
BASE_IMAGE="git.raines.xyz/particle-os/debian-bootc:latest-with-fixes"

echo "🏗️  Building Debian bootc qcow2 image"
echo "======================================"
echo "Base image: $BASE_IMAGE"
echo "Output file: $QCOW2_FILE"
echo "Size: $SIZE"
echo ""

# Create qcow2 disk image
echo "📦 Creating qcow2 disk image..."
qemu-img create -f qcow2 "$QCOW2_FILE" "$SIZE"

# Create a temporary directory for the installation
TEMP_DIR=$(mktemp -d)
echo "📁 Using temporary directory: $TEMP_DIR"

# Create a rootfs directory
ROOTFS_DIR="$TEMP_DIR/rootfs"
mkdir -p "$ROOTFS_DIR"

echo "🔧 Setting up rootfs structure..."
# Create basic directory structure
mkdir -p "$ROOTFS_DIR"/{bin,sbin,usr/bin,usr/sbin,etc,var,proc,sys,dev,run,tmp,root,home}
mkdir -p "$ROOTFS_DIR"/usr/{lib,lib64,share,include}
mkdir -p "$ROOTFS_DIR"/var/{lib,log,cache,spool}
mkdir -p "$ROOTFS_DIR"/etc/{systemd,apt,ostree}

echo "📋 Creating basic system files..."

# Create a basic /etc/os-release
cat > "$ROOTFS_DIR/etc/os-release" << 'EOF'
NAME="Debian GNU/Linux"
VERSION="14 (Trixie)"
ID=debian
ID_LIKE=debian
PRETTY_NAME="Debian GNU/Linux 14 (Trixie)"
VERSION_ID="14"
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
EOF

# Create a basic /etc/hostname
echo "debian-bootc" > "$ROOTFS_DIR/etc/hostname"

# Create a basic /etc/hosts
cat > "$ROOTFS_DIR/etc/hosts" << 'EOF'
127.0.0.1	localhost
127.0.1.1	debian-bootc
::1		localhost ip6-localhost ip6-loopback
ff02::1		ip6-allnodes
ff02::2		ip6-allrouters
EOF

# Create a basic /etc/passwd
cat > "$ROOTFS_DIR/etc/passwd" << 'EOF'
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
EOF

# Create a basic /etc/group
cat > "$ROOTFS_DIR/etc/group" << 'EOF'
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog
tty:x:5:
disk:x:6:
lp:x:7:
mail:x:8:
news:x:9:
uucp:x:10:
man:x:12:
proxy:x:13:
kmem:x:15:
dialout:x:20:
fax:x:21:
voice:x:22:
cdrom:x:24:
floppy:x:25:
tape:x:26:
sudo:x:27:
audio:x:29:
dip:x:30:
www-data:x:33:
backup:x:34:
operator:x:37:
list:x:38:
irc:x:39:
src:x:40:
gnats:x:41:
shadow:x:42:
utmp:x:43:
video:x:44:
sasl:x:45:
plugdev:x:46:
staff:x:50:
games:x:60:
users:x:100:
nogroup:x:65534:
EOF

echo "🔧 Setting up loopback device..."
# Set up loopback device
sudo losetup -f "$QCOW2_FILE"
LOOP_DEVICE=$(sudo losetup -j "$QCOW2_FILE" | cut -d: -f1)

echo "📀 Partitioning disk..."
# Partition the disk
sudo parted "$LOOP_DEVICE" --script mklabel gpt
sudo parted "$LOOP_DEVICE" --script mkpart primary 1MiB 100%

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

echo "🔗 Mounting filesystem..."
# Mount the partition
sudo mkdir -p "$TEMP_DIR/mount"
sudo mount "${LOOP_DEVICE}p1" "$TEMP_DIR/mount"

echo "📋 Copying rootfs to disk..."
# Copy the rootfs to the disk
sudo cp -r "$ROOTFS_DIR"/* "$TEMP_DIR/mount/"

echo "🔧 Setting up bootloader..."
# Install GRUB
sudo mkdir -p "$TEMP_DIR/mount/boot/grub"
sudo grub-install --target=x86_64-efi --efi-directory="$TEMP_DIR/mount" --boot-directory="$TEMP_DIR/mount/boot" "$LOOP_DEVICE"

# Create a basic GRUB configuration
sudo tee "$TEMP_DIR/mount/boot/grub/grub.cfg" > /dev/null << 'EOF'
set timeout=5
set default=0

menuentry "Debian Bootc" {
    linux /boot/vmlinuz root=/dev/sda1 ro quiet
    initrd /boot/initrd.img
}
EOF

echo "🧹 Cleaning up..."
# Unmount and cleanup
sudo umount "$TEMP_DIR/mount"
sudo losetup -d "$LOOP_DEVICE"
sudo rm -rf "$TEMP_DIR"

echo "✅ Debian bootc qcow2 image created successfully: $QCOW2_FILE"
echo "🚀 You can now boot this image with:"
echo "   qemu-system-x86_64 -m 2G -drive file=$QCOW2_FILE,format=qcow2 -netdev user,id=net0 -device e1000,netdev=net0 -nographic"
