#!/bin/bash
# Script to create a simple non-bootable ISO for testing

set -e

echo "Creating simple non-bootable ISO for testing..."

# Create build directory
mkdir -p build

echo "Creating simple ISO using podman..."

# Use podman to create everything inside the container
podman run --rm \
    -v "$(pwd)/build:/output:Z" \
    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 genisoimage
        
        # Create working directory
        mkdir -p /tmp/iso-content
        cd /tmp/iso-content
        
        # 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 simple ISO for testing.' >> README.txt
        
        # List contents to debug
        echo 'Contents of /tmp/iso-content:'
        ls -la /tmp/iso-content/
        
        # Create simple ISO without bootloader inside container
        genisoimage -o /tmp/debian-atomic-installer-simple.iso \
            -r \
            -V 'DEBIAN_ATOMIC' \
            .
        
        # Copy ISO to output directory
        cp /tmp/debian-atomic-installer-simple.iso /output/
        echo 'ISO created and copied to output directory'
    "

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