#!/bin/bash

# Download DEB Packages Script for Offline Installation
# This script downloads all system .deb packages required for offline installation

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SYSTEM_DIR="$SCRIPT_DIR/repo/system-packages"
DEB_DIR="$SYSTEM_DIR/debs"

echo "=========================================="
echo "Downloading DEB Packages for Offline Install"
echo "=========================================="
echo ""

# Create debs directory
mkdir -p "$DEB_DIR"

# Check if running on Debian/Ubuntu/Raspberry Pi OS
if ! command -v apt-get &> /dev/null; then
    echo "Error: This script requires apt-get (Debian/Ubuntu/Raspberry Pi OS)"
    exit 1
fi

echo "Reading package list from: $SYSTEM_DIR/apt-packages.txt"
echo ""

# Update package cache
echo "Updating package cache..."
sudo apt update

# Read packages from file
PACKAGES=$(grep -v '^#' "$SYSTEM_DIR/apt-packages.txt" | grep -v '^$' | tr '\n' ' ')

echo "Packages to download:"
echo "$PACKAGES"
echo ""

# Download packages and dependencies
echo "Downloading packages with dependencies..."
cd "$DEB_DIR"

# Use apt-get download to get .deb files
for pkg in $PACKAGES; do
    echo "Downloading: $pkg"
    apt-get download "$pkg" 2>/dev/null || echo "  Warning: Could not download $pkg"
done

# Download dependencies
echo ""
echo "Downloading dependencies..."
sudo apt-get install --download-only --reinstall -y $PACKAGES

# Copy downloaded debs from apt cache
echo ""
echo "Copying packages from apt cache..."
sudo cp /var/cache/apt/archives/*.deb "$DEB_DIR/" 2>/dev/null || true

# Remove duplicate packages
echo ""
echo "Removing duplicates..."
cd "$DEB_DIR"
for file in *.deb; do
    [ -f "$file" ] || continue
    basename="${file%%_*}"
    count=$(ls -1 "${basename}"_*.deb 2>/dev/null | wc -l)
    if [ "$count" -gt 1 ]; then
        # Keep only the latest version
        ls -t "${basename}"_*.deb | tail -n +2 | xargs rm -f
    fi
done

# Create installation order file
echo ""
echo "Creating installation order..."
cat > "$DEB_DIR/install-order.txt" << 'EOF'
# Install packages in this order to resolve dependencies

# 1. Base tools and libraries
python3-pip_*.deb
python3-setuptools_*.deb
python3-dev_*.deb
zlib1g-dev_*.deb

# 2. SDL2 libraries
libsdl2-dev_*.deb
libsdl2-image-dev_*.deb
libsdl2-mixer-dev_*.deb
libsdl2-ttf-dev_*.deb

# 3. Multimedia libraries
libportmidi-dev_*.deb
libswscale-dev_*.deb
libavformat-dev_*.deb
libavcodec-dev_*.deb
libavcodec-extra_*.deb

# 4. FFmpeg and codecs
ffmpeg_*.deb
libx264-dev_*.deb

# 5. GStreamer
gstreamer1.0-plugins-base_*.deb
gstreamer1.0-plugins-good_*.deb
gstreamer1.0-plugins-bad_*.deb
gstreamer1.0-alsa_*.deb

# 6. Network tools
wget_*.deb
curl_*.deb
EOF

# Summary
echo ""
echo "=========================================="
echo "DEB Download Complete!"
echo "=========================================="
echo ""
echo "Downloaded .deb files: $(ls -1 *.deb 2>/dev/null | wc -l)"
echo "Location: $DEB_DIR"
echo ""
echo "To install offline, copy the repo folder and run:"
echo "  bash install.sh --offline"
echo ""
