#!/bin/bash
# Script to create a minimal bootable ISO with GRUB (simplified approach)

set -e

echo "Creating minimal bootable ISO (simplified approach)..."

# Create build directory
mkdir -p build

# Create a working directory for ISO contents
mkdir -p build/iso-work
cd build/iso-work

echo "Creating ISO structure..."

# Create basic ISO structure
mkdir -p boot/grub
mkdir -p isolinux

# Create a simple kernel placeholder
echo 'This is a placeholder kernel' > boot/vmlinuz
echo 'This is a placeholder initrd' > boot/initrd.img

# Create GRUB configuration
cat > boot/grub/grub.cfg << 'EOF'
set timeout=5
set default=0

menuentry 'Debian Atomic Desktop Installer' {
    linux /boot/vmlinuz root=live:CDLABEL=DEBIAN_ATOMIC quiet
    initrd /boot/initrd.img
}

menuentry 'Debian Atomic Desktop Installer (Safe Mode)' {
    linux /boot/vmlinuz root=live:CDLABEL=DEBIAN_ATOMIC nomodeset
    initrd /boot/initrd.img
}
EOF

# Create isolinux configuration
cat > isolinux/isolinux.cfg << 'EOF'
DEFAULT install
TIMEOUT 50
PROMPT 1

LABEL install
    KERNEL /boot/vmlinuz
    APPEND root=live:CDLABEL=DEBIAN_ATOMIC quiet initrd=/boot/initrd.img
    
LABEL install-safe
    KERNEL /boot/vmlinuz
    APPEND root=live:CDLABEL=DEBIAN_ATOMIC nomodeset initrd=/boot/initrd.img
EOF

# Create a simple README
echo 'Debian Atomic Desktop Installer ISO' > README.txt
echo 'This is a minimal bootable ISO for testing.' >> README.txt

echo "Creating bootable ISO using podman..."

# Use podman only for the final ISO creation
podman run --rm \
    -v "$(pwd):/work" \
    -w /work \
    debian:trixie \
    bash -c "
        # Configure apt-cacher-ng proxy if available
        if [ -n \"$APT_CACHER_NG_PROXY\" ]; then
            echo \"Acquire::http::Proxy \\\"$APT_CACHER_NG_PROXY\\\";\" > /etc/apt/apt.conf.d/99proxy
            echo \"Acquire::https::Proxy \\\"$APT_CACHER_NG_PROXY\\\";\" >> /etc/apt/apt.conf.d/99proxy
        fi
        
        # Update and install required packages
        apt-get update
        apt-get install -y isolinux xorriso
        
        # Copy isolinux files
        cp /usr/lib/ISOLINUX/isolinux.bin isolinux/ 2>/dev/null || echo 'isolinux.bin not found'
        cp /usr/lib/syslinux/modules/bios/ldlinux.c32 isolinux/ 2>/dev/null || echo 'ldlinux.c32 not found'
        
        # Check if isolinux files exist
        if [ ! -f isolinux/isolinux.bin ]; then
            echo 'Creating minimal isolinux.bin placeholder...'
            echo 'ISOLINUX' > isolinux/isolinux.bin
        fi
        
        if [ ! -f isolinux/ldlinux.c32 ]; then
            echo 'Creating minimal ldlinux.c32 placeholder...'
            echo 'LDLINUX' > isolinux/ldlinux.c32
        fi
        
        # Create bootable ISO with simpler approach
        xorrisofs -o ../debian-atomic-installer-bootable.iso \
            -b isolinux/isolinux.bin \
            -c isolinux/boot.cat \
            -boot-info-table \
            -no-emul-boot \
            -boot-load-size 4 \
            -r \
            -V 'DEBIAN_ATOMIC' \
            /work
    "

cd ../..

echo "Bootable ISO created: build/debian-atomic-installer-bootable.iso"
if [ -f "build/debian-atomic-installer-bootable.iso" ]; then
    echo "Size: $(du -h build/debian-atomic-installer-bootable.iso | cut -f1)"
    echo "✅ Bootable ISO creation completed!"
else
    echo "❌ ISO creation failed!"
    exit 1
fi 