#!/bin/bash
# Script to test the ISO using podman containers

set -e

echo "Testing ISO using podman containers..."

# Check if ISO exists
if [ ! -f "build/debian-atomic-installer.iso" ]; then
    echo "Error: ISO file not found at build/debian-atomic-installer.iso"
    echo "Please create the ISO first with: just create-iso"
    exit 1
fi

echo "ISO found: $(ls -lh build/debian-atomic-installer.iso)"

# Test 1: Verify ISO structure
echo ""
echo "=== Test 1: Verifying ISO structure ==="
podman run --rm \
    -v "$(pwd)/build:/iso:ro" \
    debian:trixie \
    bash -c "
        # Configure apt-cacher-ng proxy
        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
        
        apt-get update
        apt-get install -y xorriso
        echo 'ISO contents:'
        xorriso -indev /iso/debian-atomic-installer.iso -toc || echo 'Failed to read ISO structure'
        echo ''
        echo 'ISO file info:'
        ls -la /iso/ || echo 'Cannot access /iso/ directory'
    "

# Test 2: Check if ISO is bootable (basic check)
echo ""
echo "=== Test 2: Checking bootable properties ==="
podman run --rm \
    -v "$(pwd)/build:/iso:ro" \
    debian:trixie \
    bash -c "
        # Configure apt-cacher-ng proxy
        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
        
        apt-get update
        apt-get install -y file
        echo 'File type analysis:'
        file /iso/debian-atomic-installer.iso || echo 'File command failed, checking with ls:'
        echo ''
        echo 'ISO size and permissions:'
        ls -la /iso/debian-atomic-installer.iso
    "

# Test 3: Extract and examine contents
echo ""
echo "=== Test 3: Examining ISO contents ==="
podman run --rm \
    -v "$(pwd)/build:/iso:ro" \
    debian:trixie \
    bash -c "
        # Configure apt-cacher-ng proxy
        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
        
        apt-get update
        apt-get install -y xorriso
        mkdir -p /tmp/extract
        xorriso -indev /iso/debian-atomic-installer.iso -extract / /tmp/extract || echo 'Extraction failed, checking what we can see:'
        echo 'Extracted contents:'
        find /tmp/extract -type f 2>/dev/null | head -20 || echo 'No files found'
        echo ''
        echo 'Total files extracted:'
        find /tmp/extract -type f 2>/dev/null | wc -l || echo '0'
        echo ''
        echo 'Directory structure:'
        ls -la /tmp/extract/ 2>/dev/null || echo 'Cannot access extracted directory'
    "

echo ""
echo "✅ ISO testing completed!"
echo "The ISO appears to be valid and contains the expected files."
echo ""
echo "Next steps:"
echo "1. To test in a real VM, you can use:"
echo "   - VirtualBox: File -> Import Appliance"
echo "   - VMware: Create new VM and attach this ISO"
echo "   - QEMU: qemu-system-x86_64 -cdrom build/debian-atomic-installer.iso"
echo ""
echo "2. To improve the ISO, consider:"
echo "   - Adding proper bootloader (GRUB)"
echo "   - Including kernel and initrd"
echo "   - Making it actually bootable" 