#!/bin/bash

# Create OSTree Deployment Script v2
# Uses standard ostree tools to create proper OSTree deployment

set -euo pipefail

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

# Check if ostree is available
if ! command -v ostree >/dev/null 2>&1; then
    echo "❌ Error: ostree command not found"
    echo "   Install with: sudo apt install ostree"
    exit 1
fi

echo "✅ OSTree tools available, proceeding..."

# Create working directory
WORK_DIR="/tmp/ostree-deployment"
echo "🔧 Creating working directory: $WORK_DIR"
rm -rf "$WORK_DIR"
mkdir -p "$WORK_DIR"

# Create OSTree repository
REPO_DIR="$WORK_DIR/repo"
echo "📦 Initializing OSTree repository: $REPO_DIR"
ostree init --repo="$REPO_DIR" --mode=bare

# Create deployment structure
DEPLOY_DIR="$WORK_DIR/deploy"
echo "🏗️  Creating deployment structure: $DEPLOY_DIR"
mkdir -p "$DEPLOY_DIR"

# Create the atomic filesystem layout
echo "🔗 Creating atomic filesystem layout..."
cd "$DEPLOY_DIR"

# Create essential directories
mkdir -p usr/bin usr/sbin usr/lib usr/lib64 usr/etc
mkdir -p boot var/home var/log var/cache var/tmp
mkdir -p etc

# Create symlinks for atomic layout
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 "✅ Atomic filesystem layout created"

# Copy essential system files from current system
echo "📋 Copying essential system files..."
if [ -d /usr/bin ]; then
    echo "   Copying /usr/bin..."
    cp -r /usr/bin/* usr/bin/ 2>/dev/null || true
fi

if [ -d /usr/sbin ]; then
    echo "   Copying /usr/sbin..."
    cp -r /usr/sbin/* usr/sbin/ 2>/dev/null || true
fi

if [ -d /usr/lib ]; then
    echo "   Copying /usr/lib..."
    cp -r /usr/lib/* usr/lib/ 2>/dev/null || true
fi

if [ -d /usr/lib64 ]; then
    echo "   Copying /usr/lib64..."
    cp -r /usr/lib64/* usr/lib64/ 2>/dev/null || true
fi

if [ -d /boot ]; then
    echo "   Copying /boot..."
    cp -r /boot/* boot/ 2>/dev/null || true
fi

if [ -d /etc ]; then
    echo "   Copying /etc..."
    cp -r /etc/* etc/ 2>/dev/null || true
fi

echo "✅ System files copied"

# Create OSTree commit
echo "💾 Creating OSTree commit..."
ostree commit \
    --repo="$REPO_DIR" \
    --branch=simple-cli/main \
    --subject="Simple-CLI OSTree deployment v2" \
    --body="OSTree deployment with atomic filesystem layout and boot optimizations" \
    "$DEPLOY_DIR"

echo "✅ OSTree commit created"

# Show commit info
echo ""
echo "📊 OSTree commit information:"
ostree --repo="$REPO_DIR" log simple-cli/main | head -10

# Create deployment marker
echo ""
echo "📝 Creating deployment marker..."
mkdir -p "$DEPLOY_DIR/run"
touch "$DEPLOY_DIR/run/ostree-booted"

# Create basic GRUB configuration
echo "🥾 Creating GRUB configuration..."
mkdir -p "$DEPLOY_DIR/boot/grub"

# Find actual kernel and initrd files
KERNEL_FILE=$(find "$DEPLOY_DIR/boot" -name "vmlinuz-*" | head -1)
INITRD_FILE=$(find "$DEPLOY_DIR/boot" -name "initrd.img-*" | head -1)

if [[ -n "$KERNEL_FILE" && -n "$INITRD_FILE" ]]; then
    KERNEL_NAME=$(basename "$KERNEL_FILE")
    INITRD_NAME=$(basename "$INITRD_FILE")
    
    cat > "$DEPLOY_DIR/boot/grub/grub.cfg" << EOF
# GRUB configuration for Simple-CLI OSTree deployment
set timeout=1
set default=0

menuentry "Simple CLI OSTree" {
    set root=(hd0,msdos1)
    linux /boot/$KERNEL_NAME root=/dev/sda1 rw console=ttyS0 quiet splash fastboot
    initrd /boot/$INITRD_NAME
}

menuentry "Simple CLI OSTree (Recovery)" {
    set root=(hd0,msdos1)
    linux /boot/$KERNEL_NAME root=/dev/sda1 rw single console=ttyS0
    initrd /boot/$INITRD_NAME
}
EOF
    echo "✅ GRUB configuration created with kernel: $KERNEL_NAME, initrd: $INITRD_NAME"
else
    echo "⚠️  Warning: Kernel or initrd not found, GRUB config incomplete"
fi

echo ""
echo "🎯 OSTree deployment created successfully!"
echo "📁 Repository: $REPO_DIR"
echo "📁 Deployment: $DEPLOY_DIR"
echo ""
echo "🚀 Next steps:"
echo "   1. Copy deployment to target location"
echo "   2. Use bootc-image-builder to create bootable image"
echo "   3. Test boot performance in QEMU"
echo ""
echo "📋 Files created:"
ls -la "$DEPLOY_DIR" | head -20
