#!/bin/bash

# Build apt-ostree for Debian Trixie/Forky
# This script ensures compatibility with libapt-pkg7.0

set -e

# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'

print_status() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

print_header() {
    echo ""
    echo -e "${BLUE}================================${NC}"
    echo -e "${BLUE}$1${NC}"
    echo -e "${BLUE}================================${NC}"
}

print_header "Building apt-ostree for Debian Trixie/Forky"

# Check if we're in the right directory
if [ ! -f "Cargo.toml" ]; then
    print_error "Cargo.toml not found. Please run this script from the project root."
    exit 1
fi

# Check if debian directory exists
if [ ! -d "debian" ]; then
    print_error "debian/ directory not found. Please ensure Debian packaging files are present."
    exit 1
fi

# Check system compatibility
print_status "Checking system compatibility..."

# Check if we're on Debian Trixie or newer
if [ -f /etc/os-release ]; then
    source /etc/os-release
    if [[ "$ID" == "debian" && "$VERSION_ID" == "13" ]] || [[ "$ID" == "debian" && "$VERSION_ID" == "14" ]]; then
        print_success "Detected Debian $VERSION_ID (Trixie/Forky)"
    else
        print_error "This script is designed for Debian Trixie (13) or Forky (14)"
        print_error "Current system: $ID $VERSION_ID"
        exit 1
    fi
else
    print_error "Cannot determine OS version"
    exit 1
fi

# Check for required dependencies
print_status "Checking build dependencies..."

# Check for apt-pkg
if ! pkg-config --exists apt-pkg; then
    print_error "apt-pkg development files not found"
    print_error "Install with: sudo apt install libapt-pkg-dev"
    exit 1
fi

APT_PKG_VERSION=$(pkg-config --modversion apt-pkg)
print_status "Found libapt-pkg version: $APT_PKG_VERSION"

# Check if it's version 7.0 or newer
if [[ "$APT_PKG_VERSION" < "3.0.0" ]]; then
    print_error "libapt-pkg version $APT_PKG_VERSION is too old"
    print_error "Need version 3.0.0 or newer for Debian Trixie/Forky"
    exit 1
fi

print_success "System compatibility verified"

# Clean previous builds
print_status "Cleaning previous builds..."
rm -rf target/
rm -f ../apt-ostree_*.deb
rm -f ../apt-ostree-dbgsym_*.deb

# Update Cargo.lock if needed
print_status "Updating Cargo.lock..."
cargo update

# Test build first
print_status "Testing Rust build..."
cargo build --release

if [ $? -eq 0 ]; then
    print_success "Rust build successful"
else
    print_error "Rust build failed"
    exit 1
fi

# Test development features
print_status "Testing development features..."
cargo build --features development --release

if [ $? -eq 0 ]; then
    print_success "Development features build successful"
else
    print_error "Development features build failed"
    exit 1
fi

# Test development commands
print_status "Testing development commands..."
if cargo run --features development -- testutils --help >/dev/null 2>&1; then
    print_success "Development commands working correctly"
else
    print_error "Development commands failed"
    exit 1
fi

# Build the Debian package
print_status "Building Debian package..."
dpkg-buildpackage -us -uc -b

# Check if build was successful
if [ $? -eq 0 ]; then
    print_success "Package built successfully!"
    
    # List built packages
    print_status "Built packages:"
    ls -la ../apt-ostree_*.deb 2>/dev/null || echo "No apt-ostree .deb files found"
    ls -la ../apt-ostree-dbgsym_*.deb 2>/dev/null || echo "No debug symbol files found"
    
    # Test package installation
    print_status "Testing package installation..."
    if sudo dpkg -i ../apt-ostree_*.deb; then
        print_success "Package installation test successful!"
        
        # Test if apt-ostree works
        if apt-ostree --help >/dev/null 2>&1; then
            print_success "apt-ostree command working correctly!"
        else
            print_error "apt-ostree command failed after installation"
        fi
        
        # Uninstall test package
        sudo dpkg -r apt-ostree
        print_status "Test package uninstalled"
    else
        print_error "Package installation test failed!"
        exit 1
    fi
    
else
    print_error "Package build failed!"
    exit 1
fi

print_success "Build and test completed successfully!"
print_status "Package ready for Debian Trixie/Forky:"
ls -la ../apt-ostree_*.deb
