#!/bin/bash

# Define the destination directory
DEST_DIR="latest_version_rpms"

# Ensure the destination directory exists
mkdir -p "$DEST_DIR"

# Loop through the matching files
for i in packagegroup-systemcontroller*; do
    board=$(echo $i | sed -e 's/packagegroup-systemcontroller-\(.*\)-1.0-.*/\1/')
    echo "Processing board: $board"
    # Find the latest systemcontroller-app file and copy it
    latest_app=$(ls systemcontroller-app-$board-* | tail -n1)
    if [ -n "$latest_app" ]; then
        echo "Latest app file: $latest_app"
        cp "$latest_app" "$DEST_DIR"
    else
        echo "No app file found for $board"
    fi

    # Find the latest systemcontroller-firmware file and copy it
    latest_firmware=$(ls systemcontroller-firmware-$board-* 2>/dev/null | tail -n1)
    if [ -n "$latest_firmware" ]; then
        echo "Latest firmware file: $latest_firmware"
        cp "$latest_firmware" "$DEST_DIR"
    else
        echo "No firmware file found for $board"
    fi
done
cp packagegroup-systemcontroller* $DEST_DIR

echo "Copy operation completed."

