#!/bin/bash

set -x

TMPLOCAL="$(mktemp --directory)"
cd "${TMPLOCAL}"

# Detect the architecture.
# Then download both the 64-bit and 32-bit packages.
if echo $1 | grep -q -P "x86_64.rpm$"; then
    # Example package name: bluez-libs-5.63-1.el8.x86_64.rpm
    echo "x86_64 RPM detected."
    echo "Downloading x86_64 RPM."
    wget "${1}"
    pkg32_url="$(echo ${1} | sed s'/\.x86_64/\.i686/'g)"
    echo "Downloading x86_32 RPM."
    wget "${pkg32_url}"
    rpm2cpio $(basename "${1}") | cpio -idmv
    rpm2cpio $(basename "${pkg32_url}") | cpio -idmv
elif echo $1 | grep -q -P "i686.rpm$"; then
    # Example package name: bluez-libs-5.63-1.el8.i686.rpm
    echo "x86_32 RPM detected."
    echo "Downloading x86_32 RPM."
    wget "${1}"
    pkg64_url="$(echo ${1} | sed s'/\.i686/\.x86_64/'g)"
    echo "Downloading x86_64 RPM."
    wget "${pkg64_url}"
    rpm2cpio $(basename "${1}") | cpio -idmv
    rpm2cpio $(basename "${pkg64_url}") | cpio -idmv
elif echo $1 | grep -q -P "x86_64.eopkg$"; then
    if echo $1 | grep -q -- "-32bit-"; then
        # Example package name: curl-gnutls-32bit-8.1.2-44-1-x86_64.eopkg
        echo "x86_32 Solus package detected."
    echo "Downloading x86_32 RPM."
    wget "${1}"
    pkg64_url="$(echo ${1} | sed s'/\.i686/\.x86_64/'g)"
    echo "Downloading x86_64 RPM."
    wget "${pkg64_url}"
    rpm2cpio $(basename "${1}") | cpio -idmv
    rpm2cpio $(basename "${pkg64_url}") | cpio -idmv
    else
        # Example package name: curl-gnutls-8.1.2-44-1-x86_64.eopkg
        echo "x86_64 Solus Linux package detected."
    fi
elif echo $1 | grep -q -P "x86_64.xbps$"; then
    if echo $1 | grep -q -- "-32bit-"; then
        # Example package name: libjpeg-turbo-32bit-3.0.1_1.x86_64.xbps
        echo "x86_32 Void Linux package detected."
    else
        # Example package name: libjpeg-turbo-3.0.1_1.x86_64.xbps
        echo "x86_64 Void Linux package detected."
    fi
else
    echo "Unsupported package type."
    exit 1
fi

# Clean up.
cd -
#rm -r -f "${TMPLOCAL}"
