#!/bin/bash
# Script to install custom bootc and ostree packages

set -e

echo "Installing custom bootc and ostree packages..."

# Paths to the custom package builds
BOOTC_BUILD_DIR="/home/rob/Documents/Projects/bootc-deb/builds"
OSTREE_BUILD_DIR="/home/rob/Documents/Projects/libostree-dev/builds"

# Check if build directories exist
if [ ! -d "$BOOTC_BUILD_DIR" ]; then
    echo "Error: bootc build directory not found at $BOOTC_BUILD_DIR"
    exit 1
fi

if [ ! -d "$OSTREE_BUILD_DIR" ]; then
    echo "Error: ostree build directory not found at $OSTREE_BUILD_DIR"
    exit 1
fi

# Function to install packages with error handling
install_packages() {
    local pkg_dir="$1"
    local pattern="$2"
    
    echo "Installing packages from $pkg_dir matching $pattern..."
    cd "$pkg_dir"
    
    # Find all matching packages
    local packages=($(ls $pattern 2>/dev/null || true))
    
    if [ ${#packages[@]} -eq 0 ]; then
        echo "Warning: No packages found matching $pattern in $pkg_dir"
        return 1
    fi
    
    echo "Found packages: ${packages[*]}"
    
    # Install packages
    sudo dpkg -i "${packages[@]}" || true
    sudo apt-get install -f -y
    
    echo "Packages installed successfully!"
}

# Install ostree packages first (bootc depends on them)
echo "Installing ostree packages..."
install_packages "$OSTREE_BUILD_DIR" "libostree-1-1_*.deb libostree-dev_*.deb ostree_*.deb ostree-boot_*.deb"

# Install bootc packages
echo "Installing bootc packages..."
install_packages "$BOOTC_BUILD_DIR" "bootc_*.deb"

# Verify installation
echo "Verifying installation..."
if command -v bootc &> /dev/null; then
    echo "bootc version: $(bootc --version)"
else
    echo "Warning: bootc not found in PATH"
fi

if command -v ostree &> /dev/null; then
    echo "ostree version: $(ostree --version)"
else
    echo "Warning: ostree not found in PATH"
fi

echo "Custom packages installation completed!" 