#!/bin/bash

# Create Minimal OSTree Deployment Script
# Creates essential OSTree structure without copying large system files

set -euo pipefail

echo "🌱 Creating Minimal OSTree Deployment 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..."

# Use a different working directory to avoid /tmp space issues
WORK_DIR="$HOME/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 minimal deployment structure
DEPLOY_DIR="$WORK_DIR/deploy"
echo "🏗️  Creating minimal deployment structure: $DEPLOY_DIR"
mkdir -p "$DEPLOY_DIR"

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

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

# 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"

# Create minimal system files instead of copying everything
echo "📋 Creating minimal system files..."

# Create a simple init script
cat > usr/bin/init << 'EOF'
#!/bin/bash
echo "Simple-CLI OSTree System Starting..."
echo "Boot performance optimizations active"
echo "System ready for development"
exec /bin/bash
EOF
chmod +x usr/bin/init

# Create minimal /etc structure
mkdir -p etc/systemd/system
cat > etc/systemd/system.conf << 'EOF'
[Manager]
DefaultTimeoutStartSec=15s
DefaultTimeoutStopSec=15s
DefaultRestartSec=100ms
EOF

# Create minimal /boot structure
mkdir -p boot/grub
cat > boot/grub/grub.cfg << 'EOF'
# Minimal GRUB configuration for Simple-CLI OSTree
set timeout=1
set default=0

menuentry "Simple CLI OSTree" {
    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
}
EOF

# Create deployment marker
touch run/ostree-booted

echo "✅ Minimal system files created"

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

echo "✅ OSTree commit created"

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

echo ""
echo "🎯 Minimal OSTree deployment created successfully!"
echo "📁 Repository: $REPO_DIR"
echo "📁 Deployment: $DEPLOY_DIR"
echo ""
echo "📋 Files created:"
find "$DEPLOY_DIR" -type f | head -20

echo ""
echo "🚀 Next steps:"
echo "   1. This creates the basic OSTree structure"
echo "   2. For full system, need to integrate with simple-cli container"
echo "   3. Use bootc-image-builder to create bootable image"
echo "   4. Test boot performance in QEMU"
