#!/bin/bash

# Remove any previously downloaded RPM files
rm -f *.rpm

# Update repository
echo "Empty Repository Initiation"
createrepo .

# Check if packages.txt exists
if [ ! -f packages.txt ]; then
    echo "Error: packages.txt not found."
    exit 1
fi

# Read packages from packages.txt, query the latest version, and download it
while IFS= read -r package; do
    echo "Finding the latest version of $package for x86_64 architecture..."
    latest_version=$(yum --showduplicates list available "$package" | grep -v 'Installed' | grep 'x86_64' | grep -v 'aarch64\|arm' | awk '{print $2}' | sort -rV | head -n 1)
    
    if [ -z "$latest_version" ]; then
        echo "Error: No available version found for $package."
    else
        echo "Downloading $package version $latest_version for x86_64 architecture..."
        yum download --assumeno "$package-$latest_version"
    fi
done < packages.txt

# Update repository
echo "Repository Create"
createrepo .

# Remove downloaded ARM RPM files
rm -f *.arm.rpm

# Remove downloaded aarch64 RPM files
rm -f *.aarch64.rpm

# Download https://copr.fedorainfracloud.org/coprs/kefah/RediSearch/
wget https://download.copr.fedorainfracloud.org/results/kefah/RediSearch/rhel-9-x86_64/07156148-redisearch/redisearch-2.8.12-1.el9.x86_64.rpm
wget https://download.copr.fedorainfracloud.org/results/kefah/RediSearch/rhel-9-x86_64/07156147-rejson/rejson-2.6.9-1.el9.x86_64.rpm
wget https://download.copr.fedorainfracloud.org/results/kefah/RediSearch/rhel-9-x86_64/07156146-redis/redis-7.2.4-1.el9.x86_64.rpm
wget https://download.copr.fedorainfracloud.org/results/kefah/RediSearch/rhel-9-x86_64/07156148-redisearch/redisearch-2.8.12-1.el9.x86_64.rpm
wget https://download.copr.fedorainfracloud.org/results/kefah/RediSearch/rhel-9-x86_64/07156147-rejson/rejson-2.6.9-1.el9.x86_64.rpm 

# Update repository
echo "Repository Update"
createrepo .

