#!/bin/bash
set -e

echo "=== Building examples against installed libopenshot ==="

# Apply quilt patches if they exist
if [ -d "debian/patches" ]; then
    echo "Applying quilt patches..."
    export QUILT_PATCHES=debian/patches
    quilt push -a || true
fi

# Copy examples to test temp directory
cp -r examples "$AUTOPKGTEST_TMP/"

# Create build directory
BUILD_DIR=$(mktemp -d)
cd "$BUILD_DIR"

echo "Build directory: $BUILD_DIR"

# Copy examples source from AUTOPKGTEST_TMP
cp -r "$AUTOPKGTEST_TMP/examples"/* .

# Configure with CMake
echo "Running cmake..."
cmake .

# Build examples
echo "Building examples..."
make -j$(nproc)

echo "=== Build successful ==="

# List built executables
echo "Built executables:"
ls -lh openshot-example openshot-html-example openshot-player 2>/dev/null || true

# Cleanup
cd /
rm -rf "$BUILD_DIR"

echo "=== Test passed ==="
