#!/bin/bash

# === Configuration ===
# The base task ID to check (now taken from the first argument)
BASE_TASK_ID="$1"
# How many seconds to wait between checks
SLEEP_DURATION=10
# =====================

# Check if a task ID was provided as an argument
if [ -z "$BASE_TASK_ID" ]; then
    echo "Usage: $0 <task_id>"
    echo "Please provide the base task ID to monitor."
    exit 1
fi

# The command to run (now uses the variable)
COMMAND="riscv-koji taskinfo $BASE_TASK_ID -r"

echo "Waiting for 'buildArch' task under $BASE_TASK_ID... (Ctrl+C to stop)"

build_arch_id=""
# Loop until 'build_arch_id' is non-empty
while [ -z "$build_arch_id" ]; do
    # Run command and parse with awk
    build_arch_id=$($COMMAND 2>/dev/null | awk '
        # Store task ID when found
        /^  Task: / { current_task_id = $2 }
        
        # If we find "Type: buildArch" and we have a task ID stored,
        # print the ID and exit awk.
        /^  Type: buildArch/ && current_task_id { 
            print current_task_id
            exit 0
        }
    ')
    
    # If not found, print a dot and sleep
    if [ -z "$build_arch_id" ]; then
        echo -n "."
        sleep $SLEEP_DURATION
    fi
done

echo "" # New line after the dots
echo "Found buildArch Task ID: $build_arch_id"
echo "Script finished."
