#!/bin/bash
# Manual package building without makepkg
set -e

PACKAGE=$1
if [ -z "$PACKAGE" ]; then
    echo "Usage: $0 <package-name>"
    exit 1
fi

echo "Manually building package: $PACKAGE"

# Find PKGBUILD
PKGBUILD_PATH=""
for repo in packages community; do
    if [ -d "${ARCH_ROOT}/sources/${repo}/${PACKAGE}/trunk" ]; then
        PKGBUILD_PATH="${ARCH_ROOT}/sources/${repo}/${PACKAGE}/trunk"
        break
    fi
done

if [ -z "$PKGBUILD_PATH" ]; then
    echo "Package $PACKAGE not found"
    exit 1
fi

# Set up build environment
BUILD_DIR="${BUILDDIR}/${PACKAGE}"
INSTALL_DIR="/opt/arch-s390x/sysroot"
mkdir -p "$BUILD_DIR" "$INSTALL_DIR"

# Copy PKGBUILD
rm -rf "$BUILD_DIR"
cp -r "$PKGBUILD_PATH" "$BUILD_DIR"
cd "$BUILD_DIR"

echo "Working in: $BUILD_DIR"

# Source the PKGBUILD to get variables
source PKGBUILD

echo "Package: $pkgname"
echo "Version: $pkgver"
echo "Description: $pkgdesc"

# Download sources
mkdir -p sources
cd sources

# Handle source downloads and git repos
for src_url in "${source[@]}"; do
    # Skip signature files for now
    if [[ "$src_url" == *.sign ]] || [[ "$src_url" == *"SKIP"* ]]; then
        continue
    fi
    
    # Handle git URLs
    if [[ "$src_url" == git+* ]]; then
        # Parse git URL and commit
        git_url=$(echo "$src_url" | sed 's/git+//' | sed 's/#.*//')
        repo_name=$(basename "$git_url" .git)
        
        if [ ! -d "$repo_name" ]; then
            echo "Cloning git repository: $git_url"
            git clone "$git_url" "$repo_name"
        fi
        
        # Handle commit hash if specified
        if [[ "$src_url" == *"#commit="* ]]; then
            commit_hash=$(echo "$src_url" | sed 's/.*#commit=//')
            echo "Checking out commit: $commit_hash"
            cd "$repo_name"
            git checkout "$commit_hash"
            cd ..
        fi
        continue
    fi
    
    # Check if it's a local file first
    filename=$(basename "$src_url")
    if [ -f "../$filename" ]; then
        echo "Copying local file: $filename"
        cp "../$filename" .
        continue
    fi
    
    # Handle URLs (must contain :// )
    if [[ "$src_url" == *"://"* ]]; then
        if [ ! -f "$filename" ]; then
            echo "Downloading: $src_url"
            curl -L -o "$filename" "$src_url"
        fi
        
        # Extract if it's an archive
        if [[ "$filename" == *.tar.* ]]; then
            echo "Extracting: $filename"
            tar -xf "$filename"
        fi
    else
        echo "Warning: Could not handle source: $src_url"
    fi
done

cd ..

# Export pkgdir for build
export pkgdir="$INSTALL_DIR"
mkdir -p "$pkgdir"

# Set srcdir for PKGBUILD compatibility
export srcdir="$BUILD_DIR/sources"

# Go back to build dir before calling functions
cd "$BUILD_DIR"

# Call prepare function if it exists (for complex packages like glibc)
if declare -f prepare > /dev/null; then
    echo "Running prepare() function..."
    cd sources
    prepare
    cd "$BUILD_DIR"
fi

# Call build function if it exists
if declare -f build > /dev/null; then
    echo "Running build() function..."
    cd sources
    build
    cd "$BUILD_DIR"
fi

# Call package function if it exists  
if declare -f package > /dev/null; then
    echo "Running package() function..."
    cd sources
    package
    cd "$BUILD_DIR"
fi

echo "Build complete for $PACKAGE"
echo "Installed to: $INSTALL_DIR"