#!/bin/bash

# Check if a filename is provided as an argument
if [ "$#" -ne 1 ]; then
    echo "Usage: $0 <filename>"
    exit 1
fi

INPUT_FILE="$1"

# Check if the file exists and is readable
if [ ! -f "$INPUT_FILE" ] || [ ! -r "$INPUT_FILE" ]; then
    echo "Error: File not found or not readable: $INPUT_FILE"
    exit 1
fi

echo "Processing tasks from $INPUT_FILE..."

# Read the file line by line
# Use awk to print the last field of each line, which is the task ID
awk '{print $NF}' "$INPUT_FILE" | while read -r task_id; do
    # Validate if task_id is a number
    if [[ "$task_id" =~ ^[0-9]+$ ]]; then
        echo "Setting priority 18 for task ID: $task_id"
        # Run the Koji command
        koji -p riscv set-task-priority --priority 18 "$task_id"
    else
        echo "Skipping invalid line (task ID not found): $task_id"
    fi
done

echo "All tasks processed."
