#!/bin/bash
set -e

PACKAGES=(
    #"pkg1"
    #"pkg2"
    #"pkg3"
)

SOURCE_TAG="f43-updates"
BUILD_TAG="f43"

for pkg in "${PACKAGES[@]}"; do

    # Get the latest tagged build's NVR from primary Koji.
    echo "Finding latest tagged build in '$SOURCE_TAG'..."
    latest_nvr=$(koji --quiet list-tagged --latest "$SOURCE_TAG" "$pkg" | awk '{print $1}')

    if [[ -z "$latest_nvr" ]]; then
        echo "Could not find any build in '$SOURCE_TAG' for $pkg. Skipping."
        continue
    fi

    # Get the source URL to build from.
    source_url=$(koji buildinfo "$latest_nvr" | grep '^Source:' | awk '{print $2}')
    echo "Found source URL for $latest_nvr: $source_url"
    if [[ -z "$source_url" ]]; then
        echo "Could not find source URL for $latest_nvr. Skipping."
        continue
    fi

    # Submit the build on RISC-V Koji
    echo "Submitting new build for '$BUILD_TAG' from source: $source_url"
    koji -p riscv build --wait-repo --nowait "$BUILD_TAG" "$source_url"
    echo "Build submitted for $latest_nvr."
done
