#!/bin/bash

# Create OSTree Deployment Script
# Converts our simple-cli container into a proper OSTree deployment

set -euo pipefail

echo "🌱 Creating OSTree Deployment for Simple-CLI"
echo "============================================="

# Check if we're running in the container
echo "✅ Running in container, proceeding with OSTree deployment..."

# Create OSTree repository
echo "🔧 Initializing OSTree repository..."
mkdir -p /ostree/repo
ostree init --repo=/ostree/repo --mode=bare

# Create the filesystem restructuring
echo "🔄 Restructuring filesystem for OSTree..."
mkdir -p /ostree/deploy/simple-cli/var

# Create symlinks for atomic filesystem layout
echo "🔗 Creating atomic filesystem symlinks..."
ln -sf /usr/bin /bin
ln -sf /usr/sbin /sbin
ln -sf /usr/lib /lib
ln -sf /usr/lib64 /lib64

# Move user data directories to /var
echo "📁 Setting up /var structure..."
mkdir -p /ostree/deploy/simple-cli/var/home
mkdir -p /ostree/deploy/simple-cli/var/log
mkdir -p /ostree/deploy/simple-cli/var/cache
mkdir -p /ostree/deploy/simple-cli/var/tmp

# Create /home symlink to /var/home
ln -sf /ostree/deploy/simple-cli/var/home /home

# Create a minimal OSTree deployment structure
echo "💾 Creating minimal OSTree deployment structure..."

# Create essential directories
mkdir -p /ostree/deploy/simple-cli/usr
mkdir -p /ostree/deploy/simple-cli/boot
mkdir -p /ostree/deploy/simple-cli/etc

# Copy essential system files
cp -r /usr/* /ostree/deploy/simple-cli/usr/
cp -r /boot/* /ostree/deploy/simple-cli/boot/
cp -r /etc/* /ostree/deploy/simple-cli/etc/

# Create the atomic filesystem symlinks in the deployment
cd /ostree/deploy/simple-cli
ln -sf usr/bin bin
ln -sf usr/sbin sbin
ln -sf usr/lib lib
ln -sf usr/lib64 lib64
ln -sf var/home home

echo "✅ Minimal OSTree deployment structure created"

# Create deployment
echo "🚀 Creating OSTree deployment..."
ostree admin deploy \
    --sysroot=/ostree/deploy \
    --os=simple-cli \
    simple-cli/main

# Set up boot configuration
echo "🥾 Setting up boot configuration..."
mkdir -p /ostree/deploy/simple-cli/boot/grub
cat > /ostree/deploy/simple-cli/boot/grub/grub.cfg << 'EOF'
# GRUB configuration for Simple-CLI OSTree deployment
set timeout=1
set default=0

menuentry "Simple CLI" {
    set root=(hd0,msdos1)
    linux /boot/vmlinuz-6.12.41+deb13-amd64 root=/dev/sda1 rw console=ttyS0 quiet splash fastboot
    initrd /boot/initrd.img-6.12.41+deb13-amd64
}

menuentry "Simple CLI (Recovery)" {
    set root=(hd0,msdos1)
    linux /boot/vmlinuz-6.12.41+deb13-amd64 root=/dev/sda1 rw single console=ttyS0
    initrd /boot/initrd.img-6.12.41+deb13-amd64
}
EOF

# Create deployment marker
echo "📝 Creating deployment marker..."
touch /ostree/deploy/simple-cli/run/ostree-booted

echo ""
echo "✅ OSTree deployment created successfully!"
echo "📊 Deployment info:"
ostree --repo=/ostree/repo log simple-cli/main | head -10

echo ""
echo "🎯 Next steps:"
echo "   1. Exit this container"
echo "   2. Rebuild the bootable image with:"
echo "      ./scripts/bootc-image-builder.sh -o /tmp/output simple-cli:latest"
echo "   3. Test boot performance in QEMU"
